Giter Site home page Giter Site logo

ring-jwt-middleware's Introduction

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 "[email protected]"
 "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

ring-jwt-middleware's People

Contributors

yogsototh avatar frenchy64 avatar craigbro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.