Repository navigation
Expand file tree
/
Copy pathAuthenticationController.cs
More file actions
85 lines (74 loc) · 3.26 KB
/
Copy pathAuthenticationController.cs
File metadata and controls
85 lines (74 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.IdentityModel.Tokens.Jwt;
using System.Net;
using System.Security.Claims;
using System.Text;
using FrontEnd.SPA.Infrastructure;
using FrontEnd.SPA.Models;
using FrontEnd.SPA.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace FrontEnd.SPA.Controllers;
[ApiController]
[AllowAnonymous]
[Route( "api/[controller]" )]
public class AuthenticationController : ControllerBase
{
private readonly IAccountAuthentication authentication;
private readonly JwtSettings configuration;
private readonly ITokenLifetimeManager tokenLifetimeManager;
public AuthenticationController( IOptions<JwtSettings> configuration,
IAccountAuthentication authentication,
ITokenLifetimeManager tokenLifetimeManager )
{
this.configuration = configuration.Value;
this.authentication = authentication;
this.tokenLifetimeManager = tokenLifetimeManager;
}
[HttpPost( "login" )]
[ProducesResponseType( (int) HttpStatusCode.OK, Type = typeof(JwtToken) )]
[ProducesResponseType( (int) HttpStatusCode.Unauthorized, Type = typeof(ApiResponse) )]
public IActionResult LogIn( Credentials credentials )
{
if ( !authentication.IsValid( credentials ) ) return Unauthorized();
var userFingerprint = "Something random";
var tokenDescriptor = new SecurityTokenDescriptor
{
Issuer = configuration.Issuer,
Audience = configuration.Audience,
Expires = DateTime.Now.AddHours( 5 ),
Subject = new ClaimsIdentity( new[]
{
new Claim( JwtRegisteredClaimNames.Sub, credentials.Username ),
new Claim( JwtRegisteredClaimNames.Email, credentials.Username ),
new Claim( JwtRegisteredClaimNames.Jti, JwtTokenReplayManager.Encode( userFingerprint ) )
} ),
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey( Encoding.UTF8.GetBytes( configuration.Key ) ),
SecurityAlgorithms.HmacSha512Signature
)
};
var token = new JwtSecurityTokenHandler().CreateToken( tokenDescriptor ) as JwtSecurityToken;
Response.Cookies.Append( "__Host-vftn",
userFingerprint,
new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.Strict,
IsEssential = true
} );
return Ok( new JwtToken( token!.RawData ) );
}
[HttpPost( "logout" )]
[ProducesResponseType( (int) HttpStatusCode.OK )]
public IActionResult LogOut( [FromHeader( Name = "Authorization" )] string? authorization )
{
if ( string.IsNullOrWhiteSpace( authorization ) ) return Ok();
string bearerToken =
authorization.Replace( "Bearer ", string.Empty, StringComparison.InvariantCultureIgnoreCase );
tokenLifetimeManager.SignOut( new JwtSecurityToken( bearerToken ) );
return Ok();
}
}