< Summary - Syki

Information
Class: Syki.Back.Configs.AuthenticationConfigs
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Configs/AuthenticationConfigs.cs
Tag: 4_16869239191
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 61
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddAuthenticationConfigs(...)100%22100%

File(s)

/home/runner/work/syki/syki/Back/Configs/AuthenticationConfigs.cs

#LineLine coverage
 1using System.Text;
 2using Microsoft.IdentityModel.Tokens;
 3using System.IdentityModel.Tokens.Jwt;
 4using Microsoft.AspNetCore.Authentication.JwtBearer;
 5
 6namespace Syki.Back.Configs;
 7
 8public static class AuthenticationConfigs
 9{
 10    public const string BearerScheme = "Bearer";
 11
 12    public static void AddAuthenticationConfigs(this WebApplicationBuilder builder)
 13    {
 214        var settings = builder.Configuration.Auth();
 15
 216        JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
 217        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
 18
 219        var tokenValidationParameters = new TokenValidationParameters
 220        {
 221            ValidateIssuer = true,
 222            ValidIssuer = settings.Issuer,
 223
 224            ValidateIssuerSigningKey = true,
 225            IssuerSigningKey = new SymmetricSecurityKey(
 226                Encoding.ASCII.GetBytes(settings.SecurityKey)
 227            ),
 228
 229            ValidAlgorithms = [ "HS256" ],
 230
 231            ValidateAudience = true,
 232            ValidAudience = settings.Audience,
 233
 234            ValidateLifetime = true,
 235            ClockSkew = TimeSpan.Zero,
 236
 237            RoleClaimType = "role",
 238        };
 39
 240        builder.Services.AddAuthentication(BearerScheme)
 241            .AddJwtBearer(BearerScheme, options =>
 242            {
 243                options.TokenValidationParameters = tokenValidationParameters;
 244
 245                options.Events = new JwtBearerEvents
 246                {
 247                    OnMessageReceived = context =>
 248                    {
 499049                        var cookieJwt = context.Request.Cookies["syki_jwt"];
 499050                        if (cookieJwt.HasValue())
 251                        {
 498852                            context.Token = cookieJwt;
 498853                            return Task.CompletedTask;
 254                        }
 255
 256                        return Task.CompletedTask;
 257                    }
 258                };
 459            });
 260    }
 61}