Description

New returns a middleware to be used with the JWTSecurity DSL definitions of goa. It supports the scopes claim in the JWT and ensures goa-defined Security DSLs are properly validated.

The steps taken by the middleware are:

1. Validate the "Bearer" token present in the "Authorization" header against the key(s)
   given to New
2. If scopes are defined in the design for the action validate them against the "scopes" JWT
   claim

The `exp` (expiration) and `nbf` (not before) date checks are validated by the JWT library.

validationKeys can be one of these:

* a single string
* a single []byte
* a list of string
* a list of []byte
* a single rsa.PublicKey
* a list of rsa.PublicKey

The type of the keys determine the algorithms that will be used to do the check. The goal of having lists of keys is to allow for key rotation, still check the previous keys until rotation has been completed.

You can define an optional function to do additional validations on the token once the signature and the claims requirements are proven to be valid. Example:

validationHandler, _ := goa.NewMiddleware(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
    token := jwt.ContextJWT(ctx)
    if val, ok := token.Claims["is_uncle"].(string); !ok || val != "ben" {
        return jwt.ErrJWTError("you are not uncle ben's")
    }
})

Mount the middleware with the generated UseXX function where XX is the name of the scheme as defined in the design, e.g.:

app.UseJWT(jwt.New("secret", validationHandler, app.NewJWTSecurity()))

New is referenced in 6 repositories

github.com/goadesign/goa github.com/gopheracademy/congo github.com/potatogopher/omnibus

...