JWT verification

Description

The JWT policy can verify requests containing HS256 or RS256 signed JSON Web Tokens (as specified in RFC 7519)

Each of your Consumers will have JWT credentials (public and secret keys) which must be used to sign their JWTs.

A token can then be passed through the Authorization header or in the request’s URI or even in the body and the gateway will either proxy the request to your upstream services if the token’s signature is verified, or discard the request if not.

Express Gateway can also verify on some of the registered claims of RFC 7519 (such as exp and nbf).

Usage

In order to use the JWT policy, consumers must have a jwt credential associated with them.

To create consumers (user and apps): use the CLI and create user or create app command.

To create a jwt credential for an user or app: use the CLI and create credential command with type jwt. You can also use the Admin API to do the same thing

Once you create a token, you will get a key pair: keyId and keySecret.

keyId must be placed in the sub field of your JWT. Otherwise, the Gateway will not be able to verify the consumer bound to it, and it will refuse the request. You can skip this check setting (although we do not recommend it) the checkCredentialExistence to false.

keySecret is a suggested password you can use to sign your tokens — so you should put this one in your policy configuration. Anyway, you’re free to select your own secret, if required.

Using RS256

You can also use a public/private key and leverage RS256 to sign and verify your tokens.

First of all, generate a new public/private key using openssl:

$ openssl genrsa -out private.pem 2048

Keep the private key in a super-secure place (under your bed might be a good place) in your system.

Then extract the public key from it:

$ openssl rsa -in private.pem -outform PEM -pubout -out public.pem

Now you have the public key in public.pem and the private one in private.pem.

Use the private key to sign your tokens, and give the public key to Express Gateway using the secretOrPrivateKeyFile parameter, so it can verify your tokens are correct.

Verify external tokens

You don’t have to be the one issuing and signin the tokens. You can also leverage thirthy party providers, such as Auth0 or oAuth.io by downloading their public certificate and provide them to the gateway.

Installation

To enable the JWT policy, add jwt in gateway.config.yml in the policies section.

policies:
  - jwt
  # other policies

Example


http:
  port: 8790
serviceEndpoints:
  example: # will be referenced in proxy policy
    url: 'http://example.com'

apiEndpoints:
  api:
    path: '*'

pipelines:
  example-pipeline:
    apiEndpoints:   # process all request matching "api" apiEndpoint
      - api
    policies:
      jwt: # secure API with key auth
        - action:
      proxy: # name of the policy
        - action:
            serviceEndpoint: example # reference to serviceEndpoints Section

Express Gateway supports several ways to locate your Json Web Token in your request.

Locating the JWT token

By default the Authorization header with Bearer is used to locate your token.

The JWT scheme and header are not standardized, therefore they can be overriden if required.

Other strategies

  • authBearer: (default), extracts the token from the Authorization header with Bearer scheme.
  • authScheme: extracts the token from the Authorization header with the specified scheme.
  • header: extracts the token from the specified header.
  • query: extracts the token from the a specified query parameter

Note: extracting the JWT from a query parameter is usually discouraged for security purposes, as those might get caught by your logging system, disclosing potentially sensitive informations.

Options Reference
  • secretOrPublicKey:
    • the secret used to verify the incoming token
    • required unless secretOrPublicKeyFile is provided
  • secretOrPublicKeyFile:
    • a public key file used to verify the token signature. This can be provided from a thirty party service as well.
    • required unless secretOrPublicKey is provided
  • jwtExtractor:
    • the strategy used to extract the token from the request
    • default value: authBearer
    • possible values: ['authBearer', 'authScheme', 'header', 'query']
  • jwtExtractorField:
    • The field to check, according to the selected extraction strategy.
    • Not required for authBearer strategy
  • audience:
    • if provided, the aud claim will be verified against this value
  • checkCredentialExistence:
    • determines whether the gateway should check for the credential existence in the system, looking at the sub claim.
    • default value: true