16 Commits

Author SHA1 Message Date
david.nguyen 61f7c0f05e added credentials in header support
Publish NuGets / build (release) Successful in 25s
2025-03-17 09:46:54 -04:00
mathias 20518b8f6a update readme 2024-12-22 23:13:37 -05:00
mathias 8249714c6b fix git url -.-
Publish NuGets / build (release) Successful in 18s
2024-12-22 13:44:10 -05:00
mathias 7e1e5c76b7 remove extra layer of publishing 2024-12-22 13:40:32 -05:00
mathias 6fee2c5c9a fix symbols, I think...
Publish NuGets / build (release) Failing after 19s
2024-12-22 13:38:47 -05:00
mathias 0f1f900055 fix options binding
Publish NuGets / build (release) Failing after 18s
2024-12-22 13:10:44 -05:00
mathias 892101a84d prepare AoT support 2024-12-22 13:03:54 -05:00
mathias 3478750bc9 remove nuget verification for now
Publish NuGets / build (release) Failing after 18s
2024-12-22 12:22:51 -05:00
mathias e7ebf0cf19 rename README.md
Publish NuGets / build (release) Failing after 16s
2024-12-22 12:17:13 -05:00
mathias 4c5b5aec66 fix pipeline
Publish NuGets / build (release) Failing after 16s
2024-12-22 12:15:41 -05:00
mathias 6bebdf916c include symbols and source into the project
Publish NuGets / build (release) Failing after 12s
2024-12-22 12:10:29 -05:00
mathias 2e4a8b6d98 fix pipeline for include symbols and source
Publish NuGets / build (release) Failing after 12s
2024-12-22 12:03:21 -05:00
mathias 602076b669 added icon and readme for nugets, added pipeline verification and include symbols and source
Publish NuGets / build (release) Failing after 13s
2024-12-22 11:58:30 -05:00
mathias 9df5750f1c part of last commit
Publish NuGets / build (release) Successful in 18s
2024-12-21 02:07:13 -05:00
mathias d1e217329a minor optimization to reuse the HttpClient and added helper function 2024-12-21 02:05:30 -05:00
mathias 4e9119c8c7 refactor Token to AccessToken
Publish NuGets / build (release) Successful in 18s
2024-12-20 02:29:20 -05:00
12 changed files with 187 additions and 28 deletions
+1 -1
View File
@@ -35,4 +35,4 @@ jobs:
- name: Publish to NuGet.org
run: |
dotnet nuget push ./artifacts/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }}
dotnet nuget push ./artifacts/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }}
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Open Harbor
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.
@@ -0,0 +1,12 @@
using System.Net.Http.Headers;
namespace OpenHarbor.JwtTokenManager.Abstractions;
public static class HttpClientExtensions
{
public static HttpClient SetJwtAccessToken(this HttpClient httpClient, JwtTokenManagerResult token)
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.TokenType.ToString(), token.AccessToken);
return httpClient;
}
}
@@ -2,7 +2,7 @@ namespace OpenHarbor.JwtTokenManager.Abstractions;
public class JwtTokenManagerResult
{
public required string Token { get; set; }
public required string AccessToken { get; set; }
public required TokenType TokenType { get; set; }
public required DateTime ExpiresAt { get; set; }
public int ExpiresIn { get; set; }
@@ -2,10 +2,28 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>Mathias Beaulieu-Duncan</Authors>
<PackageIconUrl>https://gravatar.com/avatar/9cecda5822fc5d4d2e61ec03da571b3d?size=256</PackageIconUrl>
<Company>Open Harbor</Company>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://git.openharbor.io/Open-Harbor/dotnet-jwt-token-manager</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
<IncludeSymbols>true</IncludeSymbols>
<IncludeSource>true</IncludeSource>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<None Include="..\icon.png" Pack="true" PackagePath="" CopyToOutputDirectory="Always" />
<None Include="..\README.md" Pack="true" PackagePath="" CopyToOutputDirectory="Always" />
</ItemGroup>
</Project>
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;
namespace OpenHarbor.JwtTokenManager;
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true
)]
[JsonSerializable(typeof(JwtTokenResponse))]
public partial class JwtTokenManagerJsonContext : JsonSerializerContext
{
}
@@ -6,5 +6,6 @@ public class JwtTokenManagerOptions
public required string ClientId { get; set; }
public required string ClientSecret { get; set; }
public IEnumerable<string> Scopes { get; set; } = Array.Empty<string>();
public bool IsCredentialsInHeader { get; set; } = false;
}
@@ -1,6 +1,6 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using OpenHarbor.JwtTokenManager.Abstractions;
@@ -10,15 +10,10 @@ namespace OpenHarbor.JwtTokenManager;
public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientFactory httpClientFactory, ILogger<JwtTokenManagerService>? logger, IMemoryCache? memoryCache, JwtTokenManagerCacheOptions cacheOptions)
: IJwtTokenManagerService
{
private readonly HttpClient _httpClient = httpClientFactory.CreateClient();
private readonly TimeSpan _cacheExpirationOffset = TimeSpan.FromSeconds(cacheOptions.ExpirationOffset);
private readonly string _scopes = string.Join(" ", options.Scopes);
private static readonly JsonSerializerOptions SnakeCaseOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true
};
public async Task<JwtTokenManagerResult> GetTokenAsync(CancellationToken cancellationToken = default)
{
if (memoryCache != null)
@@ -30,33 +25,43 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
}
}
var client = httpClientFactory.CreateClient();
var formContent = new FormUrlEncodedContent(new[]
var formContentKeyValues = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", options.ClientId),
new KeyValuePair<string, string>("client_secret", options.ClientSecret),
new KeyValuePair<string, string>("scopes", string.Join(" ", options.Scopes))
});
new ("grant_type", "client_credentials"),
new ("scopes", string.Join(" ", _scopes))
};
if (options.IsCredentialsInHeader)
{
formContentKeyValues.AddRange([
new KeyValuePair<string, string>("client_id", options.ClientId),
new KeyValuePair<string, string>("client_secret", options.ClientSecret)
]);
}
var formContent = new FormUrlEncodedContent(formContentKeyValues);
var request = new HttpRequestMessage(HttpMethod.Post, options.TokenEndpoint)
{
Content = formContent
};
if (false == options.IsCredentialsInHeader)
{
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{options.ClientId}:{options.ClientSecret}"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
}
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.SendAsync(request, cancellationToken);
var response = await _httpClient.SendAsync(request, cancellationToken);
var t = await response.Content.ReadAsStringAsync(cancellationToken);
response.EnsureSuccessStatusCode();
var tokenResponse = await response.Content.ReadFromJsonAsync<JwtTokenResponse>(SnakeCaseOptions, cancellationToken);
var tokenResponse = await response.Content.ReadFromJsonAsync(JwtTokenManagerJsonContext.Default.JwtTokenResponse, cancellationToken);
if (tokenResponse == null)
{
throw new InvalidOperationException("Failed to deserialize the response content.");
throw new InvalidOperationException("Failed to deserialize the token response content.");
}
var parsedResult = Enum.TryParse<TokenType>(tokenResponse.TokenType, out var tokenType);
@@ -71,7 +76,7 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
var expiresAt = now.Add(expiration);
var result = new JwtTokenManagerResult
{
Token = tokenResponse.AccessToken,
AccessToken = tokenResponse.AccessToken,
TokenType = tokenType,
ExpiresAt = expiresAt,
ExpiresIn = tokenResponse.ExpiresIn,
@@ -2,12 +2,32 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>Mathias Beaulieu-Duncan</Authors>
<PackageIconUrl>https://gravatar.com/avatar/9cecda5822fc5d4d2e61ec03da571b3d?size=256</PackageIconUrl>
<Company>Open Harbor</Company>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://git.openharbor.io/Open-Harbor/dotnet-jwt-token-manager</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
<IncludeSymbols>true</IncludeSymbols>
<IncludeSource>true</IncludeSource>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<EnableJsonSourceGeneration>true</EnableJsonSourceGeneration>
</PropertyGroup>
<ItemGroup>
<None Include="..\icon.png" Pack="true" PackagePath="" CopyToOutputDirectory="Always" />
<None Include="..\README.md" Pack="true" PackagePath="" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -9,6 +10,7 @@ namespace OpenHarbor.JwtTokenManager;
public static class ServiceCollectionExtensions
{
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
public static IServiceCollection AddJwtTokenManager(
this IServiceCollection services,
IConfiguration configuration,
@@ -19,8 +21,7 @@ public static class ServiceCollectionExtensions
throw new ArgumentNullException(nameof(configuration));
if (string.IsNullOrWhiteSpace(sectionName))
throw new ArgumentException("Section name must be provided.", nameof(sectionName));
// Configure JwtTokenManagerOptions from the section
services.Configure<JwtTokenManagerOptions>(configuration.GetSection(sectionName));
// Apply the builder options
@@ -30,7 +31,7 @@ public static class ServiceCollectionExtensions
// Register the service
services.AddSingleton<IJwtTokenManagerService>(provider =>
{
var optionsMonitor = provider.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<JwtTokenManagerOptions>>();
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>();
var options = optionsMonitor.Get(Options.DefaultName);
// Apply additional configuration
+69
View File
@@ -0,0 +1,69 @@
# 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.
| Package Name | NuGet | NuGet Install |
|-----------------------------------------| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |----------------------------------------------------------------------------:|
| OpenHarbor.JwtTokenManager | [![NuGet](https://img.shields.io/nuget/v/OpenHarbor.JwtTokenManager.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager/) | ```PM> Install-Package OpenHarbor.JwtTokenManager``` |
| OpenHarbor.JwtTokenManager.Abstractions | [![NuGet](https://img.shields.io/nuget/v/OpenHarbor.JwtTokenManager.Abstractions.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager.Abstractions/) | ```PM> Install-Package OpenHarbor.JwtTokenManager.Abstractions``` |
# How to use
> appsettings.json
```json
{
"JwtTokenManager": {
"TokenEndpoint": "",
"ClientId": "",
"ClientSecret": "",
"Scopes": []
}
}
```
> Program.cs
```csharp
builder.Services.AddHttpClient();
builder.Services.AddMemoryCache(); // use the IMemoryCache provider you want
builder.Services.AddJwtTokenManager(builder.Configuration, "JwtTokenManager");
```
> Program.cs with deeper configurations
```csharp
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
```csharp
public class MyService(HttpClient httpClient, IJwtTokenManagerService jwtTokenManagerService)
{
public async Task DoActionAsync(CancellationToken cancellationToken) {
var tokenResult = await jwtTokenManagerService.GetTokenAsync(cancellationToken);
httpClient.SetJwtAccessToken(tokenResult);
...
}
}
```
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB