-
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.
Merge pull request #33 from OS2Valghalla/dev
Latest development
- Loading branch information
Showing
158 changed files
with
12,977 additions
and
1,110 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Valghalla.Application.Auth | ||
{ | ||
public static class AuthenticationUtilities | ||
{ | ||
public static bool IsAnonymousEndpoint(HttpContext context) => context.GetEndpoint()?.Metadata?.GetMetadata<IAllowAnonymous>() != null; | ||
|
||
public static bool IsApiEndpoint(HttpContext context) => context.Request.Path.HasValue && context.Request.Path.Value.Contains("/api/"); | ||
|
||
public static async Task SetUnauthorizedResponseAsync(HttpContext context, CancellationToken cancellationToken) | ||
{ | ||
context.Response.ContentType = "application/json"; | ||
context.Response.StatusCode = StatusCodes.Status401Unauthorized; | ||
await context.Response.WriteAsync(string.Empty, cancellationToken); | ||
} | ||
|
||
public static async Task SetTokenExpiredResponseAsync(HttpContext context, CancellationToken cancellationToken) | ||
{ | ||
context.Response.ContentType = "text/plain"; | ||
context.Response.StatusCode = StatusCodes.Status401Unauthorized; | ||
await context.Response.WriteAsync("__TOKEN_EXPIRED__", cancellationToken); | ||
} | ||
} | ||
} |
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,24 @@ | ||
using System.Security.Claims; | ||
|
||
namespace Valghalla.Application.Auth | ||
{ | ||
public static class ClaimsPrincipalExtension | ||
{ | ||
public const string Name = "valghalla_Name"; | ||
public const string Cpr = "valghalla_Cpr"; | ||
public const string Cvr = "valghalla_Cvr"; | ||
public const string Serial = "valghalla_Serial"; | ||
|
||
public const string Saml2NameIdFormat = "http://schemas.itfoxtec.com/ws/2014/02/identity/claims/saml2nameidformat"; | ||
public const string Saml2NameId = "http://schemas.itfoxtec.com/ws/2014/02/identity/claims/saml2nameid"; | ||
public const string Saml2SessionIndex = "http://schemas.itfoxtec.com/ws/2014/02/identity/claims/saml2sessionindex"; | ||
|
||
public static string? GetName(this ClaimsPrincipal principal) => principal.FindFirstValue(Name); | ||
public static string? GetCpr(this ClaimsPrincipal principal) => principal.FindFirstValue(Cpr); | ||
public static string? GetCvr(this ClaimsPrincipal principal) => principal.FindFirstValue(Cvr); | ||
public static string? GetSerial(this ClaimsPrincipal principal) => principal.FindFirstValue(Serial); | ||
public static string? GetSaml2NameIdFormat(this ClaimsPrincipal principal) => principal.FindFirstValue(Saml2NameIdFormat); | ||
public static string? GetSaml2NameId(this ClaimsPrincipal principal) => principal.FindFirstValue(Saml2NameId); | ||
public static string? GetSaml2SessionIndex(this ClaimsPrincipal principal) => principal.FindFirstValue(Saml2SessionIndex); | ||
} | ||
} |
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,8 @@ | ||
namespace Valghalla.Application.Auth | ||
{ | ||
public interface IUserTokenConfigurator | ||
{ | ||
string CookieName { get; } | ||
bool Renewable { get; } | ||
} | ||
} |
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,11 @@ | ||
using System.Security.Claims; | ||
|
||
namespace Valghalla.Application.Auth | ||
{ | ||
public interface IUserTokenManager | ||
{ | ||
void ExpireUserToken(); | ||
Task<UserToken?> EnsureUserTokenAsync(CancellationToken cancellationToken); | ||
Task<UserToken?> EnsureUserTokenAsync(ClaimsPrincipal principal, CancellationToken cancellationToken); | ||
} | ||
} |
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,9 @@ | ||
namespace Valghalla.Application.Auth | ||
{ | ||
public interface IUserTokenRepository | ||
{ | ||
Task<IEnumerable<UserToken>> GetUserTokensAsync(Guid identifier, CancellationToken cancellationToken); | ||
Task AddUserTokenAsync(UserToken token, CancellationToken cancellationToken); | ||
Task RemoveExpiredUserTokensAsync(CancellationToken cancellationToken); | ||
} | ||
} |
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,106 @@ | ||
using Microsoft.IdentityModel.Tokens; | ||
using System.Security.Claims; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace Valghalla.Application.Auth | ||
{ | ||
public class UserToken | ||
{ | ||
private static readonly int LIFETIME_PERIOD_MINUTES = 60; | ||
private static readonly int RENEWABLE_AFTER_MINUTES = 30; | ||
|
||
public class TokenKey | ||
{ | ||
public Guid Identifier { get; init; } | ||
public string Code { get; init; } = null!; | ||
} | ||
|
||
public class TokenValue | ||
{ | ||
public string? Name { get; init; } | ||
public string? Cvr { get; init; } | ||
public string? Cpr { get; init; } | ||
public string? Serial { get; init; } | ||
public string? Saml2NameIdFormat { get; init; } | ||
public string? Saml2NameId { get; init; } | ||
public string? Saml2SessionIndex { get; init; } | ||
} | ||
|
||
public TokenKey Key { get; init; } = new(); | ||
public TokenValue Value { get; init; } = new(); | ||
public DateTime CreatedAt { get; init; } | ||
public DateTime ExpiredAt { get; init; } | ||
public DateTime RefreshedAfter => CreatedAt.AddMinutes(RENEWABLE_AFTER_MINUTES); | ||
|
||
public bool Valid => DateTime.UtcNow < ExpiredAt; | ||
public bool Renewable => Valid && DateTime.UtcNow > RefreshedAfter; | ||
|
||
public ClaimsPrincipal ToClaimsPrincipal(bool includeSessionIndex = true) | ||
{ | ||
var claims = new List<Claim> { | ||
new(ClaimTypes.Name, Value.Saml2NameId ?? Key.Identifier.ToString()), | ||
new(ClaimTypes.NameIdentifier, Value.Saml2NameId ?? Key.Identifier.ToString()), | ||
new(ClaimsPrincipalExtension.Name, Value.Name ?? string.Empty), | ||
new(ClaimsPrincipalExtension.Cvr, Value.Cvr ?? string.Empty), | ||
new(ClaimsPrincipalExtension.Cpr, Value.Cpr ?? string.Empty), | ||
new(ClaimsPrincipalExtension.Serial, Value.Serial ?? string.Empty), | ||
new(ClaimsPrincipalExtension.Saml2NameIdFormat, Value.Saml2NameIdFormat ?? string.Empty), | ||
new(ClaimsPrincipalExtension.Saml2NameId, Value.Saml2NameId ?? string.Empty) | ||
}; | ||
|
||
if (includeSessionIndex) | ||
{ | ||
claims.Add(new(ClaimsPrincipalExtension.Saml2SessionIndex, Value.Saml2SessionIndex ?? string.Empty)); | ||
} | ||
|
||
var identity = new ClaimsIdentity(claims, Constants.Authentication.Scheme); | ||
return new(identity); | ||
} | ||
|
||
public static UserToken CreateToken(ClaimsPrincipal principal) => new() | ||
{ | ||
Key = new TokenKey() | ||
{ | ||
Identifier = Guid.NewGuid(), | ||
Code = GenerateCode() | ||
}, | ||
Value = new TokenValue() | ||
{ | ||
Name = principal.GetName(), | ||
Cvr = principal.GetCvr(), | ||
Cpr = principal.GetCpr(), | ||
Serial = principal.GetSerial(), | ||
Saml2NameIdFormat = principal.GetSaml2NameIdFormat(), | ||
Saml2NameId = principal.GetSaml2NameId(), | ||
Saml2SessionIndex = principal.GetSaml2SessionIndex(), | ||
}, | ||
CreatedAt = DateTime.UtcNow, | ||
ExpiredAt = DateTime.UtcNow.AddMinutes(LIFETIME_PERIOD_MINUTES) | ||
}; | ||
|
||
public static string Encode(TokenKey value) | ||
{ | ||
return Base64UrlEncoder.Encode(JsonSerializer.Serialize(value)); | ||
} | ||
|
||
public static TokenKey? Decode(string value) | ||
{ | ||
return JsonSerializer.Deserialize<TokenKey>(Base64UrlEncoder.Decode(value)); | ||
} | ||
|
||
private static string GenerateCode() | ||
{ | ||
var salt = Guid.NewGuid().ToString(); | ||
var hashObject = new HMACSHA512(Encoding.UTF8.GetBytes(Constants.Authentication.Cookie)); | ||
var signature = hashObject.ComputeHash(Encoding.UTF8.GetBytes(salt)); | ||
var encodedSignature = Convert.ToBase64String(signature) | ||
.Replace("+", string.Empty) | ||
.Replace("=", string.Empty) | ||
.Replace("/", string.Empty); | ||
|
||
return encodedSignature; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
namespace Valghalla.Application.Cache | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace Valghalla.Application.Cache | ||
{ | ||
public interface ITenantMemoryCache | ||
{ | ||
TItem? GetOrCreate<TItem>(string key, Func<TItem> factory); | ||
TItem? GetOrCreate<TItem>(string key, Func<ICacheEntry, TItem> factory); | ||
Task<TItem?> GetOrCreateAsync<TItem>(string key, Func<Task<TItem>> factory); | ||
Task<TItem?> GetOrCreateAsync<TItem>(string key, Func<ICacheEntry, Task<TItem>> factory); | ||
void Remove(string key); | ||
} | ||
} |
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
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
8 changes: 8 additions & 0 deletions
8
Valghalla.Application/Queue/Messages/RemovedFromTaskByValidationJobMessage.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,8 @@ | ||
namespace Valghalla.Application.Queue.Messages | ||
{ | ||
public sealed record RemovedFromTaskByValidationJobMessage | ||
{ | ||
public Guid ParticipantId { get; init; } | ||
public Guid TaskAssignmentId { get; init; } | ||
} | ||
} |
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,9 @@ | ||
using System.Security.Claims; | ||
|
||
namespace Valghalla.Application.Saml | ||
{ | ||
public interface ISaml2AuthPostProcessor | ||
{ | ||
Task<ClaimsPrincipal> HandleAsync(ClaimsPrincipal claimsPrincipal, CancellationToken cancellationToken); | ||
} | ||
} |
Oops, something went wrong.