Compare commits

...

8 Commits
8.1.3 ... main

Author SHA1 Message Date
560f562205
fix iscredentialsinheader reverse condition
All checks were successful
Publish NuGets / build (release) Successful in 17s
2025-03-17 10:35:18 -04:00
61f7c0f05e
added credentials in header support
All checks were successful
Publish NuGets / build (release) Successful in 25s
2025-03-17 09:46:54 -04:00
20518b8f6a
update readme 2024-12-22 23:13:37 -05:00
8249714c6b
fix git url -.-
All checks were successful
Publish NuGets / build (release) Successful in 18s
2024-12-22 13:44:10 -05:00
7e1e5c76b7
remove extra layer of publishing 2024-12-22 13:40:32 -05:00
6fee2c5c9a
fix symbols, I think...
Some checks failed
Publish NuGets / build (release) Failing after 19s
2024-12-22 13:38:47 -05:00
0f1f900055
fix options binding
Some checks failed
Publish NuGets / build (release) Failing after 18s
2024-12-22 13:10:44 -05:00
892101a84d
prepare AoT support 2024-12-22 13:03:54 -05:00
8 changed files with 60 additions and 27 deletions

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 }}

View File

@ -2,19 +2,23 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>Mathias Beaulieu-Duncan</Authors>
<Company>Open Harbor</Company>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://git.openharbor.io/Open-Harbor/dotnet-cqrs</RepositoryUrl>
<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>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>

View File

@ -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
{
}

View File

@ -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;
}

View File

@ -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;
@ -14,12 +14,6 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
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,26 +24,41 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
return cachedToken;
}
}
var formContent = new FormUrlEncodedContent([
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(" ", _scopes))
]);
var formContentKeyValues = new List<KeyValuePair<string, string>>()
{
new ("grant_type", "client_credentials"),
new ("scopes", string.Join(" ", _scopes))
};
if (false == 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 (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 _httpClient.SendAsync(request, 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)
{

View File

@ -2,19 +2,25 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>Mathias Beaulieu-Duncan</Authors>
<Company>Open Harbor</Company>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://git.openharbor.io/Open-Harbor/dotnet-cqrs</RepositoryUrl>
<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>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<EnableJsonSourceGeneration>true</EnableJsonSourceGeneration>
</PropertyGroup>
<ItemGroup>

View File

@ -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

View File

@ -4,10 +4,10 @@
> Install nuget package to your awesome project.
| Full Version | NuGet | NuGet Install |
|-----------------------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |----------------------------------------------------------------------------:|
| OpenHarbor.JwtTokenManager | <a href="https://www.nuget.org/packages/OpenHarbor.JwtTokenManager" target="_blank">[![NuGet](https://img.shields.io/nuget/v/OpenHarbor.JwtTokenManager.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager/)</a> | ```PM> Install-Package OpenHarbor.JwtTokenManager``` |
| OpenHarbor.JwtTokenManager.Abstractions | <a href="https://www.nuget.org/packages/OpenHarbor.JwtTokenManager.Abstractions" target="_blank">[![NuGet](https://img.shields.io/nuget/v/OpenHarbor.JwtTokenManager.Abstractions.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager.Abstractions/)</a> | ```PM> Install-Package OpenHarbor.JwtTokenManager.Abstractions``` |
| 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