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())
Index ¶
- func BearerToken(ctx context.Context) (string, bool)
- func ClientID(ctx context.Context) (string, bool)
- func HasScope(ctx context.Context, scope string) bool
- func Request(r *http.Request) *http.Request
- func Scopes(ctx context.Context) []string
- func UserID(ctx context.Context) (string, bool)
- type Middleware
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BearerToken ¶
BearerToken returns the bearer token 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
Click to show internal directories.
Click to hide internal directories.