krakend

How to extract user_id from JWT and forward to downstream APIs in Krakend?


Is it feasible to forward claims in a JWT as request headers. As its the common use case to have, for instance, the sub claim added as an X-User header to the request.

Can it be propagated via KrakenD components?


Solution

  • Yes, it is feasible to forward claims in a JWT as request headers with KrakenD. For example, this is a common use case to add the sub claim as an X-User header to the request. The propagation allows other KrakenD components, such as rate-limiting, to work with the information in the token.

    Here is an example of how you can configure it:

    {
        "extra_config": {
            "auth/validator": {
                "propagate_claims": [
                    ["sub", "x-user"],
                    ["realm_access.role", "x-role"]
                ]
            }
        }
    }
    

    In this case, the sub claim's value will be added to the request as an x-user header. If the claim does not exist, the mapping is just skipped.

    Please note that the endpoint input_headers also needs to be set so the backend can see the headers​​.

    You'll find more details at https://www.krakend.io/docs/authorization/jwt-validation/#propagate-jwt-claims-as-request-headers