< Summary

Information
Class: Syki.Front.Auth.SykiAuthStateProvider
Assembly: Front
File(s): /home/runner/work/syki/syki/Front/Auth/SykiAuthStateProvider.cs
Tag: 22_11348620282
Line coverage
82%
Covered lines: 14
Uncovered lines: 3
Coverable lines: 17
Total lines: 48
Line coverage: 82.3%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetAuthenticationStateAsync()50%2.03280%
MarkUserAsAuthenticated()100%11100%
MarkUserAsLoggedOut()100%210%
CreateClaimsPrincipalFromToken(...)100%22100%

File(s)

/home/runner/work/syki/syki/Front/Auth/SykiAuthStateProvider.cs

#LineLine coverage
 1using Microsoft.JSInterop;
 2using System.Security.Claims;
 3using System.IdentityModel.Tokens.Jwt;
 4using Microsoft.AspNetCore.Components.Authorization;
 5
 6namespace Syki.Front.Auth;
 7
 3968public class SykiAuthStateProvider(ILocalStorageService storage) : AuthenticationStateProvider
 9{
 10    public override async Task<AuthenticationState> GetAuthenticationStateAsync()
 11    {
 38312        var jwt = await storage.GetItemAsync("AccessToken");
 13
 38314        if (jwt.IsEmpty())
 15        {
 016            return new(new ClaimsPrincipal(new ClaimsIdentity()));
 17        }
 18
 38319        return new(CreateClaimsPrincipalFromToken(jwt!));
 38320    }
 21
 22    public void MarkUserAsAuthenticated()
 23    {
 38324        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
 38325    }
 26
 27    public void MarkUserAsLoggedOut()
 28    {
 029        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
 030    }
 31
 32    private static ClaimsPrincipal CreateClaimsPrincipalFromToken(string token)
 33    {
 38334        var tokenHandler = new JwtSecurityTokenHandler();
 35
 38336        var identity = new ClaimsIdentity();
 37
 38338        if (tokenHandler.CanReadToken(token))
 39        {
 38340            var jwtSecurityToken = tokenHandler.ReadJwtToken(token);
 38341            identity = new(jwtSecurityToken.Claims, "Bearer");
 42
 38343            identity.AddClaim(new Claim(ClaimTypes.Role, identity.FindFirst("role")!.Value));
 44        }
 45
 38346        return new(identity);
 47    }
 48}