Compare commits
2 Commits
4e9119c8c7
...
9df5750f1c
Author | SHA1 | Date | |
---|---|---|---|
9df5750f1c | |||
d1e217329a |
21
LICENSE
Normal file
21
LICENSE
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,10 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Authors>Mathias Beaulieu-Duncan</Authors>
|
<Authors>Mathias Beaulieu-Duncan</Authors>
|
||||||
<PackageIconUrl>https://gravatar.com/avatar/9cecda5822fc5d4d2e61ec03da571b3d?size=256</PackageIconUrl>
|
<PackageIconUrl>https://gravatar.com/avatar/9cecda5822fc5d4d2e61ec03da571b3d?size=256</PackageIconUrl>
|
||||||
|
<RepositoryUrl>https://git.openharbor.io/Open-Harbor/dotnet-jwt-token-manager</RepositoryUrl>
|
||||||
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||||
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -10,6 +10,7 @@ namespace OpenHarbor.JwtTokenManager;
|
|||||||
public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientFactory httpClientFactory, ILogger<JwtTokenManagerService>? logger, IMemoryCache? memoryCache, JwtTokenManagerCacheOptions cacheOptions)
|
public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientFactory httpClientFactory, ILogger<JwtTokenManagerService>? logger, IMemoryCache? memoryCache, JwtTokenManagerCacheOptions cacheOptions)
|
||||||
: IJwtTokenManagerService
|
: IJwtTokenManagerService
|
||||||
{
|
{
|
||||||
|
private readonly HttpClient _httpClient = httpClientFactory.CreateClient();
|
||||||
private readonly TimeSpan _cacheExpirationOffset = TimeSpan.FromSeconds(cacheOptions.ExpirationOffset);
|
private readonly TimeSpan _cacheExpirationOffset = TimeSpan.FromSeconds(cacheOptions.ExpirationOffset);
|
||||||
private readonly string _scopes = string.Join(" ", options.Scopes);
|
private readonly string _scopes = string.Join(" ", options.Scopes);
|
||||||
|
|
||||||
@ -29,34 +30,30 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
|
|||||||
return cachedToken;
|
return cachedToken;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var client = httpClientFactory.CreateClient();
|
var formContent = new FormUrlEncodedContent([
|
||||||
|
|
||||||
var formContent = new FormUrlEncodedContent(new[]
|
|
||||||
{
|
|
||||||
new KeyValuePair<string, string>("grant_type", "client_credentials"),
|
new KeyValuePair<string, string>("grant_type", "client_credentials"),
|
||||||
new KeyValuePair<string, string>("client_id", options.ClientId),
|
new KeyValuePair<string, string>("client_id", options.ClientId),
|
||||||
new KeyValuePair<string, string>("client_secret", options.ClientSecret),
|
new KeyValuePair<string, string>("client_secret", options.ClientSecret),
|
||||||
new KeyValuePair<string, string>("scopes", string.Join(" ", options.Scopes))
|
new KeyValuePair<string, string>("scopes", string.Join(" ", _scopes))
|
||||||
});
|
]);
|
||||||
|
|
||||||
var request = new HttpRequestMessage(HttpMethod.Post, options.TokenEndpoint)
|
var request = new HttpRequestMessage(HttpMethod.Post, options.TokenEndpoint)
|
||||||
{
|
{
|
||||||
Content = formContent
|
Content = formContent
|
||||||
};
|
};
|
||||||
|
|
||||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
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();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
var tokenResponse = await response.Content.ReadFromJsonAsync<JwtTokenResponse>(SnakeCaseOptions, cancellationToken);
|
var tokenResponse = await response.Content.ReadFromJsonAsync<JwtTokenResponse>(SnakeCaseOptions, cancellationToken);
|
||||||
|
|
||||||
if (tokenResponse == null)
|
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);
|
var parsedResult = Enum.TryParse<TokenType>(tokenResponse.TokenType, out var tokenType);
|
||||||
|
@ -6,6 +6,10 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Authors>Mathias Beaulieu-Duncan</Authors>
|
<Authors>Mathias Beaulieu-Duncan</Authors>
|
||||||
<PackageIconUrl>https://gravatar.com/avatar/9cecda5822fc5d4d2e61ec03da571b3d?size=256</PackageIconUrl>
|
<PackageIconUrl>https://gravatar.com/avatar/9cecda5822fc5d4d2e61ec03da571b3d?size=256</PackageIconUrl>
|
||||||
|
<RepositoryUrl>https://git.openharbor.io/Open-Harbor/dotnet-jwt-token-manager</RepositoryUrl>
|
||||||
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||||
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
69
readme.md
Normal file
69
readme.md
Normal 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.
|
||||||
|
|
||||||
|
| 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``` |
|
||||||
|
|
||||||
|
# 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);
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user