1 Star 0 Fork 0

kyzala / AlastackAuthenticationHmacAuth

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Alastack Authentication HmacAuth

A .NET API supports Hmac and Hawk authentication.

GitHub Workflow StatusGitHub

Getting started

Install package from the .NET CLI

Client:

dotnet add package Alastack.Authentication.HmacAuth

AspNetCore:

dotnet add package Alastack.Authentication.HmacAuth.AspNetCore

Hmac Authentication

The following code snippet demonstrates creating a Hmac authentication client:

var authHandler = new HmacDelegatingHandler("id123", "3@uo45er?")
{
    InnerHandler = new SocketsHttpHandler
    {
        ConnectTimeout = TimeSpan.FromSeconds(10),
        PooledConnectionLifetime = TimeSpan.FromSeconds(1000),
        SslOptions = new SslClientAuthenticationOptions()
        {
            RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true
        },
        UseCookies = false
    }
};
var client = new HttpClient(authHandler, disposeHandler: false)
{
    BaseAddress = new Uri("https://localhost:5001/")
};

The following code snippet demonstrates creating a Hmac authentication client with dependency injection and then invoking it:

var host = new HostBuilder()
.ConfigureServices(services =>
{
    services.Configure<HmacSettings>(options =>
    {
        options.AppId = "id123";
        options.AppKey = "3@uo45er?";
    });
    services.AddSingleton<IValidateOptions<HmacSettings>, HmacSettingsValidation>();
    services.AddTransient<HmacDelegatingHandler>();
    services.AddHttpClient<ApiClient>("ApiClient", httpClient => 
        httpClient.BaseAddress = "https://localhost:5001/")
    .AddHttpMessageHandler<HmacDelegatingHandler>();
}).Build();
var apiClient = host.Services.GetRequiredService<ApiClient>();
await apiClient.CreateTodoItemAsync(new TodoItem { Name = "walk dog" });

The following code will add Hmac authentication for AspNetCore:

builder.Services.AddAuthentication()
.AddHmac(options =>
{
     var credential = new HmacCredential { AppId = "id123", AppKey = "3@uo45er?" };
     var dict = new Dictionary<string, HmacCredential> { { "id123", credential } };
     options.CredentialProvider = new MemoryCredentialProvider<HmacCredential>(dict);
});

Hawk Authentication

You can use Postman as Hawk authentication client. For more information see Authenticate with Hawk access authentication.

The following code snippet demonstrates creating a Hawk authentication client:

var authHandler = new HawkDelegatingHandler("id123", "3@uo45er?")
{
    InnerHandler = new SocketsHttpHandler
    {
        ConnectTimeout = TimeSpan.FromSeconds(10),
        PooledConnectionLifetime = TimeSpan.FromSeconds(1000),
        SslOptions = new SslClientAuthenticationOptions()
        {
            RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true
        },
        UseCookies = false
    }
};
var client = new HttpClient(authHandler, disposeHandler: false)
{
    BaseAddress = new Uri("https://localhost:5001/")
};

The following code snippet demonstrates creating a Hawk authentication client with dependency injection and then invoking it:

var host = new HostBuilder()
.ConfigureServices(services =>
{
    services.Configure<HawkSettings>(options =>
    {
        options.AuthId = "id123";
        options.AuthKey = "3@uo45er?";
    });
    services.AddSingleton<IValidateOptions<HawkSettings>, HawkSettingsValidation>();
    services.AddTransient<HawkDelegatingHandler>();
    services.AddHttpClient<ApiClient>("ApiClient", httpClient => 
        httpClient.BaseAddress = "https://localhost:5001/")
    .AddHttpMessageHandler<HawkDelegatingHandler>();
}).Build();
var apiClient = host.Services.GetRequiredService<ApiClient>();
await apiClient.CreateTodoItemAsync(new TodoItem { Name = "walk dog" });

The following code will add Hawk authentication for AspNetCore:

builder.Services.AddAuthentication()
.AddHawk(options =>
{
     var credential = new HawkCredential { AuthId = "id123", AuthKey = "3@uo45er?" };
     var dict = new Dictionary<string, HawkCredential> { { "id123", credential } };
     options.CredentialProvider = new MemoryCredentialProvider<HawkCredential>(dict);
});

NuGet Packages

This repo builds the following packages.

Package Version Description
Alastack.Authentication.HmacAuth Nuget Hmac and Hawk authentication client.
Alastack.Authentication.HmacAuth.AspNetCore Nuget Hmac and Hawk authentication for ASP.NET Core.
Alastack.Authentication.HmacAuth.Sql Nuget Sql CredentialProvider.
Alastack.Authentication.HmacAuth.MongoDB Nuget MongoDB CredentialProvider.
Alastack.Authentication.HmacAuth.EntityFrameworkCore Nuget EntityFrameworkCore CredentialProvider.
Alastack.Authentication.HmacAuth.LiteDB Nuget LiteDB CredentialProvider.

Configure Authentication

  • Hmac - HmacSettings for authentication client, HmacOptions for AspNetCoe.
  • Hawk - HawkSettings for authentication client, HawkOptions for AspNetCoe.

Performance

  • ICredentialCache<TCredential> defines credential cache abstraction.
  • CredentialCache<TCredential> is the default implementation.
  • IDataCache instance stores credential data.
  • CacheKeyPrefix and CredentialCacheTime options configure cache parameters.

API Extentions

  • ICrypto

A hash algorithms abstraction. DefaultCrypto is the default implementation.

  • ICryptoFactory

A factory abstraction for a component that can create ICrypto instances. DefaultCryptoFactory is the default implementation.

  • INonceGenerator

A nonce generator abstraction. NonceGenerator is the default implementation.

  • ITimestampCalculator

A timestamp calculator abstraction. TimestampCalculator is the default implementation.

  • IAuthorizationParameterExtractor

a HTTP Authorization header parameter extractor abstraction. HmacParameterExtractor is the Hmac authentication implementation. HawkParameterExtractor is the Hawk authentication implementation.

  • IHostResolver

A host resolver abstraction. DefaultHostResolver is the default implementation. DefaultHostResolver supports forwarded header. HmacOptions.ForwardIndex and HawkOptions.ForwardIndex is used to set the reverse host index of the forwarding header.

The following HTTP headers display X-Forwarded information.

X-Forwarded-Host: 192.168.1.103, 192.168.1.102:1080, 192.168.1.103:2080, 192.168.1.102, 192.168.1.103:3080
X-Forwarded-Proto: http, http, http, https, http

If ForwardIndex is 3, DefaultHostResolver will return 192.168.1.102:1080.

  • IReplayRequestValidator

A HTTP replay request Validator abstraction. ReplayRequestValidator is the default implementation.

  • ICredentialProvider<TCredential>

A credential provider abstraction. MemoryCredentialProvider<TCredential> is a in-memory implementation.

  • IDataCache

A data cache abstraction. DataCache integrates in-memory and distributing cache implementation.

Samples

Visit the samples folder.

MIT License Copyright (c) 2022 kyzala Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A .NET API supports Hmac and Hawk authentication. 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/kyzala/AlastackAuthenticationHmacAuth.git
git@gitee.com:kyzala/AlastackAuthenticationHmacAuth.git
kyzala
AlastackAuthenticationHmacAuth
AlastackAuthenticationHmacAuth
main

搜索帮助