initial commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace OpenHarbor.JwtTokenManager;
|
||||
|
||||
public class JwtTokenManagerBuilderOptions
|
||||
{
|
||||
internal Action<JwtTokenManagerCacheOptions>? CacheOptions { get; set; }
|
||||
public Func<IServiceProvider, IMemoryCache>? CacheFactory { get; set; }
|
||||
public Action<JwtTokenManagerOptions>? AdditionalConfiguration { get; set; }
|
||||
|
||||
public JwtTokenManagerBuilderOptions Cache(Func<IServiceProvider, IMemoryCache>? cacheFactory = null)
|
||||
{
|
||||
CacheFactory = cacheFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JwtTokenManagerBuilderOptions Cache(
|
||||
Action<JwtTokenManagerCacheOptions> cacheOptions,
|
||||
Func<IServiceProvider, IMemoryCache>? cacheFactory = null)
|
||||
{
|
||||
CacheOptions = cacheOptions;
|
||||
return Cache(cacheFactory);
|
||||
}
|
||||
|
||||
public JwtTokenManagerBuilderOptions Configuration(
|
||||
Action<JwtTokenManagerOptions> configureOptions)
|
||||
{
|
||||
AdditionalConfiguration = configureOptions;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace OpenHarbor.JwtTokenManager;
|
||||
|
||||
public class JwtTokenManagerCacheOptions
|
||||
{
|
||||
public uint ExpirationOffset { get; set; } = 15;
|
||||
public string CacheKey { get; set; } = "OpenHarborJwtTokenManager.JwtTokenResult";
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace OpenHarbor.JwtTokenManager;
|
||||
|
||||
public class JwtTokenManagerOptions
|
||||
{
|
||||
public required string TokenEndpoint { get; set; }
|
||||
public required string ClientId { get; set; }
|
||||
public required string ClientSecret { get; set; }
|
||||
public IEnumerable<string> Scopes { get; set; } = Array.Empty<string>();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenHarbor.JwtTokenManager.Abstractions;
|
||||
|
||||
namespace OpenHarbor.JwtTokenManager;
|
||||
|
||||
public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientFactory httpClientFactory, ILogger<JwtTokenManagerService>? logger, IMemoryCache? memoryCache, JwtTokenManagerCacheOptions cacheOptions)
|
||||
: IJwtTokenManagerService
|
||||
{
|
||||
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)
|
||||
{
|
||||
var memoryGetValueResult = memoryCache.TryGetValue(cacheOptions.CacheKey, out JwtTokenManagerResult? cachedToken);
|
||||
if (memoryGetValueResult && null != cachedToken)
|
||||
{
|
||||
return cachedToken;
|
||||
}
|
||||
}
|
||||
|
||||
var client = httpClientFactory.CreateClient();
|
||||
|
||||
var formContent = new FormUrlEncodedContent(new[]
|
||||
{
|
||||
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))
|
||||
});
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, options.TokenEndpoint)
|
||||
{
|
||||
Content = formContent
|
||||
};
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
var response = await client.SendAsync(request, cancellationToken);
|
||||
|
||||
var t = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var tokenResponse = await response.Content.ReadFromJsonAsync<JwtTokenResponse>(SnakeCaseOptions, cancellationToken);
|
||||
|
||||
if (tokenResponse == null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to deserialize the response content.");
|
||||
}
|
||||
|
||||
var parsedResult = Enum.TryParse<TokenType>(tokenResponse.TokenType, out var tokenType);
|
||||
|
||||
if (parsedResult == false)
|
||||
{
|
||||
throw new InvalidOperationException($"Unsupported token type: {tokenResponse.TokenType}");
|
||||
}
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var expiration = TimeSpan.FromSeconds(tokenResponse.ExpiresIn);
|
||||
var expiresAt = now.Add(expiration);
|
||||
var result = new JwtTokenManagerResult
|
||||
{
|
||||
Token = tokenResponse.AccessToken,
|
||||
TokenType = tokenType,
|
||||
ExpiresAt = expiresAt,
|
||||
ExpiresIn = tokenResponse.ExpiresIn,
|
||||
};
|
||||
|
||||
if (null != memoryCache && expiration < _cacheExpirationOffset)
|
||||
{
|
||||
logger?.LogWarning("Caching is enable but the token expiration time [{expiration}] is less than the expiration offset [{cacheExpirationOffset}]. Caching is ignored, please validate your authorization server configuration and the {className} cache expiration offset configuration.",
|
||||
expiration, _cacheExpirationOffset, nameof(JwtTokenManagerService));
|
||||
}
|
||||
else
|
||||
memoryCache?.Set(cacheOptions.CacheKey, result, expiration.Subtract(_cacheExpirationOffset));
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace OpenHarbor.JwtTokenManager;
|
||||
|
||||
public class JwtTokenResponse
|
||||
{
|
||||
public required string AccessToken { get; set; }
|
||||
public required string TokenType { get; set; }
|
||||
public int ExpiresIn { get; set; }
|
||||
//public string? RefreshToken { get; set; }
|
||||
//public int? RefreshExpiresIn { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OpenHarbor.JwtTokenManager.Abstractions\OpenHarbor.JwtTokenManager.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,52 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenHarbor.JwtTokenManager.Abstractions;
|
||||
|
||||
namespace OpenHarbor.JwtTokenManager;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddJwtTokenManager(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
string sectionName,
|
||||
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
||||
{
|
||||
if (configuration == null)
|
||||
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
|
||||
var builderOptions = new JwtTokenManagerBuilderOptions();
|
||||
configureBuilderOptions?.Invoke(builderOptions);
|
||||
|
||||
// Register the service
|
||||
services.AddSingleton<IJwtTokenManagerService>(provider =>
|
||||
{
|
||||
var optionsMonitor = provider.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<JwtTokenManagerOptions>>();
|
||||
var options = optionsMonitor.Get(Options.DefaultName);
|
||||
|
||||
// Apply additional configuration
|
||||
builderOptions.AdditionalConfiguration?.Invoke(options);
|
||||
|
||||
// Configure cache options
|
||||
var cacheOptions = new JwtTokenManagerCacheOptions();
|
||||
builderOptions.CacheOptions?.Invoke(cacheOptions);
|
||||
|
||||
var memoryCache = builderOptions.CacheFactory?.Invoke(provider) ?? provider.GetService<IMemoryCache>();
|
||||
var httpClientFactory = provider.GetRequiredService<IHttpClientFactory>();
|
||||
var logger = provider.GetService<ILogger<JwtTokenManagerService>>();
|
||||
|
||||
return new JwtTokenManagerService(options, httpClientFactory, logger, memoryCache, cacheOptions);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user