< Summary - Syki

Line coverage
64%
Covered lines: 31
Uncovered lines: 17
Coverable lines: 48
Total lines: 116
Line coverage: 64.5%
Branch coverage
16%
Covered branches: 2
Total branches: 12
Branch coverage: 16.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: GuidPattern()100%210%
File 2: AppendSykiJwtCookie(...)100%210%
File 2: AppendJWTCookie(...)100%11100%
File 2: DeleteJWTCookie(...)100%11100%
File 2: NormalizePath(...)100%210%
File 2: GetTargetControllerName(...)16.66%231257.14%
File 2: SignInTwoFactorUserIdSchemeAsync()100%11100%

File(s)

/_/Back/obj/Release/net10.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs

File '/_/Back/obj/Release/net10.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs' does not exist (any more).

/home/runner/work/syki/syki/Back/Extensions/HttpExtensions.cs

#LineLine coverage
 1using Syki.Back.Auth.Schemes;
 2using System.Security.Claims;
 3using System.Text.RegularExpressions;
 4using Microsoft.AspNetCore.Authentication;
 5using Microsoft.AspNetCore.Mvc.Controllers;
 6
 7namespace Syki.Back.Extensions;
 8
 9public static partial class HttpExtensions
 10{
 11    extension(HttpResponse response)
 12    {
 13        public void AppendSykiJwtCookie(string token, AuthSettings settings)
 14        {
 015            response.Cookies.Append(
 016                "syki_jwt",
 017                token,
 018                new CookieOptions
 019                {
 020                    HttpOnly = true,
 021                    SameSite = SameSiteMode.Lax,
 022                    Secure = settings.CookieSecure,
 023                    Domain = settings.CookieDomain
 024                }
 025            );
 026        }
 27
 28        public void AppendJWTCookie(string token, AuthSettings settings)
 29        {
 25230            response.Cookies.Append(
 25231                JwtBearerScheme.Cookie,
 25232                token,
 25233                new CookieOptions
 25234                {
 25235                    Path = "/",
 25236                    HttpOnly = true,
 25237                    Secure = settings.CookieSecure,
 25238                    SameSite = settings.CookieSameSite,
 25239                }
 25240            );
 25241        }
 42
 43        public void DeleteJWTCookie(AuthSettings settings)
 44        {
 2445            response.Cookies.Delete(
 2446                JwtBearerScheme.Cookie,
 2447                new CookieOptions
 2448                {
 2449                    Path = "/",
 2450                    HttpOnly = true,
 2451                    SameSite = settings.CookieSameSite,
 2452                    Secure = settings.CookieSecure,
 2453                }
 2454            );
 2455        }
 56    }
 57
 58    [GeneratedRegex(@"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", RegexOptions.IgnoreCase)]
 59    private static partial Regex GuidPattern();
 60
 061    private static string NormalizePath(string path) => GuidPattern().Replace(path, "{id}");
 62
 63    extension(HttpContext context)
 64    {
 65        public string GetTargetControllerName()
 66        {
 104667            var endpoint = context.GetEndpoint();
 104668            if (endpoint == null) return NormalizePath(context.Request.Path.ToString());
 69
 104670            var action = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
 209271            if (action != null) return $"{action.ControllerName}Controller";
 72
 073            if (endpoint is RouteEndpoint routeEndpoint)
 074                return routeEndpoint.RoutePattern.RawText ?? endpoint.DisplayName ?? NormalizePath(context.Request.Path.
 75
 076            return endpoint.DisplayName ?? NormalizePath(context.Request.Path.ToString());
 77        }
 78
 79        public async Task SignInTwoFactorUserIdSchemeAsync(int userId)
 80        {
 81            // Store user ID in Identity cookie for 2FA verification step
 2082            var identity = new ClaimsIdentity(IdentityConstants.TwoFactorUserIdScheme);
 2083            identity.AddClaim(new Claim(ClaimTypes.Name, userId.ToString()));
 2084            await context!.SignInAsync(IdentityConstants.TwoFactorUserIdScheme, new ClaimsPrincipal(identity));
 2085        }
 86    }
 87}