-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
108 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Inshapardaz.Api/Infrastructure/Middleware/CookieAuthenticationMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Text; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Inshapardaz.Domain.Adapters.Configuration; | ||
using Microsoft.Extensions.Options; | ||
using Inshapardaz.Domain.Adapters.Repositories; | ||
|
||
namespace Inshapardaz.Api.Infrastructure.Middleware; | ||
|
||
public class CookieAuthenticationMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly Settings _appSettings; | ||
|
||
public CookieAuthenticationMiddleware(RequestDelegate next, IOptions<Settings> appSettings) | ||
{ | ||
_next = next; | ||
_appSettings = appSettings.Value; | ||
} | ||
|
||
public async Task Invoke(HttpContext context, IAccountRepository accountRepository) | ||
{ | ||
if (context.Items["AccountId"] is null) | ||
{ | ||
var token = context.Request.Cookies["token"]; | ||
|
||
if (token != null) | ||
{ | ||
await AttachAccountToContext(context, token, accountRepository); | ||
} | ||
} | ||
|
||
await _next(context); | ||
} | ||
|
||
private async Task AttachAccountToContext(HttpContext context, string token, IAccountRepository accountRepository) | ||
{ | ||
try | ||
{ | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var key = Encoding.ASCII.GetBytes(_appSettings.Security.Secret); | ||
tokenHandler.ValidateToken(token, new TokenValidationParameters | ||
{ | ||
ValidateIssuerSigningKey = true, | ||
IssuerSigningKey = new SymmetricSecurityKey(key), | ||
ValidateIssuer = false, | ||
ValidateAudience = false, | ||
// set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later) | ||
ClockSkew = TimeSpan.FromMinutes(1), | ||
}, out SecurityToken validatedToken); | ||
|
||
var accessToken = (JwtSecurityToken)validatedToken; | ||
var accountId = int.Parse(accessToken.Claims.First(x => x.Type == "id").Value); | ||
|
||
context.Items["AccountId"] = accountId; | ||
context.Items["Account"] = await accountRepository.GetAccountById(accountId, CancellationToken.None); | ||
} | ||
catch (SecurityTokenValidationException) | ||
{ | ||
throw new UnauthorizedAccessException("JWT token invalid or expired"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters