/ring-jwt-middleware

JWT auth checks for Ring

Primary LanguageClojureEclipse Public License 1.0EPL-1.0

https://travis-ci.org/threatgrid/ring-jwt-middleware.png?branch=master

ring-jwt-middleware

A simple middleware to authenticate users using JWT (JSON Web Tokens) currently, only RS256 is supported.

Features

  • RS256 signing
  • uses IANA “JSON Web Token Claims”
  • JWT lifetime & Expiration support
  • custom additional validation through a user provided fn
  • custom revokation check through a user provided fn

Usage

Middleware & options

Use wrap-jwt-auth-fn to create an instance of the middleware, wrap your routes with it:

(let [wrap-jwt
      (wrap-jwt-auth-fn {:pubkey-path jwt-cert-path
                         :is-revoked-fn revoked?
                         :jwt-max-lifetime-in-sec jwt-lifetime
                         :jwt-check-fn check-jwt-fields
                         :no-jwt-handler authorize-no-jwt-header-strategy})]
  (api (api-data url-prefix)
        (middleware [wrap-jwt]
          (routes service url-prefix))))
OptionDescriptionDefault
:pubkey-paththe path to your public keynil
:pubkey-fnA fn from claims to public key, has precedence over pubkey-pathnil
:jwt-max-lifetime-in-secset a max lifetime for JWTs to expire86400
:is-revoked-fna fn to checks if a given JWT should be revoked, should return boolnil
:jwt-check-fna fn to custom check the JWT, should return a vec of error strings or nilnil
:no-jwt-handlera middleware to pass when no JWT header is found (handler -> (request -> response))forbid-no-jwt-header-strategy
:post-jwt-format-fna fn that given a JWT generate an identity informationjwt->user-id (the “sub” claim)

If the request contains a valid JWT auth header, the JWT is merged with the ring request under a :jwt key as well with a :identiy key. Otherwise the request is passed to :no-jwt-handler.

By default if no JWT auth header is found the request is terminated with unauthorized HTTP response. You can use authorize-no-jwt-header-strategy for the :no-jwt-handler key if you want to manage how to deal with that case in you own handler. You could also provide your own middleware function for this case.

By default the :identity contains the "sub" field of the JWT. But you can use more complex transformation. For example, there is a `jwt->oauth-ids` function in the code that could be used to handle JWT generated from an OAuth2 provider.

JWT Format

Currently this middleware only supportes JWTs using claims registered in the IANA “JSON Web Token Claims”, which means you need to generate JWTs using most of the claims described here: https://tools.ietf.org/html/rfc7519#section-4 namely jti, exp, iat, nbf,=sub=:

ClaimDescriptionFormat
:expExpiration time: https://tools.ietf.org/html/rfc7519#section-4.1.4Long
:iatIssued At: https://tools.ietf.org/html/rfc7519#section-4.1.6Long
:jtiJWT ID: https://tools.ietf.org/html/rfc7519#section-4.1.7String
:nbfNot Before: https://tools.ietf.org/html/rfc7519#section-4.1.5Long
:subSubject: https://tools.ietf.org/html/rfc7519#section-4.1.2String

here is a sample token:

{:jti "r3e03ac6e-8d09-4d5e-8598-30e51a26cd2a"
 :exp 1499419023
 :iat 1498814223
 :nbf 1498813923
 :sub "f0010924-e1bc-4b03-b600-89c6cf52757c"

 :email "foo@bar.com"
 "http://example.com/claim/user/name" "john doe"}

Generating Certs and a Token

A simple script is available to generate keys for signing the tokens: > ./resources/cert/gen_cert.sh some dummy ones are already available for easy testing.

  • use ring-jwt-middleware.core-test/make-jwt to generate a sample token from a map

License

Copyright © 2015-2019 Cisco Systems Eclipse Public License v1.0