Skip to content

Commit

Permalink
v1.2.0 (#4)
Browse files Browse the repository at this point in the history
* Add expiry setting and increase timeout

* Rename

* Update README.md

* Bump version
  • Loading branch information
Hawxy authored Nov 27, 2020
1 parent dea2557 commit 83a2f72
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Both a .NET Generic Host and ASP.NET Core example are available in the [samples]

### Internal Cache

The `Auth0TokenCache` will cache a token for a given audience until 5 minutes before expiry.
The `Auth0TokenCache` will cache a token for a given audience until 30 minutes before expiry. You can increase or decrease this by setting the `TokenExpiryBuffer` on the `Auth0Configuration`.

In some situations you might want to request an access token from Auth0 manually. You can achieve this by injecting `IAuth0TokenCache` into a class and calling `GetTokenAsync` with the audience of the API you're requesting the token for.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net5</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Authors>Hawxy</Authors>
<Description>Dependency Injection, HttpClientFactory &amp; ASP.NET Core extensions for Auth0.NET</Description>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand Down
8 changes: 7 additions & 1 deletion src/Auth0Net.DependencyInjection/Cache/Auth0Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Auth0Net.DependencyInjection.Cache
using System;

namespace Auth0Net.DependencyInjection.Cache
{
/// <summary>
/// Configuration for the Auth0 Clients and Auth0 Token Cache.
Expand All @@ -17,5 +19,9 @@ public class Auth0Configuration
/// The Client Secret of the Auth0 Machine-to-Machine application.
/// </summary>
public string? ClientSecret { get; set; }
/// <summary>
/// The time before access token expiry that the token should be renewed. The default is 30 minutes.
/// </summary>
public TimeSpan TokenExpiryBuffer { get; set; } = TimeSpan.FromMinutes(30);
}
}
2 changes: 1 addition & 1 deletion src/Auth0Net.DependencyInjection/Cache/Auth0TokenCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<string> GetTokenAsync(string audience)

var response = await _client.GetTokenAsync(tokenRequest);

e.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(response.ExpiresIn).Subtract(TimeSpan.FromMinutes(5));
e.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(response.ExpiresIn).Subtract(_config.TokenExpiryBuffer);
return response.AccessToken;
});
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Auth0Net.DependencyInjection.Tests/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ public void AddAuth0AuthenticationClient_Resolves_AuthenticationClient()
var domain = "test.au.auth0.com";
var clientId = "fake-id";
var clientSecret = "fake-secret";
var renewal = TimeSpan.FromMinutes(60);

var services = new ServiceCollection().AddAuth0AuthenticationClient(x =>
{
x.Domain = domain;
x.ClientId = clientId;
x.ClientSecret = clientSecret;
x.TokenExpiryBuffer = renewal;
}).Services;

var serviceDescriptor = services.FirstOrDefault(x => x.ServiceType == typeof(AuthenticationApiClient));
Expand All @@ -121,6 +123,7 @@ public void AddAuth0AuthenticationClient_Resolves_AuthenticationClient()
Assert.Equal(domain, configuration.Value.Domain);
Assert.Equal(clientId, configuration.Value.ClientId);
Assert.Equal(clientSecret, configuration.Value.ClientSecret);
Assert.Equal(renewal, configuration.Value.TokenExpiryBuffer);
}

[Fact]
Expand Down

0 comments on commit 83a2f72

Please sign in to comment.