I have a requirement to pass a JWT client assertion to the oauth2 client credentials grant config record. I'm passing the parameter as the optional parameter. But this parameter has to be generated each time the token endpoint is called for an access token. Therefore I did something like the following.
http:OAuth2ClientCredentialsGrantConfig oauth2Config = {
tokenUrl: "https://*****/oauth2/token",
clientId: "*******",
optionalParams: getJWT(),
clientSecret: "*****",
credentialBearer: oauth2:POST_BODY_BEARER
};
Here, the getJWT() method returns a map with the JWT.
function getJWT() returns map<string> {
string jwt = // logic to generate the JWT
map<string> jwtAssertion = {
"client_assertion" : jwt
};
return jwtAssertion;
}
This works only once. When the access token returned by the token endpoint expires and when the token endpoint is called again for the access token, the getJWT() method does not get called. Therefore, I suppose the new request is going with the old JWT, hence the request fails.
Is there a way to pass a dynamically changing value as a parameter to the http:OAuth2ClientCredentialsGrantConfig record?
You can achieve this by writing a custom ClientOAuth2Handler and using it as described in the imperative approach section.
Your custom handler should check for the
exp
value ofclient_assertion
and create a newhttp:ClientOAuth2Handler
with a newclient_assertion
when it expires. You can get an idea from the below code.