diff options
Diffstat (limited to 'backend/api')
-rw-r--r-- | backend/api/api/Controllers/AuthController.cs | 16 | ||||
-rw-r--r-- | backend/api/api/Models/JwtToken.cs | 30 | ||||
-rw-r--r-- | backend/api/api/Services/AuthService.cs | 15 | ||||
-rw-r--r-- | backend/api/api/Services/IAuthService.cs | 1 |
4 files changed, 61 insertions, 1 deletions
diff --git a/backend/api/api/Controllers/AuthController.cs b/backend/api/api/Controllers/AuthController.cs index e1601815..6dfe483a 100644 --- a/backend/api/api/Controllers/AuthController.cs +++ b/backend/api/api/Controllers/AuthController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using api.Services; using Microsoft.AspNetCore.Authorization; +using Microsoft.Net.Http.Headers; namespace api.Controllers { @@ -37,6 +38,21 @@ namespace api.Controllers return Ok("works"); } + [HttpPost("renewJwt")] + [Authorize(Roles = "User")] + public async Task<ActionResult<string>> RenewJwt() { + var authorization = Request.Headers[HeaderNames.Authorization]; + + var newToken=_auth.RenewToken(authorization); + if(newToken== null) + return BadRequest(); + return Ok(newToken); + + + + + } + } diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs index 23307185..3ecbf92d 100644 --- a/backend/api/api/Models/JwtToken.cs +++ b/backend/api/api/Models/JwtToken.cs @@ -31,6 +31,36 @@ namespace api.Models } + public string RenewToken(string existingToken) + { + if (existingToken == null) + return null; + var tokenHandler = new JwtSecurityTokenHandler(); + var key= Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + try + { + tokenHandler.ValidateToken(existingToken, new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false, + }, out SecurityToken validatedToken); + + var jwtToken = (JwtSecurityToken)validatedToken; + var userName =jwtToken.Claims.First(x => x.Type == "name").Value; + var authUser = new AuthRequest(); + authUser.UserName = userName; + + return GenToken(authUser); + } + catch + { + return null; + } + + } + } diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs index 015fdac7..4f838463 100644 --- a/backend/api/api/Services/AuthService.cs +++ b/backend/api/api/Services/AuthService.cs @@ -1,4 +1,5 @@ -using api.Interfaces; +using System.Net.Http.Headers; +using api.Interfaces; using api.Models; using api.Models.Users; using MongoDB.Driver; @@ -44,6 +45,18 @@ namespace api.Services return "User added"; } + public string RenewToken(string header) + { + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + return _jwt.RenewToken(parameter); + } + return null; + } + } } diff --git a/backend/api/api/Services/IAuthService.cs b/backend/api/api/Services/IAuthService.cs index 79085f8c..591d122d 100644 --- a/backend/api/api/Services/IAuthService.cs +++ b/backend/api/api/Services/IAuthService.cs @@ -6,5 +6,6 @@ namespace api.Services { string Login(AuthRequest user); string Register(RegisterRequest user); + string RenewToken(string token); } }
\ No newline at end of file |