express-gateway icon indicating copy to clipboard operation
express-gateway copied to clipboard

Decoded JWT is not being saved in req.user when verified

Open JoeKolba opened this issue 6 years ago • 4 comments

[x] Bug

JWT Policy is validating the JWT correctly, however the user in req.user is not being created. I am running the latest version.

JWT: { "sub": "1234567890", "name": "John Doe", "admin": "true", }

gateway config: policies: - jwt: - action: secretOrPublicKey: secret checkCredentialExistence: false - proxy: - action: serviceEndpoint: endpoint

JoeKolba avatar Jan 04 '20 04:01 JoeKolba

I have the same problem, I don't know if it's a bug or not but can't figure it out.

vahid4mm avatar Jan 29 '20 22:01 vahid4mm

After hours of hair pulling, I found this link https://www.express-gateway.io/docs/policies/request-transformer/ it seems the req.user is not added by default and it's necessary to use request-transformer and add it to body or header as you wish. I really don't know if this is mentioned somewhere in the docs or not because I'm terrible at reading docs.

policies:
      -
        jwt:
          action:
            secretOrPublicKey: theKEY
            checkCredentialExistence: false
      -
        request-transformer:
          action:
            body:
              add:
                user: req.user

vahid4mm avatar Jan 30 '20 00:01 vahid4mm

This is on purpose. Express Gateway will never touch your request body — if you need to forward it somehow you need effectively to use the request transformed to add is as an header or in the body — that is really up to you.

The issue here pointed out is different though — he's complaining that req.user is null — a totally different thing from what you're describing here.

XVincentX avatar Jan 30 '20 11:01 XVincentX

The fact that this is on purpose totally make sense. Actually everything about express gateway make sense. I'm sorry that I didn't understand the actual problem @deviant32 has mentioned (to be fair, the post doesn't talk about being null).

vahid4mm avatar Jan 30 '20 16:01 vahid4mm