- Remove all // and /// comments from the backend test suite. - Replace hardcoded normalized-form string literals (e.g. NormalizedTitle/ NormalizedName duplicating a title/name in upper case) with a variable plus .ToUpperInvariant(), so the two values can't drift apart.
33 lines
1.4 KiB
C#
33 lines
1.4 KiB
C#
using System.Security.Claims;
|
|
using System.Text.Encodings.Web;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Musify.Api.Tests.TestSupport
|
|
{
|
|
public sealed class FakeAuthenticationHandler(
|
|
IOptionsMonitor<AuthenticationSchemeOptions> options,
|
|
ILoggerFactory logger,
|
|
UrlEncoder encoder)
|
|
: AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
|
|
{
|
|
public const string SchemeName = "Test";
|
|
public const string UserIdHeader = "X-Test-User-Id";
|
|
|
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
{
|
|
if (!Request.Headers.TryGetValue(UserIdHeader, out var userId) || string.IsNullOrEmpty(userId))
|
|
return Task.FromResult(AuthenticateResult.NoResult());
|
|
|
|
var claims = new List<Claim> { new(ClaimTypes.NameIdentifier, userId!) };
|
|
if (Request.Headers.TryGetValue("X-Test-User-Name", out var userName) && !string.IsNullOrEmpty(userName))
|
|
claims.Add(new Claim(ClaimTypes.Name, userName!));
|
|
|
|
var identity = new ClaimsIdentity(claims, SchemeName);
|
|
var ticket = new AuthenticationTicket(new ClaimsPrincipal(identity), SchemeName);
|
|
return Task.FromResult(AuthenticateResult.Success(ticket));
|
|
}
|
|
}
|
|
}
|