Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/Api/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Authentication;
using Api.Controllers.Payload.Requests;
using Api.Controllers.Payload.Responses;
using Application.Common.Interfaces;
Expand Down Expand Up @@ -67,12 +68,21 @@ public async Task<IActionResult> Refresh()
{
var refreshToken = Request.Cookies[nameof(RefreshToken)];
var jweToken = Request.Cookies["JweToken"];

var authResult = await _identityService.RefreshTokenAsync(jweToken!, refreshToken!);

SetRefreshToken(authResult.RefreshToken);
SetJweToken(authResult.Token, authResult.RefreshToken);


try
{
var authResult = await _identityService.RefreshTokenAsync(jweToken!, refreshToken!);

SetRefreshToken(authResult.RefreshToken);
SetJweToken(authResult.Token, authResult.RefreshToken);
}
catch (AuthenticationException)
{
RemoveJweToken();
RemoveRefreshToken();
throw;
}

return Ok();
}

Expand Down
15 changes: 3 additions & 12 deletions src/Infrastructure/Identity/IdentityService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Data;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Reflection.Metadata.Ecma335;
using System.Security.Authentication;
using System.Security.Claims;
using System.Security.Cryptography;
Expand Down Expand Up @@ -149,19 +150,9 @@ public async Task<AuthenticationResult> RefreshTokenAsync(string token, string r
throw new AuthenticationException("This refresh token does not match this Jwt.");
}

var jweToken = CreateJweToken(user);
var result = await GenerateAuthenticationResultForUserAsync(user);

storedRefreshToken.JwtId = jweToken.Id;
storedRefreshToken.ExpiryDateTime =
LocalDateTime.FromDateTime(DateTime.UtcNow.AddDays(_jweSettings.RefreshTokenLifetimeInDays));
_context.RefreshTokens.Update(storedRefreshToken);
await _context.SaveChangesAsync();

return new AuthenticationResult()
{
Token = jweToken,
RefreshToken = _mapper.Map<RefreshTokenDto>(storedRefreshToken)
};
return result;
}

private ClaimsPrincipal? GetPrincipalFromToken(string token)
Expand Down