2024-08-17 16:38:40 +02:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization.Policy;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
2025-06-12 18:22:37 +02:00
|
|
|
namespace Vegasco.Server.Api.Tests.Integration;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
public sealed class TestUserAlwaysAuthorizedPolicyEvaluator : IPolicyEvaluator
|
|
|
|
|
{
|
|
|
|
|
public const string Username = "Test user";
|
|
|
|
|
public static readonly string UserId = Guid.NewGuid().ToString();
|
|
|
|
|
|
|
|
|
|
public Task<AuthenticateResult> AuthenticateAsync(AuthorizationPolicy policy, HttpContext context)
|
|
|
|
|
{
|
|
|
|
|
Claim[] claims =
|
|
|
|
|
[
|
|
|
|
|
|
|
|
|
|
new Claim(ClaimTypes.Name, Username),
|
|
|
|
|
new Claim("name", Username),
|
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, UserId),
|
|
|
|
|
new Claim("aud", "https://localhost")
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
ClaimsIdentity identity = new(claims, JwtBearerDefaults.AuthenticationScheme);
|
|
|
|
|
ClaimsPrincipal principal = new(identity);
|
|
|
|
|
AuthenticationTicket ticket = new(principal, JwtBearerDefaults.AuthenticationScheme);
|
2025-06-12 17:43:22 +02:00
|
|
|
AuthenticateResult result = AuthenticateResult.Success(ticket);
|
2024-08-17 16:38:40 +02:00
|
|
|
return Task.FromResult(result); ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<PolicyAuthorizationResult> AuthorizeAsync(AuthorizationPolicy policy, AuthenticateResult authenticationResult, HttpContext context,
|
|
|
|
|
object? resource)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(PolicyAuthorizationResult.Success());
|
|
|
|
|
}
|
|
|
|
|
}
|