Skip to content

Commit

Permalink
Correction for realm and user service
Browse files Browse the repository at this point in the history
  • Loading branch information
fdonnet committed Oct 29, 2024
1 parent 3758521 commit a5d1876
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
39 changes: 21 additions & 18 deletions src/Ubik.Accounting.WebApp/Security/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,31 @@ public async Task<string> GetTokenAsync()
if (token == null)
return string.Empty;

var response = await new HttpClient().RequestRefreshTokenAsync(new RefreshTokenRequest
if (token.ExpiresUtc < DateTimeOffset.UtcNow.AddSeconds(10) && token.ExpiresRefreshUtc > DateTimeOffset.UtcNow.AddSeconds(10))
{
Address = authOptions.Value.TokenUrl,
ClientId = authOptions.Value.ClientId,
ClientSecret = authOptions.Value.ClientSecret,
RefreshToken = token.RefreshToken,
GrantType = "refresh_token",
});

if (!response.IsError)
{
await cache.SetUserTokenAsync(new TokenCacheEntry
var response = await new HttpClient().RequestRefreshTokenAsync(new RefreshTokenRequest
{
UserId = userEmail,
RefreshToken = response.RefreshToken!,
AccessToken = response.AccessToken!,
ExpiresUtc = new JwtSecurityToken(response.AccessToken).ValidTo,
ExpiresRefreshUtc = DateTimeOffset.UtcNow.AddMinutes(authOptions.Value.RefreshTokenExpTimeInMinutes)
Address = authOptions.Value.TokenUrl,
ClientId = authOptions.Value.ClientId,
ClientSecret = authOptions.Value.ClientSecret,
RefreshToken = token.RefreshToken,
GrantType = "refresh_token",
});

if (!response.IsError)
{
await cache.SetUserTokenAsync(new TokenCacheEntry
{
UserId = userEmail,
RefreshToken = response.RefreshToken!,
AccessToken = response.AccessToken!,
ExpiresUtc = new JwtSecurityToken(response.AccessToken).ValidTo,
ExpiresRefreshUtc = DateTimeOffset.UtcNow.AddMinutes(authOptions.Value.RefreshTokenExpTimeInMinutes)
});
}
else
throw new InvalidOperationException("Error refreshing token");
}
else
throw new InvalidOperationException("Error refreshing token");

return token.AccessToken;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Ubik.Api.Tests.Integration/import/ubik-realm.json
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@
"https://localhost:7289/*",
"http://localhost:5002/*",
"http://ubik-proxy/*",
"https://ubik-webapp/*",
"http://ubik-webapp/*",
"https://localhost:7249/*"
],
Expand All @@ -823,6 +824,7 @@
"https://localhost:7289",
"http://localhost:5002",
"http://ubik-proxy",
"https://ubik-webapp",
"http://ubik-webapp",
"https://localhost:7249"
],
Expand All @@ -841,7 +843,7 @@
"oidc.ciba.grant.enabled": "false",
"client.secret.creation.time": "1728482137",
"backchannel.logout.session.required": "true",
"post.logout.redirect.uris": "https://localhost:7149/*##https://localhost:7249/*##http://localhost:5002/*##http://ubik-proxy/*##http://ubik-webapp/*",
"post.logout.redirect.uris": "https://localhost:7149/*##https://localhost:7249/*##http://localhost:5002/*##http://ubik-proxy/*##https://ubik-webapp/*##http://ubik-webapp/*",
"oauth2.device.authorization.grant.enabled": "false",
"display.on.consent.screen": "false",
"backchannel.logout.revoke.offline.tokens": "false"
Expand Down

0 comments on commit a5d1876

Please sign in to comment.