< Summary - Syki

Line coverage
86%
Covered lines: 33
Uncovered lines: 5
Coverable lines: 38
Total lines: 111
Line coverage: 86.8%
Branch coverage
33%
Covered branches: 6
Total branches: 18
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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 AppendJWTCookie(string token, AuthSettings settings)
 14        {
 28215            response.Cookies.Append(
 28216                JwtBearerScheme.Cookie,
 28217                token,
 28218                new CookieOptions
 28219                {
 28220                    Path = "/",
 28221                    HttpOnly = true,
 28222                    Secure = settings.CookieSecure,
 28223                    SameSite = settings.CookieSameSite,
 28224                }
 28225            );
 28226        }
 27
 28        public void DeleteJWTCookie(AuthSettings settings)
 29        {
 2430            response.Cookies.Delete(
 2431                JwtBearerScheme.Cookie,
 2432                new CookieOptions
 2433                {
 2434                    Path = "/",
 2435                    HttpOnly = true,
 2436                    SameSite = settings.CookieSameSite,
 2437                    Secure = settings.CookieSecure,
 2438                }
 2439            );
 2440        }
 41    }
 42
 43    [GeneratedRegex(@"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", RegexOptions.IgnoreCase)]
 44    private static partial Regex GuidPattern();
 45
 046    private static string NormalizePath(string path) => GuidPattern().Replace(path, "{id}");
 47
 48    extension(HttpContext context)
 49    {
 50        public string GetTargetControllerName()
 51        {
 136052            var endpoint = context.GetEndpoint();
 136053            if (endpoint == null) return NormalizePath(context.Request.Path.ToString());
 54
 136055            var action = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
 271856            if (action != null) return $"{action.ControllerName}Controller";
 57
 258            if (endpoint is RouteEndpoint routeEndpoint)
 259                return routeEndpoint.RoutePattern.RawText ?? endpoint.DisplayName ?? NormalizePath(context.Request.Path.
 60
 061            return endpoint.DisplayName ?? NormalizePath(context.Request.Path.ToString());
 62        }
 63
 64        public string GetIpAddress()
 65        {
 066            return context.Connection.RemoteIpAddress?.ToString() ?? "-";
 67        }
 68
 69        public string GetUserAgent()
 70        {
 071            return context.Request.Headers.UserAgent.FirstOrDefault() ?? "-";
 72        }
 73
 74        public async Task SignInTwoFactorUserIdSchemeAsync(int userId)
 75        {
 76            // Store user ID in Identity cookie for 2FA verification step
 2077            var identity = new ClaimsIdentity(IdentityConstants.TwoFactorUserIdScheme);
 2078            identity.AddClaim(new Claim(ClaimTypes.Name, userId.ToString()));
 2079            await context!.SignInAsync(IdentityConstants.TwoFactorUserIdScheme, new ClaimsPrincipal(identity));
 2080        }
 81    }
 82}