-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathApplicationAuthenticationStateProvider.cs
More file actions
77 lines (67 loc) · 3.41 KB
/
ApplicationAuthenticationStateProvider.cs
File metadata and controls
77 lines (67 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright © 2024-Present The Synapse Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"),
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using IdentityModel;
using Microsoft.AspNetCore.Components.Authorization;
using Synapse.Api.Client.Services;
using Synapse.Dashboard.Components.DocumentDetailsStateManagement;
using System.Security.Claims;
namespace Synapse.Dashboard.Services;
/// <summary>
/// Represents an <see cref="AuthenticationStateProvider"/> implementation used to determine the current authentication state using Synapse Static Bearer Tokens
/// </summary>
/// <param name="logger">The service used to perform logging</param>
/// <param name="api">The service used to interact with the Synapse API</param>
/// <param name="tokenManager">The service used to manage security tokens</param>
/// <param name="navigationManager">The service used to provides an abstraction for querying and managing URI navigation.</param>
public class ApplicationAuthenticationStateProvider(ILogger<ApplicationAuthenticationStateProvider> logger, ISynapseApiClient api, ISecurityTokenManager tokenManager, NavigationManager navigationManager)
: AuthenticationStateProvider
{
static readonly ClaimsPrincipal Anonymous = new();
/// <summary>
/// Gets the service used to perform logging
/// </summary>
protected ILogger<ApplicationAuthenticationStateProvider> Logger { get; } = logger;
/// <summary>
/// Gets the service used to interact with the Synapse API
/// </summary>
protected ISynapseApiClient Api { get; } = api;
/// <summary>
/// Gets the service used to manage security tokens
/// </summary>
protected ISecurityTokenManager TokenManager { get; } = tokenManager;
/// <inheritdoc/>
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
try
{
var token = await this.TokenManager.GetTokenAsync();
if (string.IsNullOrWhiteSpace(token)) return new AuthenticationState(Anonymous);
var profile = await this.Api.Users.GetUserProfileAsync();
var claims = profile.Claims?.Select(c => new Claim(c.Type, c.Value)).ToList() ?? [];
var identity = new ClaimsIdentity(claims, profile.AuthenticationType, JwtClaimTypes.Name, JwtClaimTypes.Role);
var principal = new ClaimsPrincipal(identity);
return new AuthenticationState(principal);
}
catch (Exception ex)
{
this.Logger.LogError("Unable to get authentication state: {exception}", ex.ToString());
await this.TokenManager.SetTokenAsync("");
navigationManager.NavigateTo("/");
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
}
}
/// <summary>
/// Notifies the application that its authentication state has changed
/// </summary>
public void NotifyNotifyAuthenticationStateChanged() => base.NotifyAuthenticationStateChanged(this.GetAuthenticationStateAsync());
}