Skip to content

Commit 2ab66d5

Browse files
committed
Make expires_in account for custom expiration
1 parent 4ee0e9d commit 2ab66d5

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/Security/Authentication/BearerToken/src/BearerTokenHandler.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,26 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop
7373

7474
protected override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties? properties)
7575
{
76+
long expiresInTotalSeconds;
77+
var utcNow = TimeProvider.GetUtcNow();
78+
7679
properties ??= new();
77-
properties.ExpiresUtc ??= TimeProvider.GetUtcNow() + Options.BearerTokenExpiration;
80+
81+
if (properties.ExpiresUtc is null)
82+
{
83+
properties.ExpiresUtc ??= utcNow + Options.BearerTokenExpiration;
84+
expiresInTotalSeconds = (long)Options.BearerTokenExpiration.TotalSeconds;
85+
}
86+
else
87+
{
88+
expiresInTotalSeconds = (long)(properties.ExpiresUtc.Value - utcNow).TotalSeconds;
89+
}
7890

7991
var ticket = new AuthenticationTicket(user, properties, Scheme.Name);
8092
var accessTokenResponse = new AccessTokenResponse
8193
{
8294
AccessToken = BearerTokenProtector.Protect(ticket),
83-
ExpiresInTotalSeconds = Options.BearerTokenExpiration.TotalSeconds,
95+
ExpiresInTotalSeconds = expiresInTotalSeconds,
8496
};
8597

8698
return Context.Response.WriteAsJsonAsync(accessTokenResponse, BearerTokenJsonSerializerContext.Default.AccessTokenResponse);

src/Shared/BearerToken/DTO/AccessTokenResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ internal sealed class AccessTokenResponse
1414
public required string AccessToken { get; init; }
1515

1616
[JsonPropertyName("expires_in")]
17-
public required double ExpiresInTotalSeconds { get; init; }
17+
public required long ExpiresInTotalSeconds { get; init; }
1818
}

src/Tools/Tools.slnf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"src\\Middleware\\StaticFiles\\src\\Microsoft.AspNetCore.StaticFiles.csproj",
7676
"src\\Middleware\\WebSockets\\src\\Microsoft.AspNetCore.WebSockets.csproj",
7777
"src\\ObjectPool\\src\\Microsoft.Extensions.ObjectPool.csproj",
78+
"src\\Security\\Authentication\\BearerToken\\src\\Microsoft.AspNetCore.Authentication.BearerToken.csproj",
7879
"src\\Security\\Authentication\\Certificate\\src\\Microsoft.AspNetCore.Authentication.Certificate.csproj",
7980
"src\\Security\\Authentication\\Cookies\\src\\Microsoft.AspNetCore.Authentication.Cookies.csproj",
8081
"src\\Security\\Authentication\\Core\\src\\Microsoft.AspNetCore.Authentication.csproj",

0 commit comments

Comments
 (0)