Customize response parameters of oauth response

Viewed 104

I am currently working on a project that is using JDBCTokenStore. By default it return

{
    "access_token": "<somedata>",
    "token_type": "bearer",
    "expires_in": <time>,
    "scope": "read write"
}

Is there a way to customize this response? I tried using TokenEnhancer, but that saves information in DB which is not what I want.

I just want to customize the response and send it back to end user (add an additional parameter like state of the user which changes dynamically based on some criteria).

{
        "access_token": "<somedata>",
        "token_type": "bearer",
        "expires_in": <time>,
        "scope": "read write"
        "state": <true or false>
    }

Any help here would be highly appreciated.

1 Answers

You can customize the token response using a custom TokenEnhancer implementation.

public class CustomTokenEnhancer implements TokenEnhancer {

    private TokenEnhancer delegate;

    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        OAuth2AccessToken result = delegate.enhance(accessToken, authentication);
        result.getAdditionalInformation().put("custom_response_claim", "value");
        return result;
    }
} 

Adding parameters to OAuth2AccessToken.additionalInformation map would be processed as additional parameters in token response.

Related