Skip to content

OAuth client to simplify the process of retrieving and managing OAuth tokens. It supports multiple grant types and provides built-in handlers for token refresh and expiration.

License

Notifications You must be signed in to change notification settings

SamhammerAG/OAuth2Authenticator

Repository files navigation

OAuth2Authenticator

ci

OAuth2Authenticator is an OAuth client to simplify the process of retrieving and managing OAuth tokens. It supports multiple grant types and provides built-in handlers for token refresh and expiration.

Nuget Package

Currently supported grant types

Setup

Installation

Install via NuGet:

dotnet add package OAuth2Authenticator

Initialize

Initialize the client service in the application startup. 

public void ConfigureServices(IServiceCollection services)
{
    services.AddOAuth2Authenticator();
}

Usage

OAuth2Authenticator

This class holds the request logic for all OAuth2 grant types. Injectable over the IOAuth2Authenticator interface.

private readonly IOAuth2Authenticator _authenticator; // Retrieve this service via dependency injection.

await _authenticator.PasswordGrant(url, clientId, username, password);

await _authenticator.RefreshTokenGrant(url, clientId, refreshToken);

await _authenticator.ClientCredentialsGrant(url, clientId, clientSecret);

Returns an OAuth2TokenResponse or null. Usage examples for manual token handling can be found in the OAuth2TokenHandler.cs.

OAuth2TokenHandler

This class holds common logic which is needed for token handling. Injectable over the IOAuth2TokenHandler interface.

RefreshHandler

The refresh handler checks whether the access token is about to expire or has already expired and automatically attempts to renew the token with the refresh token. If a renewal with the refresh token is not possible, a new token is retrieved via the specified callback. The handler always attempts to return a valid token.

private readonly IOAuth2TokenHandler _handler; // Retrieve this service via dependency injection.
private static OAuth2TokenResponse token; // Save the last token somewhere static or distributed.

token = await _handler.RefreshHandler(
    token,
    url,
    clientId,
    async () =>
    {
        // Executed to obtain a new token if an refresh was not possible or none exists yet.
        return await _authenticator.PasswordGrant(url, clientId, username, password);
    });

Returns an OAuth2TokenResponse or null.

ClientCredentialsHandler

The client credentials handler checks whether the access token is about to expire or has already expired and automatically requests a new token.

private readonly IOAuth2TokenHandler _handler; // Retrieve this service via dependency injection.
private static OAuth2TokenResponse token; // Save the last token somewhere static or distributed.

token = await _handler.ClientCredentialsHandler(
    token,
    url,
    clientId,
    clientSecret);

Returns an OAuth2TokenResponse or null.

OAuth2TokenResponseExtension

Checks if the token request was successful.

token.Successful()

Checks that the token is not expired.

token.Valid()

License

Apache 2.0

About

OAuth client to simplify the process of retrieving and managing OAuth tokens. It supports multiple grant types and provides built-in handlers for token refresh and expiration.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 5

Languages