| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Microsoft.AspNetCore.Components.Authorization; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// User information retrieved from Auth0 |
| | | 8 | | /// </summary> |
| | | 9 | | public class UserInfo |
| | | 10 | | { |
| | 0 | 11 | | public string Auth0UserId { get; set; } = string.Empty; |
| | 0 | 12 | | public string Email { get; set; } = string.Empty; |
| | 0 | 13 | | public string DisplayName { get; set; } = string.Empty; |
| | 0 | 14 | | public string? AvatarUrl { get; set; } |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Implementation of authentication service using ASP.NET Core authentication |
| | | 19 | | /// </summary> |
| | | 20 | | public class AuthService : IAuthService |
| | | 21 | | { |
| | | 22 | | private readonly AuthenticationStateProvider _authenticationStateProvider; |
| | | 23 | | private UserInfo? _cachedUser; |
| | | 24 | | |
| | | 25 | | public AuthService(AuthenticationStateProvider authenticationStateProvider) |
| | | 26 | | { |
| | | 27 | | _authenticationStateProvider = authenticationStateProvider; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<UserInfo?> GetCurrentUserAsync() |
| | | 31 | | { |
| | | 32 | | if (_cachedUser != null) |
| | | 33 | | { |
| | | 34 | | return _cachedUser; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | var authState = await _authenticationStateProvider.GetAuthenticationStateAsync(); |
| | | 38 | | var user = authState.User; |
| | | 39 | | |
| | | 40 | | if (!user.Identity?.IsAuthenticated ?? true) |
| | | 41 | | { |
| | | 42 | | return null; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | const string customNamespace = "https://chronicis.app"; |
| | | 46 | | |
| | | 47 | | // Extract claims from Auth0 token |
| | | 48 | | var auth0UserId = user.FindFirst(ClaimTypes.NameIdentifier)?.Value |
| | | 49 | | ?? user.FindFirst("sub")?.Value |
| | | 50 | | ?? ""; |
| | | 51 | | |
| | | 52 | | var email = user.FindFirst($"{customNamespace}/email")?.Value |
| | | 53 | | ?? user.FindFirst(ClaimTypes.Email)?.Value |
| | | 54 | | ?? user.FindFirst("email")?.Value |
| | | 55 | | ?? ""; |
| | | 56 | | |
| | | 57 | | var displayName = user.FindFirst($"{customNamespace}/name")?.Value |
| | | 58 | | ?? user.FindFirst(ClaimTypes.Name)?.Value |
| | | 59 | | ?? user.FindFirst("name")?.Value |
| | | 60 | | ?? user.FindFirst("preferred_username")?.Value |
| | | 61 | | ?? "Unknown User"; |
| | | 62 | | |
| | | 63 | | var avatarUrl = user.FindFirst($"{customNamespace}/picture")?.Value |
| | | 64 | | ?? user.FindFirst("picture")?.Value; |
| | | 65 | | |
| | | 66 | | _cachedUser = new UserInfo |
| | | 67 | | { |
| | | 68 | | Auth0UserId = auth0UserId, |
| | | 69 | | Email = email, |
| | | 70 | | DisplayName = displayName, |
| | | 71 | | AvatarUrl = avatarUrl |
| | | 72 | | }; |
| | | 73 | | |
| | | 74 | | return _cachedUser; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public async Task<bool> IsAuthenticatedAsync() |
| | | 78 | | { |
| | | 79 | | var authState = await _authenticationStateProvider.GetAuthenticationStateAsync(); |
| | | 80 | | return authState.User.Identity?.IsAuthenticated ?? false; |
| | | 81 | | } |
| | | 82 | | } |