booking-microservices/src/BuildingBlocks/TestBase/Auth/AuthServiceCollectionExtensions.cs
2022-12-11 02:11:38 +03:30

33 lines
1.2 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
namespace BuildingBlocks.TestBase.Auth;
//ref: https://blog.joaograssi.com/posts/2021/asp-net-core-testing-permission-protected-api-endpoints/
public static class AuthServiceCollectionExtensions
{
private static MockAuthUser GetMockUser() =>
new MockAuthUser(new Claim("sub", Guid.NewGuid().ToString()),
new Claim("email", "sam@test.com"));
public static AuthenticationBuilder AddTestAuthentication(this IServiceCollection services)
{
services.AddAuthorization(options =>
{
// AuthConstants.Scheme is just a scheme we define. I called it "TestAuth"
options.DefaultPolicy = new AuthorizationPolicyBuilder("Test")
.RequireAuthenticatedUser()
.Build();
});
// Register a default user, so all requests have it by default
services.AddScoped(_ => GetMockUser());
// Register our custom authentication handler
return services.AddAuthentication("Test")
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", options => { });
}
}