mock-auth0
A tool to mock the auth0 authentication service for development of microservices CONSUMING auth0 jwts
Background
If you use auth0 for authentication and authorization of your users against your microservices, you want to automatically unit test the authentication in your microservice for security. Happy and unhappy paths. Doing this while actually using the auth0 backend is slow and annoying, so auth0 suggest you mock their api. This turns out to be somewhat difficult, especially in the case of using RSA for signing of the tokens and not wanting to heavily dependency inject the middleware for authentication in your koa or express app. This is why I made this tool, which require less changes to your code.
Usage
Lets say you have a pretty standard koa app (I use a factory function to make the app, so I can scope nicely):
const Koa = const Router = const jwt = const jwksRsa = const createApp = { const app = // We are setting up the jwksRsa client as usual (with production host) // We switch off caching to show how things work in ours tests. app const router = // This route is protected by the authentication middleware router app return app}
You can easily unit test the authenticaion of this app like so:
let serverlet requestlet auth0Mock
See also the example.
Under the hood
createAuth0Mock
will create a local PKI and generate a working JWKS.json. Calling auth0Mock.start()
will use nock
to intercept all calls to `${jwksHost}/.well-known/jwks.json`
. So when the jwks-rsa
middleware gets a token to validate
it will fetch the key to verify against from our local PKI instead of the production one and as such, the token is valid
when signed with the local private key.