Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
6dccfa0bca
|
|||
|
560f562205
|
|||
|
61f7c0f05e
|
|||
|
20518b8f6a
|
@@ -6,5 +6,6 @@ public class JwtTokenManagerOptions
|
|||||||
public required string ClientId { get; set; }
|
public required string ClientId { get; set; }
|
||||||
public required string ClientSecret { get; set; }
|
public required string ClientSecret { get; set; }
|
||||||
public IEnumerable<string> Scopes { get; set; } = Array.Empty<string>();
|
public IEnumerable<string> Scopes { get; set; } = Array.Empty<string>();
|
||||||
|
public bool IsCredentialsInHeader { get; set; } = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
|
using System.Text;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using OpenHarbor.JwtTokenManager.Abstractions;
|
using OpenHarbor.JwtTokenManager.Abstractions;
|
||||||
@@ -24,18 +25,33 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var formContent = new FormUrlEncodedContent([
|
var formContentKeyValues = new List<KeyValuePair<string, string>>()
|
||||||
new KeyValuePair<string, string>("grant_type", "client_credentials"),
|
{
|
||||||
new KeyValuePair<string, string>("client_id", options.ClientId),
|
new ("grant_type", "client_credentials"),
|
||||||
new KeyValuePair<string, string>("client_secret", options.ClientSecret),
|
new ("scopes", string.Join(" ", _scopes))
|
||||||
new KeyValuePair<string, string>("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)
|
var request = new HttpRequestMessage(HttpMethod.Post, options.TokenEndpoint)
|
||||||
{
|
{
|
||||||
Content = formContent
|
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"));
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
|
|
||||||
var response = await _httpClient.SendAsync(request, cancellationToken);
|
var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
|||||||
@@ -10,11 +10,14 @@ namespace OpenHarbor.JwtTokenManager;
|
|||||||
|
|
||||||
public static class ServiceCollectionExtensions
|
public static class ServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
|
private static bool _defaultRegistered = false;
|
||||||
|
|
||||||
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
|
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
|
||||||
public static IServiceCollection AddJwtTokenManager(
|
public static IServiceCollection AddJwtTokenManager(
|
||||||
this IServiceCollection services,
|
this IServiceCollection services,
|
||||||
IConfiguration configuration,
|
IConfiguration configuration,
|
||||||
string sectionName,
|
string sectionName,
|
||||||
|
string? name = null,
|
||||||
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
||||||
{
|
{
|
||||||
if (configuration == null)
|
if (configuration == null)
|
||||||
@@ -22,22 +25,20 @@ public static class ServiceCollectionExtensions
|
|||||||
if (string.IsNullOrWhiteSpace(sectionName))
|
if (string.IsNullOrWhiteSpace(sectionName))
|
||||||
throw new ArgumentException("Section name must be provided.", nameof(sectionName));
|
throw new ArgumentException("Section name must be provided.", nameof(sectionName));
|
||||||
|
|
||||||
services.Configure<JwtTokenManagerOptions>(configuration.GetSection(sectionName));
|
name ??= sectionName;
|
||||||
|
|
||||||
|
services.Configure<JwtTokenManagerOptions>(name, configuration.GetSection(sectionName));
|
||||||
|
|
||||||
// Apply the builder options
|
|
||||||
var builderOptions = new JwtTokenManagerBuilderOptions();
|
var builderOptions = new JwtTokenManagerBuilderOptions();
|
||||||
configureBuilderOptions?.Invoke(builderOptions);
|
configureBuilderOptions?.Invoke(builderOptions);
|
||||||
|
|
||||||
// Register the service
|
services.AddKeyedSingleton<IJwtTokenManagerService>(name, (provider, key) =>
|
||||||
services.AddSingleton<IJwtTokenManagerService>(provider =>
|
|
||||||
{
|
{
|
||||||
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>();
|
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>();
|
||||||
var options = optionsMonitor.Get(Options.DefaultName);
|
var options = optionsMonitor.Get(name);
|
||||||
|
|
||||||
// Apply additional configuration
|
|
||||||
builderOptions.AdditionalConfiguration?.Invoke(options);
|
builderOptions.AdditionalConfiguration?.Invoke(options);
|
||||||
|
|
||||||
// Configure cache options
|
|
||||||
var cacheOptions = new JwtTokenManagerCacheOptions();
|
var cacheOptions = new JwtTokenManagerCacheOptions();
|
||||||
builderOptions.CacheOptions?.Invoke(cacheOptions);
|
builderOptions.CacheOptions?.Invoke(cacheOptions);
|
||||||
|
|
||||||
@@ -48,6 +49,24 @@ public static class ServiceCollectionExtensions
|
|||||||
return new JwtTokenManagerService(options, httpClientFactory, logger, memoryCache, cacheOptions);
|
return new JwtTokenManagerService(options, httpClientFactory, logger, memoryCache, cacheOptions);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!_defaultRegistered)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IJwtTokenManagerService>(sp =>
|
||||||
|
sp.GetRequiredKeyedService<IJwtTokenManagerService>(name));
|
||||||
|
|
||||||
|
_defaultRegistered = true;
|
||||||
|
}
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
|
||||||
|
public static IServiceCollection AddJwtTokenManager(
|
||||||
|
this IServiceCollection services,
|
||||||
|
IConfiguration configuration,
|
||||||
|
string sectionName,
|
||||||
|
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
||||||
|
{
|
||||||
|
return AddJwtTokenManager(services, configuration, sectionName, null, configureBuilderOptions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
|
|
||||||
> Install nuget package to your awesome project.
|
> Install nuget package to your awesome project.
|
||||||
|
|
||||||
| Full Version | NuGet | NuGet Install |
|
| Package Name | NuGet | NuGet Install |
|
||||||
|-----------------------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |----------------------------------------------------------------------------:|
|
|-----------------------------------------| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |----------------------------------------------------------------------------:|
|
||||||
| OpenHarbor.JwtTokenManager | <a href="https://www.nuget.org/packages/OpenHarbor.JwtTokenManager" target="_blank">[](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager/)</a> | ```PM> Install-Package OpenHarbor.JwtTokenManager``` |
|
| OpenHarbor.JwtTokenManager | [](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager/) | ```PM> Install-Package OpenHarbor.JwtTokenManager``` |
|
||||||
| OpenHarbor.JwtTokenManager.Abstractions | <a href="https://www.nuget.org/packages/OpenHarbor.JwtTokenManager.Abstractions" target="_blank">[](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager.Abstractions/)</a> | ```PM> Install-Package OpenHarbor.JwtTokenManager.Abstractions``` |
|
| OpenHarbor.JwtTokenManager.Abstractions | [](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager.Abstractions/) | ```PM> Install-Package OpenHarbor.JwtTokenManager.Abstractions``` |
|
||||||
|
|
||||||
# How to use
|
# How to use
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user