-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthorizationController.cs
145 lines (118 loc) · 5.33 KB
/
AuthorizationController.cs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Library.BLL;
using Library.Model;
using Library.MVC.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace Library.MVC.Controllers
{
[Route("api/authorize")]
[ApiController]
public class AuthorizationController : Controller
{
private readonly IUserService _userService;
public AuthorizationController(IUserService userService)
{
_userService = userService;
}
[HttpPost("login")]
public IActionResult Login([FromBody] LoginModel credentials)
{
if (credentials == null)
{
return BadRequest("No credentials passed");
}
if (credentials.Username == null || credentials.Password == null)
{
return BadRequest("No username or password provided");
}
UserRequestModel loginModel = new UserRequestModel(credentials.Username, credentials.Password);
if (!_userService.IsCredentialsValid(loginModel))
{
return Unauthorized(new { error = "Invalid username or password" });
}
var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("returntheslabpharaoncurse"));
var signinCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256);
var tokenOptions = new JwtSecurityToken(
issuer: "https://localhost:64670",
audience: "https://localhost:4200",
claims: new List<Claim>
{
new Claim(JwtRegisteredClaimNames.Sub, credentials.Username)
},
expires: DateTime.Now.AddMinutes(30),
signingCredentials: signinCredentials
);
var tokenString = new JwtSecurityTokenHandler().WriteToken(tokenOptions);
return Ok(new AuthResponse { Token = tokenString });
}
[HttpPost("logout"), Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult Logout()
{
//TODO: wylogowywanie
return Ok("You logged out, trust me bro");
}
[HttpPost("register"), Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult Register([FromBody] LoginModel credentials)
{
//nulle chyba można usunąć bo to sprawdza na wejściu ale zostawie tobie do obejrzenia
//Pusta nazwa użytkownika robi śmieszne rzeczy tzn, wszystkie zapytania przechodzą ale nic się w bazie nie zmienia
if( (credentials.Username == null || credentials.Username == "") || (credentials.Password == null || credentials.Password == ""))
{
return BadRequest("No username or password provided");
}
if (_userService.IsUsernameExists(credentials.Username))
{
return Conflict("This user already exists, please choose another username");
}
UserRequestModel registerModel = new UserRequestModel(credentials.Username, credentials.Password);
_userService.RegisterUser(registerModel);
return Ok("You registered a new account, you can now log in");
}
[HttpPut("changePassword"), Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult ChangePassword([FromBody] ChangePasswordModel passwordModel)
{
string? tokenString = ReadJWTToken();
if (tokenString == null)
{
return Unauthorized(new { error = "Unknown token" });
}
if (passwordModel.NewPassword == "")
{
return BadRequest("New password is too short");
}
//Lekki syf się tu zrobił ale nie mogłem tego zostawić bez komunikatu o złym haśle
string username = _userService.JWTWhoAmI(tokenString);
bool canChangePassword = _userService.IsCredentialsValid(new UserRequestModel(username, passwordModel.OldPassword));
if (!canChangePassword)
{
return Unauthorized(new { error = "Old password don't match" });
}
_userService.UpdateUserPassword(tokenString, passwordModel.OldPassword, passwordModel.NewPassword);
return Ok("Password changed!");
}
[HttpGet("whoami"), Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult WhoAmI()
{
string? tokenString = ReadJWTToken();
if (tokenString != null)
{
return Ok(_userService.JWTWhoAmI(tokenString));
}
return Unauthorized(new { error = "Guest" });
}
private string? ReadJWTToken()
{
var authorizationHeader = HttpContext.Request.Headers["Authorization"].FirstOrDefault();
if (authorizationHeader != null && authorizationHeader.StartsWith("Bearer "))
{
return authorizationHeader.Substring("Bearer ".Length).Trim();
}
return null;
}
}
}