Go to file
Mathias Beaulieu-Duncan 6bebdf916c
Some checks failed
Publish NuGets / build (release) Failing after 12s
include symbols and source into the project
2024-12-22 12:10:29 -05:00
.gitea/workflows include symbols and source into the project 2024-12-22 12:10:29 -05:00
OpenHarbor.JwtTokenManager include symbols and source into the project 2024-12-22 12:10:29 -05:00
OpenHarbor.JwtTokenManager.Abstractions include symbols and source into the project 2024-12-22 12:10:29 -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 minor optimization to reuse the HttpClient and added helper function 2024-12-21 02:05:30 -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);
    ...
  }
}