RossWright.MetalGuardian.Server 1.0.0-alpha6.6

This is a prerelease version of RossWright.MetalGuardian.Server.
dotnet add package RossWright.MetalGuardian.Server --version 1.0.0-alpha6.6
NuGet\Install-Package RossWright.MetalGuardian.Server -Version 1.0.0-alpha6.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RossWright.MetalGuardian.Server" Version="1.0.0-alpha6.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RossWright.MetalGuardian.Server --version 1.0.0-alpha6.6
#r "nuget: RossWright.MetalGuardian.Server, 1.0.0-alpha6.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RossWright.MetalGuardian.Server as a Cake Addin
#addin nuget:?package=RossWright.MetalGuardian.Server&version=1.0.0-alpha6.6&prerelease

// Install RossWright.MetalGuardian.Server as a Cake Tool
#tool nuget:?package=RossWright.MetalGuardian.Server&version=1.0.0-alpha6.6&prerelease

MetalGuardian

by Ross Wright

Copyright 2023 Pross Co. All Rights Reserved.

Description

Metal Guardian is a library to help implement authentication for HTTP connections using JWT and serve as a lightweight and flexible alternative to ASP.NET Identity. Metal Guardian is even more powerful when combined with MetalNexus.

Licensing

To use this library you must attain and embed a license file from RossWright.com. Simply include the file in a project with Build Action to Embedded Resource.

Client Setup

Reference the RossWright.MetalGuardian package in your project and in your program.cs call builder.Services.AddMetalGuardianBlazor()

On Blazor, setup your HttpClient with builder.Services.AddMetalGuardianHttpClient(/* your server's base url goes here */);

Otherwise, if you are using Metal Guardian with a client other than a Blazor project (for example from a server), you must register an implementation of IAuthenticationTokenRepository before the call to AddMetalGuardianHttpClient to store tokens between sessions.

Server Setup

On your ASP.NET server, the Metal Guardian service requires a appsettings to be added to the configuration:

"MetalGuardian": {
   "JwtIssuer": "https://rosswright.com", // fill in your company url
   "JwtAudience": "https://app.rosswright.com", // fill in your application's client url
   "JwtIssuerSigningKey": "sJKLh678hl_jkh5", // fill in your application's signing key - a very long random alphanumeric string
   "RefreshTokenExpireMins": 10080, // The number of minutes it should take a refresh token to expire, 10080 is 1 week
   "JwtAccessTokenExpireMins": 1440, // The number of minutes it should take an access token to expire, 1440 is 1 day
}

The library is initialized in program.cs by calling: builder.AddMetalGuardian<AuthorizationRepository>() where AuthorizationRepository is your implementation of IAuthorizationRepository

public interface IAuthUserRepository
{
    Task<IAuthenticationUser?> GetUser(string userIdentity, CancellationToken cancellationToken, Func<IAuthenticationUser, bool>? updateUser = null);
    Task AddRefreshToken(Guid userId, string refreshToken, CancellationToken cancellationToken);
    Task<IAuthenticationUser?> RefreshToken(Guid userId, string refreshToken, CancellationToken cancellationToken);
    Task ClearToken(Guid userId, string refreshToken, CancellationToken cancellationToken);
}

public interface IAuthenticationUser
{
    Guid UserId { get; }
    string Name { get; }
    IEnumerable<(string, string)>? Claims { get; }
    string PasswordSalt { get; set; }
    string PasswordHash { get; set; }
    public string? OneTimePasswordSalt { get; set; }
    public string? OneTimePasswordHash { get; set; }
    public DateTimeOffset? OneTimePasswordExpiresAt { get; set; }
}

if the updaterUser parameter to GetUser is not null, it must be called with an object implementing IAuthenticationUser for modification and the modification must be persisted if the return from the Func is true. Ensure multiple refresh tokens can be associated with a user to enable users to sign in from different locations/browsers simultaneously

All of this enables you to inject the IMetalGuardianService:

public interface IMetalGuardianService
{
    Task<AuthenticationTokens> Login(string userIdentity, string password, CancellationToken cancellationToken = default);
    Task Logout(AuthenticationTokens tokens, CancellationToken cancellationToken = default);
    Task<AuthenticationTokens> Refresh(AuthenticationTokens tokens, CancellationToken cancellationToken = default);
    Task<string> GetOneTimePassword(string userIdentifier, int? expirationInMinutes = null, CancellationToken cancellationToken = default);
    Task<AuthenticationTokens> LoginWithOneTimePassword(string userIdentifier, string oneTimePassword, CancellationToken cancellationToken = default);
}

You can implment a controller or MetalNexus request handlers that call the Metal Guardian Service to handle all these concerns. You can decorate your controllers and actions with [Authorize] attributes as usual and access information about the logged in user via the Controller.User property or by injecting IHttpContextAccessor as normal.

On the Blazor client side, in program.cs call builder.Services.AddMetalGuardianHttpClient({baseAddress}) where baseAddress is the base url of your server. Now when you inject an HttpClient, it will be setup with security. You can support connections to multiple servers by specifying a connection name builder.Services.AddMetalGuardianHttpClient({baseAddress}, {connectionName}) which you can then consume by injecting the IHttpClientFactory and calling CreateClient on that service passing the same connectionName.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-alpha6.6 43 6/24/2024
1.0.0-alpha6.5 36 6/23/2024
1.0.0-alpha6.4 34 6/13/2024
1.0.0-alpha6.3 38 6/12/2024
1.0.0-alpha6.2 36 6/12/2024
1.0.0-alpha6.1 42 5/30/2024
1.0.0-alpha6 47 5/30/2024
1.0.0-alpha5.2 45 5/25/2024
1.0.0-alpha5.1 57 5/16/2024
1.0.0-alpha5 52 4/23/2024
1.0.0-alpha4.4 104 4/4/2024
1.0.0-alpha4.2 51 4/4/2024
1.0.0-alpha4.1 56 4/4/2024
1.0.0-alpha4 40 4/4/2024
1.0.0-alpha3.3 51 4/2/2024
1.0.0-alpha3.2 50 4/1/2024
1.0.0-alpha3.1 51 4/1/2024
1.0.0-alpha3 53 4/1/2024
1.0.0-alpha2 61 3/30/2024
1.0.0-alpha1 47 3/23/2024