MParchin.Authority 2.0.0

dotnet add package MParchin.Authority --version 2.0.0                
NuGet\Install-Package MParchin.Authority -Version 2.0.0                
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="MParchin.Authority" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MParchin.Authority --version 2.0.0                
#r "nuget: MParchin.Authority, 2.0.0"                
#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 MParchin.Authority as a Cake Addin
#addin nuget:?package=MParchin.Authority&version=2.0.0

// Install MParchin.Authority as a Cake Tool
#tool nuget:?package=MParchin.Authority&version=2.0.0                

A micro service ready JWT Authentication and Authorization(claim based) solution you can override or extend most of the functionality to suit your needs, almost every functionality is generic with respect to User model so you can use your own model in place of any predefined types

on authority service that is responsible for authentication of users and generating tokens the app needs a database connection using entityframework(db should inherit from DbContext,IAuthorityDb<TDbUser>):

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddJWTAuthentication<JWToken, JWTUser<User>, DbUser, User, Db>(
    env.PUBLIC_KEY_FILE, env.PRIVATE_KEY_FILE, options =>
    {
        options.Expiration = TimeSpan.FromHours(env.TOKEN_EXPIRATION_HOURS);
        options.RefresExpiration = TimeSpan.FromDays(env.REFRESH_EXPIRATION_DAYS);
        options.Authority = env.AUTHORITY;
    });
....
AuthorityEndpoint<JWToken, JWTUser<User>, DbUser, User>.Map(app, "/"); // adding authority api methods

adding mail service:[optional]

builder.Services.AddMailService<MailService>();

adding text-message service:[optional]

builder.Services.AddTextMessageService<TextMessageService>();

by default authority uses in memory storage for otp storage you can also configure it to use redis database:[optional]

builder.Services.AddOTPStorageService(options => options.UseRedisStorage(env.REDIS_HOST));

on any other service that needs to use the token for authorization or simple identification of user:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddJWTAuthentication<JWTUser<User>, User>(env.PUBLIC_KEY_FILE, options =>
{
    options.RespectedAuthorities = env.RESPECTED_AUTHORITIES.Split(',');
});

and to get the user in api methods:

app.MapGet("/", (ClaimsPrincipal principal) => $"Hello {principal.Identity?.Name}!")
    .RequireAuthorization(Authorization.User);

...
or
...

var user = new JWTUser(principal);

also you need a pem encoded rsa key pair for authority service and just the public one for other services you can generate a pair using this commands:

ssh-keygen -t rsa -b 2048 -m PEM -f key
openssl rsa -in key -pubout -outform PEM -out key.pub
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
2.0.0 31 12/1/2024
1.2.1 77 11/24/2024
1.2.0 72 11/23/2024
1.1.1 70 11/21/2024
1.1.0 78 11/21/2024
1.0.2 80 11/10/2024
1.0.1 80 11/10/2024
1.0.0 85 11/10/2024