Skip to content

Commit 52506f2

Browse files
committed
Add configurable expiration intervals
1 parent c8991da commit 52506f2

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

examples/Umbraco.AuthorizedServices.TestSite/appsettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@
432432
"RequestIdentityPath": "",
433433
"RequestTokenPath": "",
434434
"RequestTokenFormat": "",
435-
"ApiKey: "[api_key]",
435+
"ApiKey": "",
436436
"ApiKeyProvision": {
437437
"Method": "QueryString",
438438
"Key": "key"
@@ -493,7 +493,9 @@
493493
"TokenGrantType": "ig_exchange_token",
494494
"RequestRefreshTokenPath": "/refresh_access_token",
495495
"RefreshTokenGrantType": "ig_refresh_token"
496-
}
496+
},
497+
"AccessTokenExpirationInterval": "00.00:00:40",
498+
"ExchangeTokenExpirationInterval": "25.00:00:00"
497499
}
498500
}
499501
}

src/Umbraco.AuthorizedServices/Configuration/AuthorizedServiceSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ public class ServiceDetail : ServiceSummary
253253
/// </summary>
254254
public string? SampleRequest { get; set; }
255255

256+
/// <summary>
257+
/// Gets or sets the time interval for expiration of access tokens.
258+
/// </summary>
259+
public TimeSpan AccessTokenExpirationInterval { get; set; } = TimeSpan.FromSeconds(30);
260+
261+
/// <summary>
262+
/// Gets or sets the time interval for expiration of exchange tokens.
263+
/// </summary>
264+
public TimeSpan ExchangeTokenExpirationInterval { get; set; } = TimeSpan.FromDays(30);
265+
256266
internal string GetTokenHost() => string.IsNullOrWhiteSpace(TokenHost)
257267
? IdentityHost
258268
: TokenHost;

src/Umbraco.AuthorizedServices/Services/Implement/AuthorizedServiceCaller.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ public async Task<string> SendRequestRawAsync<TRequest>(string serviceAlias, str
144144

145145
private async Task<Token> EnsureAccessToken(string serviceAlias, Token token)
146146
{
147-
if (token.WillBeExpiredAfter(TimeSpan.FromSeconds(30)))
147+
ServiceDetail serviceDetail = GetServiceDetail(serviceAlias);
148+
if (token.WillBeExpiredAfter(serviceDetail.AccessTokenExpirationInterval))
148149
{
149150
if (string.IsNullOrEmpty(token.RefreshToken))
150151
{
@@ -184,7 +185,8 @@ private async Task<Token> EnsureAccessToken(string serviceAlias, Token token)
184185

185186
private async Task<Token> EnsureExchangeAccessToken(string serviceAlias, Token token)
186187
{
187-
if (token.WillBeExpiredAfter(TimeSpan.FromDays(30)))
188+
ServiceDetail serviceDetail = GetServiceDetail(serviceAlias);
189+
if (token.WillBeExpiredAfter(serviceDetail.ExchangeTokenExpirationInterval))
188190
{
189191
return await RefreshExchangeAccessToken(serviceAlias, token.AccessToken)
190192
?? throw new AuthorizedServiceException($"Cannot request service '{serviceAlias}' as the access token will expire and it's not been possible to exchange it for a new access token.");

src/Umbraco.AuthorizedServices/appsettings-schema.Umbraco.AuthorizedServices.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@
147147
"SampleRequest": {
148148
"type": "string",
149149
"description": "Gets or sets the path to a GET request used as a sample for verifying the service in the backoffice."
150+
},
151+
"AccessTokenExpirationInterval": {
152+
"type": "string",
153+
"description": "Gets or sets the time interval for expiration of access tokens."
154+
},
155+
"ExchangeTokenExpirationInterval": {
156+
"type": "string",
157+
"description": "Gets or sets the time interval for expiration of exchange tokens."
150158
}
151159
}
152160
},

0 commit comments

Comments
 (0)