Newer
Older
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UserMicroservice.Models;
using UserMicroservice.Services;
namespace UserMicroservice.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
private readonly IUserService _userService;
private readonly IAuthService _authService;
public UserController(IUserService userService, IAuthService authService)
{
_userService = userService;
_authService = authService;
}
#region Auth Endpoints
// POST: api/Users/register
[HttpPost("register")]
public IActionResult Register([FromBody] RegisterModel model)
{
User user = _userService.CreateUser(model.Email, model.Username, model.Password);
if(user == null)
return BadRequest();
return authenticateUser(user);
}
// POST: api/Users/login
[HttpPost("login")]
public IActionResult Login([FromBody] LoginModel model)
{
User? user = _userService.GetUser(model.Username, model.Password);
if(user == null)
return Unauthorized();
return authenticateUser(user);
}
private IActionResult authenticateUser(User user)
{
AuthTokenPair authToken = _authService.AuthenticateUser(user);
if (authToken == null)
return BadRequest();
// Set the access token as an HttpOnly cookie
Response.Cookies.Append("AccessToken", authToken.AccessToken, new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.Strict,
Expires = DateTimeOffset.UtcNow.AddMinutes(30)
});
// Set the refresh token as an HttpOnly cookie
Response.Cookies.Append("RefreshToken", authToken.RefreshToken, new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.Strict,
Expires = DateTimeOffset.UtcNow.AddDays(2)
});
return Ok();
}
// POST: api/Users/logout
[Authorize]
[HttpPost("logout")]
public IActionResult Logout()
{
string? refreshToken = Request.Cookies["RefreshToken"];
if(string.IsNullOrEmpty(refreshToken))
return BadRequest();
_authService.RevokeRefreshToken(refreshToken);
// Clear the access token cookie and set it to expire immediately
Response.Cookies.Append("AccessToken", string.Empty, new CookieOptions
{
HttpOnly = true,
Secure = true,
Expires = DateTimeOffset.UtcNow.AddSeconds(-1)
});
// Clear the refresh token cookie and set it to expire immediately
Response.Cookies.Append("RefreshToken", string.Empty, new CookieOptions
{
HttpOnly = true,
Secure = true,
Expires = DateTimeOffset.UtcNow.AddSeconds(-1)
});
return Ok();
}
#endregion
// GET: api/Users
[Authorize]
[HttpGet()]
public IActionResult GetUsers()
{
List<User> users = _userService.GetUsers();
if(users == null)
return BadRequest();
return Ok(users);
}
// GET: api/Users/{id}
[Authorize]
[HttpGet("{id}")]
public IActionResult GetUser(int id)
{
User? user = _userService.GetUser(id);
if(user == null)
return NotFound($"User with {id} doesnt exist");
return Ok(user);
}
}
}