Yerson Quintana
Yerson Quintana

Reputation: 1

Customizing JWT generation not working in WSO2 APIM 4.0.0

I tried to implement the steps given in WSO2 4.0.0 documentation https://apim.docs.wso2.com/en/4.0.0/deploy-and-publish/deploy-on-gateway/api-gateway/passing-enduser-attributes-to-the-backend-via-api-gateway/ for customizing JWT Claims.

As given in the documentation, I created the custom JWT generator java class, generated the jar and placed it under WSO2 Home/repository/components/lib folder . Did the necessary configurations in deployment.toml for enabling JWT and restarted the server.

Need suggestions on this as i have followed the steps given in the documentation.

I execute the examples CustomGatewayJWTGenerator,CustomJWTGenerator of repo official examples.

{
  "sub": "xxxx",
  "aut": "xxxx",
  "aud": "xxx",
  "nbf": xxx,
  "azp": "xxx",
  "scope": "xxx",
  "iss": "xx://xxx:9443/oauth2/token",
  "exp": xxxxx,
  "iat": xxxxx,
  "jti": "xxxx-xxxx-xxx-xxxx-xxxxxx"
  //custom
   "name:"xxxxx",
    "lastname":"xxxx"
}

deployment.toml

api-manager.xml

repository\components\dropins

repository\components\lib

Upvotes: 0

Views: 297

Answers (1)

Binod Karunanayake
Binod Karunanayake

Reputation: 626

You can customize JWT by adding custom claims using a CustomGatewayJWTGenerator as in docs. Follow the steps below to test this.

  1. Get this sample code and modify the CustomGatewayJWTGenerator.populateStandardClaims() class as below.
@Override
public Map<String, Object> populateStandardClaims(JWTInfoDto jwtInfoDto) {
    Map<String, Object> claims = super.populateStandardClaims(jwtInfoDto);
    claims.put("custom-attribute", "123");
    return claims;
}
  1. Run mvn clean install and generate the jar file.
  2. Copy CustomGatewayJWTGenerator-1.0-SNAPSHOT.jar to <APIM_HOME>/repository/components/dropins directory.
  3. Add following config to the <APIM_HOME>/repository/conf/deployment.toml.
[apim.jwt]
enable = true

[apim.jwt.gateway_generator]
impl = "org.wso2.carbon.test.CustomGatewayJWTGenerator"
  1. Start/Restart the server.
  2. Create and publish a sample REST API in Publisher.
  3. Subscribe and invoke the created API in Devportal.
  4. You can see that the custom attribute is passed to the backend by decoding the JWT.
{
  ...

  "custom-attribute": "123",

  ...
}

Upvotes: 2

Related Questions