oauth2

package
v0.1.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package oauth2 provides a middelware that introspects the auth token on behalf of PACE services and populate the request context with useful information when the token is valid, otherwise aborts the request.

Example
r := mux.NewRouter()

// Alternatively, you can construct the Middleware using ENV variables and
// our custom constructor `NewMiddlware`, example:
//
// `OAUTH2_URL=XXX OAUTH2_CLIENT_ID=YYY OAUTH2_CLIENT_SECRET=ZZZ bin_to_start_your_service`
//
// Then, in your code:
//
// middleware = NewMiddleware()
middleware := Middleware{
	URL:          "http://localhost:3000",
	ClientID:     "13972c02189a6e938a4730bc81c2a20cc4e03ef5406d20d2150110584d6b3e6c",
	ClientSecret: "7d26f8918a83bd155a936bbe780f32503a88cb8bd3e8acf25248357dff31668e",
}

r.Use(middleware.Handler)

r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	userid, _ := UserID(r.Context())
	log.Printf("AUDIT: User %s does something", userid)

	if HasScope(r.Context(), "dtc:codes:write") {
		fmt.Fprintf(w, "User has scope.")
		return
	}

	fmt.Fprintf(w, "Your client may not have the right scopes to see the secret code")
})

srv := &http.Server{
	Handler: r,
	Addr:    "127.0.0.1:8000",
}

log.Fatal(srv.ListenAndServe())
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BearerToken

func BearerToken(ctx context.Context) (string, bool)

BearerToken returns the bearer token stored in ctx

func ClientID

func ClientID(ctx context.Context) (string, bool)

ClientID returns the clientID stored in ctx

func HasScope

func HasScope(ctx context.Context, scope string) bool

HasScope checks if scope is stored in ctx

func Request

func Request(r *http.Request) *http.Request

Request adds Authorization token to r

func Scopes

func Scopes(ctx context.Context) []string

Scopes returns the scopes stored in ctx

func UserID

func UserID(ctx context.Context) (string, bool)

UserID returns the userID stored in ctx

Types

type Middleware

type Middleware struct {
	URL          string
	ClientID     string
	ClientSecret string
	// contains filtered or unexported fields
}

Middleware holds data necessary for Oauth processing

func NewMiddleware

func NewMiddleware() *Middleware

NewMiddleware creates a new Oauth middleware

func (*Middleware) Handler

func (m *Middleware) Handler(next http.Handler) http.Handler

Handler will parse the bearer token, introspect it, and put the token and other relevant information back in the context.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL