OAuthHandler.Options property is null?

Viewed 27

defined an oauth handler like so which works just fine.

public class MyHandler : OAuthHandler<MyOptions> {
    public MyHandler(IOptionsMonitor<MyOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
        : base(options, logger, encoder, clock) { }
    // overriden CreateTicketAsync and BuildChallengeUrl protected methods.
}

but the authenticator requires that user can revoke authorization which doesn't seem to be supported by oauth in a native fashion. so in the handler, I added a specialization which is to revoke authorization like so.

public class MyHandler : OAuthHandler<MyOptions> {
    public async Task RevokeAuthorizationAsync(string token) {
        if(token == null) throw new ArgumentNullException(nameof(token));

        // below, Options throws a NullReferenceException???
        var request = new HttpRequestMessage(HttpMethod.Post, Options.RevocationEndpoint);
        // other relevant code here...
    }
}

so when the user hits the revoke button from the app, the account controller revoke method is called.

public class AccountController : Controller {
    [Authorize]
    public async Task Revoke() 
        => HttpContext.RevokeAuthorizationAsync("refreshTokenObtainedByWhateverMean");
}

// which in turn calls upon the HttpContext extension written expressly for the purpose

public static class MyHttpContextExtensions {
    public static async Task RevokeAuthorizationAsync(this HttpContext context, string accessOrRefreshToken) {
        var handler=context.RequestServices.GetRequiredService<MyHandler>();
        await handler.RevokeAuthorizationAsync(accessOrRefreshToken);
    }
}

upon the call to MyHandler.RevokeAuthorizationAsync, the OAuthHandler.Options is null? how can that be? when authenticating and authorizing from within the MyHandler.CreateTicketAsync and MyHandler.BuildChallengeUrl, the OAuthHandler.Options property is set.

I suspect that I might not instantiate the MyHandler class properly using the HttpContext.RequestServices.GetRequiredService method. but if this is not it, how could I specialize MyHandler and provide OAuthHandler.Options ? because I need the Options.ClientId and Options.ClientSecret to revoke the authorization. both are configured like so from the program class.

public class Program {
    builder.Services
        .AddAuthentication(o => {
            // some config here
        })
        .AddCookie()
        .AddMyAuthenticator(o => {
            o.ClientId=Configuration["ClientId"];
            o.ClientSecret=Configuration["ClientSecret"];
        });
}

so how is that OAuthHandler.Options is instantiated within the MyHandler.CreateTicketAsync and MyHandler.BuildChallengeUrl, and is null when I call upon the MyHandler.RevokeAuthorizationAsync method?

0 Answers
Related