Skip to content

Latest commit

 

History

History
88 lines (78 loc) · 1.61 KB

inject-oauth2-sub.md

File metadata and controls

88 lines (78 loc) · 1.61 KB

How to inject OAuth 2 subject into request

Prerequisites

Routing rule

Configure authn plugin to inject access token claims into authentication context:

...
"requestPlugins": [
  {
    "name": "authn",
    "conf": {
      "methods": ["oauth2"],
      "entities": ["jwt"]
    }
  },
  ...
]
...

and then put sub claim into X-USER-ID request header:

...
"requestPlugins": [
  ...
  {
    "name": "transform-request",
    "conf": {
      "headers": {
        "set": {
          "X-USER-ID": "$authn.sub"
        }
      }
    }
  }
]
...

Full configuration:

{
  "rules": [
    {
      "default": {
        "targetHost": "example.com",
        "targetPort": 80
      },
      "endpoints": [
        {
          "method": "GET",
          "pathPattern": "/user",
          "requestPlugins": [
            {
              "name": "authn",
              "conf": {
                "methods": ["oauth2"],
                "entities": ["jwt"]
              }
            },
            {
              "name": "transform-request",
              "conf": {
                "headers": {
                  "set": {
                    "X-USER-ID": "$authn.sub"
                  }
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

NOTE
Instead of oauth2 authentication method you can use oauth2-introspect and read the subject from token introspection response body.