< Summary

Information
Class: SharedObjects.Auth.AuthorizationPolicies
Assembly: IndeklimaWebApi
File(s): /home/runner/work/SEP4/SEP4/backend/SharedObjects/Auth/AuthorizationPolicies.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 41
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddAuth(...)100%210%
AddPolicies(...)100%210%

File(s)

/home/runner/work/SEP4/SEP4/backend/SharedObjects/Auth/AuthorizationPolicies.cs

#LineLine coverage
 1using System.Text;
 2using System.Security.Claims;
 3using Microsoft.AspNetCore.Authentication.JwtBearer;
 4using Microsoft.IdentityModel.Tokens;
 5
 6namespace SharedObjects.Auth;
 7
 8public static class AuthorizationPolicies
 9{
 10    public static void AddAuth(WebApplicationBuilder builder)
 011    {
 012        AuthorizationPolicies.AddPolicies(builder.Services);
 13
 014        builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
 015        {
 016            options.RequireHttpsMetadata = false;
 017            options.SaveToken = true;
 018            options.TokenValidationParameters = new TokenValidationParameters()
 019            {
 020                ValidateIssuer = true,
 021                ValidateAudience = true,
 022                ValidAudience = builder.Configuration["Jwt:Audience"],
 023                ValidIssuer = builder.Configuration["Jwt:Issuer"],
 024                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
 025            };
 026        });
 027    }
 28
 29    public static void AddPolicies(IServiceCollection services)
 030    {
 031        services.AddAuthorizationCore(options =>
 032        {
 033            options.AddPolicy("MustBeAdmin",
 034                a => a.RequireAuthenticatedUser().RequireClaim(ClaimTypes.Role, "Admin"));
 035            options.AddPolicy("MustBeUser",
 036                a => a.RequireAuthenticatedUser().RequireClaim(ClaimTypes.Role, "User"));
 037            options.AddPolicy("MustBeSuperUser",
 038                a => a.RequireAuthenticatedUser().RequireClaim(ClaimTypes.Role, "SuperUser"));
 039        });
 040    }
 41}