< Summary

Information
Class: Syki.Back.Features.Cross.GenerateJWT.GenerateJWTService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Cross/GenerateJWT/GenerateJWTService.cs
Tag: 22_11348620282
Line coverage
100%
Covered lines: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 66
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
.ctor(...)100%11100%
Generate()100%11100%
GetDbClaims()100%22100%

File(s)

/home/runner/work/syki/syki/Back/Features/Cross/GenerateJWT/GenerateJWTService.cs

#LineLine coverage
 1using System.Text;
 2using System.Security.Claims;
 3using Microsoft.IdentityModel.Tokens;
 4using System.IdentityModel.Tokens.Jwt;
 5using Syki.Back.Features.Cross.CreateUser;
 6
 7namespace Syki.Back.Features.Cross.GenerateJWT;
 8
 3949public class GenerateJWTService(AuthSettings settings, UserManager<SykiUser> userManager, SykiDbContext ctx) : ICrossSer
 10{
 11    public async Task<string> Generate(string email)
 12    {
 38313        var user = (await userManager.FindByEmailAsync(email))!;
 38314        var role = (await userManager.GetRolesAsync(user))[0];
 15
 38316        var claims = new List<Claim>
 38317        {
 38318            new("jti", Guid.NewGuid().ToString()),
 38319            new("sub", user.Id.ToString()),
 38320            new("role", role),
 38321            new("name", user.Name),
 38322            new("email", user.Email!),
 38323            new("institution", user.InstitutionId.ToString()),
 38324        };
 38325        claims.AddRange(await GetDbClaims(user.Id, role));
 26
 38327        var identityClaims = new ClaimsIdentity();
 38328        identityClaims.AddClaims(claims);
 29
 38330        var key = Encoding.ASCII.GetBytes(settings.SecurityKey);
 38331        var expirationTime = settings.ExpirationTimeInMinutes;
 38332        var signingCredentials = new SigningCredentials(
 38333            new SymmetricSecurityKey(key),
 38334            SecurityAlgorithms.HmacSha256Signature
 38335        );
 36
 38337        var tokenDescriptor = new SecurityTokenDescriptor
 38338        {
 38339            Issuer = settings.Issuer,
 38340            Subject = identityClaims,
 38341            Audience = settings.Audience,
 38342            SigningCredentials = signingCredentials,
 38343            Expires = DateTime.UtcNow.AddMinutes(expirationTime),
 38344        };
 45
 38346        var tokenHandler = new JwtSecurityTokenHandler();
 38347        var token = tokenHandler.CreateToken(tokenDescriptor);
 48
 38349        return tokenHandler.WriteToken(token);
 38350    }
 51
 52    private async Task<List<Claim>> GetDbClaims(Guid userId, string role)
 53    {
 38354        if (role.ToEnum<UserRole>() is UserRole.Student)
 55        {
 8056            var courseOfferingId = await ctx.Students.Where(a => a.Id == userId)
 8057                .Select(a => a.CourseOfferingId).FirstAsync();
 8058            var courseCurriculumId = await ctx.CourseOfferings.Where(o => o.Id == courseOfferingId)
 8059                .Select(o => o.CourseCurriculumId).FirstAsync();
 60
 8061            return [ new("CourseCurriculumId", courseCurriculumId.ToString()) ];
 62        }
 63
 30364        return [];
 38365    }
 66}