< Summary

Information
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 76
Line coverage: 100%
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
.ctor(...)100%11100%
GenerateClaims(...)100%11100%
GenerateJwt(...)100%11100%
LoginAsync()100%11100%

File(s)

/home/runner/work/SEP4/SEP4/backend/microservices/autorisering/WebApi/Controllers/AuthController.cs

#LineLine coverage
 1using System.IdentityModel.Tokens.Jwt;
 2using System.Security.Claims;
 3using System.Text;
 4using Microsoft.AspNetCore.Authorization;
 5using Microsoft.AspNetCore.Mvc;
 6using Microsoft.IdentityModel.Tokens;
 7using WebApi.Models;
 8using WebApi.Services;
 9
 10[ApiController]
 11[Route("[controller]")]
 12public class AuthController : ControllerBase
 13{
 14    private readonly IConfiguration config;
 15    private readonly IUserService _userService;
 16
 317    public AuthController(IConfiguration config, IUserService userService)
 318    {
 319        this.config = config;
 320        this._userService = userService;
 321    }
 22
 23    private List<Claim> GenerateClaims(User user)
 224    {
 225        var claims = new[]
 226        {
 227        new Claim(JwtRegisteredClaimNames.Sub, config["Jwt:Subject"]),
 228        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
 229        new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString()),
 230        new Claim(ClaimTypes.Name, user.Username),
 231        new Claim(ClaimTypes.Role, user.Role),
 232        new Claim("Email", user.Email),
 233        new Claim("Age", user.Age.ToString()),
 234    };
 235        return claims.ToList();
 236    }
 37
 38    private string GenerateJwt(User user)
 239    {
 240        List<Claim> claims = GenerateClaims(user);
 41
 242        SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Jwt:Key"]));
 243        SigningCredentials signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha512);
 44
 245        JwtHeader header = new JwtHeader(signIn);
 46
 247        JwtPayload payload = new JwtPayload(
 248            config["Jwt:Issuer"],
 249            config["Jwt:Audience"],
 250            claims,
 251            null,
 252            DateTime.UtcNow.AddMinutes(60));
 53
 254        JwtSecurityToken token = new JwtSecurityToken(header, payload);
 55
 256        string serializedToken = new JwtSecurityTokenHandler().WriteToken(token);
 257        return serializedToken;
 258    }
 59
 60    [HttpPost, Route("login")]
 61    public async Task<ActionResult> LoginAsync([FromBody] UserLoginDTO userLoginDto)
 362    {
 63        try
 364        {
 365            User user = await _userService.ValidateUserAsync(userLoginDto.Username, userLoginDto.Password);
 266            string token = GenerateJwt(user);
 67
 268            return Ok(token);
 69        }
 170        catch (Exception e)
 171        {
 172            return BadRequest(e.Message);
 73        }
 374    }
 75
 76}