Adding user-related value to OpenIddict token json response

76 views Asked by At

How does one access user/authentication/principal context during the ApplyTokenResponseContext event, e.g. for adding a patient property to the outgoing json (https://hl7.org/fhir/smart-app-launch/app-launch.html#response-5)

options.AddEventHandler<ApplyTokenResponseContext>( builder =>
{
    builder.UseInlineHandler( context =>
    {
        //how to get some context about the principal?
        context.Response.AddParameter( "patient", "16366164565" );
        return default;
    } );
} );
1

There are 1 answers

0
Steve P On

It turns out that using an event for this is not even needed. In the token endpoint, instead of doing this:

return SignIn(new ClaimsPrincipal(identity), OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);

Create an authentication property to hold the custom value and pass the properties object to the SignIn method:

var authenticationProperties = new AuthenticationProperties();
authenticationProperties.SetParameter( "patient", "66169843987" );
return SignIn(new ClaimsPrincipal(identity), authenticationProperties, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);

The extra property is returned in the token response along with the usual token response, e.g.

{
  "patient": "66169843987",
  "access_token": "*******",
  "token_type": "Bearer",
  "expires_in": 3599
}