Search code examples
asp.net-coreauthenticationoauthidentityserver4openid

Custom response in token endpoint in IdentityServer4


API details:.Net Core 3.1 REST API using IdentityServer4 version 3.1.3

I have many APIs which send responses in a specified format.

For e.g. Register endpoint returns below response:

{
    "responseCode": 0,
    "developerMessage": "Response code not specified.",
    "clientMessage": null,
    "data": {"id":123},
    "exception": null
}

I developed the authentication server using IdentityServer4. But, my token endpoint returns below response:

{
    "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ik...",
    "expires_in": 1209600,
    "token_type": "Bearer",
    "refresh_token": "1u8_VOFHTaeqWEWd6R...",
    "scope": "offline_access api1"
}

Now the requirement is that all the endpoints of the API should return the response in the same format.

Which means I need to change the response of the token (or more) endpoints.

I looked into the ICustomTokenResponseGenerator service (mentioned here) but all it does is adding more fields to the response. And it is from IdentityServer3

class CustomTokenResponseGenerator : ICustomTokenResponseGenerator
{
    public Task<TokenResponse> GenerateAsync(ValidatedTokenRequest request, TokenResponse response)
    {
        response.Custom.Add("custom_field", "custom data");      
        return Task.FromResult(response);
    }
}

But, I want to completely change the response.

Is there any other service that I can use to get the below response?

{
    "responseCode": 0,
    "developerMessage": "Response code not specified.",
    "clientMessage": null,
    "data": 
    {
        "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ik...",
        "expires_in": 1209600,
        "token_type": "Bearer",
        "refresh_token": "1u8_VOFHTaeqWEWd6R...",
        "scope": "offline_access api1"
    },
    "exception": null
}

Solution

  • As explained in your GitHub ticket:

    IdentityServer is an OAuth implementation - what you are suggesting would be incompatible with OAuth and thus is not supported by us.

    If you need to change the complete payload to something custom - write some middleware to intercept the response.