Go to file
Mathias Beaulieu-Duncan 8249714c6b
All checks were successful
Publish NuGets / build (release) Successful in 18s
fix git url -.-
2024-12-22 13:44:10 -05:00
.gitea/workflows remove extra layer of publishing 2024-12-22 13:40:32 -05:00
OpenHarbor.JwtTokenManager fix git url -.- 2024-12-22 13:44:10 -05:00
OpenHarbor.JwtTokenManager.Abstractions fix git url -.- 2024-12-22 13:44:10 -05:00
Test added packages infos 2024-12-20 01:57:54 -05:00
.gitignore initial commit 2024-12-20 01:50:06 -05:00
icon.png added icon and readme for nugets, added pipeline verification and include symbols and source 2024-12-22 11:58:30 -05:00
LICENSE minor optimization to reuse the HttpClient and added helper function 2024-12-21 02:05:30 -05:00
OpenHarbor.JwtTokenManager.sln initial commit 2024-12-20 01:50:06 -05:00
README.md rename README.md 2024-12-22 12:17:13 -05:00

Lightweight library allowing to manage access token its lifetime with caching capability for services accounts with the oauth2 protocol

Installing Nuget

Install nuget package to your awesome project.

Full Version NuGet NuGet Install
OpenHarbor.JwtTokenManager NuGet PM> Install-Package OpenHarbor.JwtTokenManager
OpenHarbor.JwtTokenManager.Abstractions NuGet PM> Install-Package OpenHarbor.JwtTokenManager.Abstractions

How to use

appsettings.json

{
"JwtTokenManager": {
    "TokenEndpoint": "",
    "ClientId": "",
    "ClientSecret": "",
    "Scopes": []
  }
}

Program.cs

builder.Services.AddHttpClient();
builder.Services.AddMemoryCache(); // use the IMemoryCache provider you want

builder.Services.AddJwtTokenManager(builder.Configuration, "JwtTokenManager");

Program.cs with deeper configurations

builder.Services.AddHttpClient();

builder.Services.AddJwtTokenManager(builder.Configuration, "JwtTokenManager", options =>
{
  options.Cache(cacheOptions =>
      {
        cacheOptions.ExpirationOffset = 30; // 15 by default
      }, 
      // optional to configure your own IMemoryCache provider for the token management
      provider => provider.GetRequiredService<IMemoryCache>());
  
  options.Configuration(overrideConfiguration =>
  {
    overrideConfiguration.Scopes = ["offline_access"];
  });
});

Use the JwtTokenManager

public class MyService(HttpClient httpClient, IJwtTokenManagerService jwtTokenManagerService)
{
  public async Task DoActionAsync(CancellationToken cancellationToken) {
    
    var tokenResult = await jwtTokenManagerService.GetTokenAsync(cancellationToken);
    httpClient.SetJwtAccessToken(tokenResult);
    ...
  }
}