node.jsjwtexpress-gateway

Decode and Set Jwt payload to request parametes


We are using express-gateway for our micro services. We have set up authentication using Jwt. We want to verify jwt and decode the payload and set it to req params

This is our gateway.config.yml file

http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  user:
    host: localhost
    paths: "/v1/users"
  product:
    host: localhost
    paths: "/v1/products"
serviceEndpoints:
  user:
    url: http://localhost:3001
  product:
    url: http://localhost:3000
policies:
- basic-auth
- key-auth
- cors
- expression
- log
- oauth2
- proxy
- rate-limit
- jwt
pipelines:
- name: default-1
  apiEndpoints:
  - user
  policies:
  - jwt:
    - action:
        secretOrPublicKey: privatekey
        checkCredentialExistence: 'false'
  - proxy:
    - action:
        serviceEndpoint: user
- name: default-2
  apiEndpoints:
  - product
  policies:
  - jwt:
    - action:
        secretOrPublicKey: privatekey
        checkCredentialExistence: 'false'
  - proxy:
    - action:
        serviceEndpoint: product

My Jwt payload token looks like

{
  "org": "1234567890",
  "siteID": "343434343",
  "expiry": "600"
}

After decoding and verifying the jwt signature the gateway should set the payload information to the req as

req.org = payload.org
req.siteId = payload.siteId

And this is passed to our underlying microservices. How to do this. Should I set any more parameter in the gateway.config.yml file. Please advice. Thank you


Solution

  • all you need to do this is grab these properties from req.user — that's where the decoded payload is stored.