Skip to content

OAuth authorized HttpClient, friendly with HttpClientFactory

License

Notifications You must be signed in to change notification settings

akunzai/GSS.Authorization.OAuth

Repository files navigation

GSS.Authorization.OAuth

Build Status Code Coverage

OAuth authorized HttpClient, friendly with HttpClientFactory

NuGet Packages

Installation

# OAuth 1.0 protocol
dotnet add package GSS.Authorization.OAuth.HttpClient

# OAuth 2.0 protocol
dotnet add package GSS.Authorization.OAuth2.HttpClient

Limits

OAuth 1.0 protocol

  • Only provide HMAC-SHA1 and PLAINTEXT signature method. You can implement GSS.Authorization.OAuth.IRequestSigner to support more signature methods.
  • Only provide InteractiveConsoleAuthorizer grant flow. You can implement GSS.Authorization.OAuth.IAuthorizer to support more grant flows.

OAuth 2.0 protocol

  • Only provide Client-Credentials and Resource-Owner-Credentials grant flow, You can implement GSS.Authorization.OAuth2.IAuthorizer to support more grant flows.

Usage

Check out these samples to learn the basics and key features.

Named OAuth 1.0 HttpClient

services.AddOAuthHttpClient("oauth",(resolver, options) =>
{
    var configuration = resolver.GetRequiredService<IConfiguration>();
    options.ClientCredentials = new OAuthCredential(configuration["OAuth:ClientId"], configuration["OAuth:ClientSecret"]);
    options.TokenCredentials = new OAuthCredential(configuration["OAuth:TokenId"],configuration["OAuth:TokenSecret"]);
    options.SignedAsQuery = configuration.GetValue("OAuth:SignedAsQuery", false);
});

Typed OAuth 1.0 HttpClient

services.AddOAuthHttpClient<OAuthHttpClient>((resolver, options) =>
{
    var configuration = resolver.GetRequiredService<IConfiguration>();
    options.ClientCredentials = new OAuthCredential(configuration["OAuth:ClientId"], configuration["OAuth:ClientSecret"]);
    options.TokenCredentials = new OAuthCredential(configuration["OAuth:TokenId"],configuration["OAuth:TokenSecret"]);
    options.SignedAsQuery = configuration.GetValue("OAuth:SignedAsQuery", false);
});

Named OAuth 2.0 HttpClient

services.AddOAuth2HttpClient<ClientCredentialsAuthorizer>("oauth2",(resolver, options) =>
{
    var configuration = resolver.GetRequiredService<IConfiguration>();
    options.AccessTokenEndpoint = configuration.GetValue<Uri>("OAuth2:AccessTokenEndpoint");
    options.ClientId = configuration["OAuth2:ClientId"];
    options.ClientSecret = configuration["OAuth2:ClientSecret"];
    options.Credentials = new NetworkCredential(configuration["OAuth2:Credentials:UserName"], configuration["OAuth2:Credentials:Password"]);
    options.Scopes = configuration.GetSection("OAuth2:Scopes").Get<IEnumerable<string>>();
});

Typed OAuth 2.0 HttpClient

services.AddOAuth2HttpClient<OAuth2HttpClient, ResourceOwnerCredentialsAuthorizer>((resolver, options) =>
{
    var configuration = resolver.GetRequiredService<IConfiguration>();
    options.AccessTokenEndpoint = configuration.GetValue<Uri>("OAuth2:AccessTokenEndpoint");
    options.ClientId = configuration["OAuth2:ClientId"];
    options.ClientSecret = configuration["OAuth2:ClientSecret"];
    options.Credentials = new NetworkCredential(configuration["OAuth2:Credentials:UserName"], configuration["OAuth2:Credentials:Password"]);
    options.Scopes = configuration.GetSection("OAuth2:Scopes").Get<IEnumerable<string>>();
});