This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and test the blazor wasm auth state provider
- Loading branch information
1 parent
2bce28d
commit 562c6c3
Showing
11 changed files
with
434 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Duende Software. All rights reserved. | ||
// See LICENSE in the project root for license information. | ||
|
||
using System.Net.Http.Json; | ||
using System.Security.Claims; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Duende.Bff.Blazor.Client.Internals; | ||
|
||
internal class GetUserService : IGetUserService | ||
{ | ||
private readonly HttpClient _client; | ||
private readonly IPersistentUserService _persistentUserService; | ||
private readonly TimeProvider _timeProvider; | ||
private readonly BffBlazorOptions _options; | ||
private readonly ILogger<GetUserService> _logger; | ||
|
||
private DateTimeOffset _userLastCheck = DateTimeOffset.MinValue; | ||
private ClaimsPrincipal _cachedUser = new(new ClaimsIdentity()); | ||
|
||
public GetUserService( | ||
IHttpClientFactory clientFactory, | ||
IPersistentUserService persistentUserService, | ||
TimeProvider timeProvider, | ||
IOptions<BffBlazorOptions> options, | ||
ILogger<GetUserService> logger) | ||
{ | ||
_client = clientFactory.CreateClient(BffClientAuthenticationStateProvider.HttpClientName); | ||
_persistentUserService = persistentUserService; | ||
_timeProvider = timeProvider; | ||
_options = options.Value; | ||
_logger = logger; | ||
} | ||
|
||
public void InitializeCache() | ||
{ | ||
_cachedUser = _persistentUserService.GetPersistedUser(); | ||
if (_cachedUser.Identity?.IsAuthenticated == true) | ||
{ | ||
_userLastCheck = _timeProvider.GetUtcNow(); | ||
} | ||
} | ||
|
||
public async ValueTask<ClaimsPrincipal> GetUserAsync(bool useCache = true) | ||
{ | ||
var now = _timeProvider.GetUtcNow(); | ||
if (useCache && now < _userLastCheck.AddMilliseconds(_options.StateProviderPollingDelay)) | ||
{ | ||
_logger.LogDebug("Taking user from cache"); | ||
return _cachedUser; | ||
} | ||
|
||
_logger.LogDebug("Fetching user"); | ||
_cachedUser = await FetchUser(); | ||
_userLastCheck = now; | ||
|
||
return _cachedUser; | ||
} | ||
|
||
// TODO - Consider using ClaimLite instead here | ||
record ClaimRecord(string Type, object Value); | ||
|
||
internal async Task<ClaimsPrincipal> FetchUser() | ||
{ | ||
try | ||
{ | ||
_logger.LogInformation("Fetching user information."); | ||
var claims = await _client.GetFromJsonAsync<List<ClaimRecord>>("bff/user?slide=false"); | ||
|
||
var identity = new ClaimsIdentity( | ||
nameof(BffClientAuthenticationStateProvider), | ||
"name", | ||
"role"); | ||
|
||
if (claims != null) | ||
{ | ||
foreach (var claim in claims) | ||
{ | ||
identity.AddClaim(new Claim(claim.Type, claim.Value.ToString() ?? "no value")); | ||
} | ||
} | ||
|
||
return new ClaimsPrincipal(identity); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogWarning(ex, "Fetching user failed."); | ||
} | ||
|
||
return new ClaimsPrincipal(new ClaimsIdentity()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Duende Software. All rights reserved. | ||
// See LICENSE in the project root for license information. | ||
|
||
using System.Security.Claims; | ||
|
||
namespace Duende.Bff.Blazor.Client.Internals; | ||
|
||
/// <summary> | ||
/// Internal service for retrieval of user info in the authentication state provider. | ||
/// </summary> | ||
public interface IGetUserService | ||
{ | ||
/// <summary> | ||
/// Gets the user. | ||
/// </summary> | ||
ValueTask<ClaimsPrincipal> GetUserAsync(bool useCache = true); | ||
|
||
/// <summary> | ||
/// Initializes the cache. | ||
/// </summary> | ||
void InitializeCache(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Duende.Bff.Blazor.Client/Internals/IPersistentUserService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Duende Software. All rights reserved. | ||
// See LICENSE in the project root for license information. | ||
|
||
using System.Security.Claims; | ||
|
||
namespace Duende.Bff.Blazor.Client.Internals; | ||
|
||
/// <summary> | ||
/// A service for interacting with the user persisted in PersistentComponentState in blazor. | ||
/// </summary> | ||
public interface IPersistentUserService | ||
{ | ||
/// <summary> | ||
/// Retrieves a ClaimsPrincipal from PersistentComponentState. If there is no persisted user, returns an anonymous | ||
/// user. | ||
/// </summary> | ||
/// <returns></returns> | ||
ClaimsPrincipal GetPersistedUser(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.