How to customize(status code,code,response body) introspection response in some case in openiddict

34 Views Asked by At

I'm using openiddict-core library for my new IdentityServer. I'm trying to customize

  • Response body
  • Response status code in some cases. Cases can be
  • If token is given in different client web.xxx but introspection client send cleint_id as android.xxx in request body we should return 401. Currently it returns 200 Ok with success properties
  • If the issuer of token is different than the current host it should return 401. Currently it returns 200 OK with { "active" : false}

After my research I found

` options.AddEventHandler<OpenIddictServerEvents.ExtractIntrospectionRequestContext>(options => options.UseSingletonHandler());

            options.AddEventHandler<OpenIddictServerEvents.ApplyIntrospectionResponseContext>(options =>
                options.UseSingletonHandler<PopulateUserinfo3>());
            
            options.AddEventHandler<OpenIddictServerEvents.ValidateIntrospectionRequestContext>(options =>
                options.UseSingletonHandler<PopulateUserinfo5>());
            options.AddEventHandler<OpenIddictServerEvents.HandleIntrospectionRequestContext>(options =>
                options.UseSingletonHandler<PopulateUserInfo4>());

`

Can be used for introspection manipulation and customization. Which one should I use for the customization?

I tried OpenIddictServerEvents to customize response but several server event handlers can do different things and none of them can do what I want.

What I tried and failed are

public class PopulateUserinfo2 : IOpenIddictServerHandler<OpenIddictServerEvents.ExtractIntrospectionRequestContext>
{
    public ValueTask HandleAsync(OpenIddictServerEvents.ExtractIntrospectionRequestContext context)
    {
        Console.WriteLine("test");
        context.Transaction.GetHttpRequest().HttpContext.Response.StatusCode = 401;
        context.Reject("invalid_token", "The specified token is not valid.");
        context.Transaction.Response = new OpenIddictResponse
        {
            Code = "identity_001"
        };
        context.HandleRequest();
        return default;
    }
}```

```c#
public class PopulateUserinfo3 : IOpenIddictServerHandler<OpenIddictServerEvents.ApplyIntrospectionResponseContext>
{
    public ValueTask HandleAsync(OpenIddictServerEvents.ApplyIntrospectionResponseContext context)
    {
        //check token's clientID and incoming request's clientID are equal
        //if not, return 401
        // //
        context.Response.Code = "identity_001";
         context.Response.Error = OpenIddictConstants.Errors.InvalidToken;
        context.Response.AccessToken = null;
        
        
        return default;
    }
}```

```c#
public class PopulateUserInfo5 : IOpenIddictServerHandler<OpenIddictServerEvents.ValidateIntrospectionRequestContext>
{
    public ValueTask HandleAsync(OpenIddictServerEvents.ValidateIntrospectionRequestContext context)
    {
        Console.WriteLine("test");
        //context.Reject();
        // context.Transaction.GetHttpRequest().HttpContext.Response.StatusCode = 401;
        // context.Reject();
        return default;
    }
}```

```c#
public class PopulateUserinfo5 : IOpenIddictServerHandler<OpenIddictServerEvents.ValidateIntrospectionRequestContext>
{
    public ValueTask HandleAsync(OpenIddictServerEvents.ValidateIntrospectionRequestContext context)
    {
        var clientId = context.Request.ClientId;  // Get the requested client ID
        var scope = context.Request.Scope;       // Get the requested scope
        // context.Reject(OpenIddictConstants.Errors.UnauthorizedClient,
        //     "Client not authorized for this scope");
        // // Check your conditions here
        // if (clientId != "android.customer.x" && scope.Contains("web.x"))
        // {
        //    
        //     return default; // Important to return here to prevent further processing
        // }

        return default; // Continue with validation if conditions are not met
    }
}```
0

There are 0 best solutions below