netmash/src/security/authentication/Netmash.Security.Authentica.../AuthenticationExtensions.cs

43 lines
1.7 KiB
C#

using Tuitio.Wrapper;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Netmash.Security.Authentication.Tuitio.Abstractions;
using System;
namespace Netmash.Security.Authentication.Tuitio
{
public static class AuthenticationExtensions
{
public static IServiceCollection AddTuitioAuthentication(this IServiceCollection services, string tuitioBaseAddress)
{
services.AddTuitioAuthentication(tuitioBaseAddress, new Models.AuthenticationOptions());
return services;
}
public static IServiceCollection AddTuitioAuthentication(this IServiceCollection services, string tuitioBaseAddress, IAuthenticationOptions options)
{
Validate(tuitioBaseAddress, options);
// Tuitio
services.UseTuitioServices(tuitioBaseAddress);
services.AddSingleton(options);
// configure authentication
services.AddAuthentication("TuitioAuthentication")
.AddScheme<AuthenticationSchemeOptions, TuitioAuthenticationHandler>("TuitioAuthentication", null);
return services;
}
private static void Validate(string tuitioBaseAddress, IAuthenticationOptions options)
{
if (string.IsNullOrEmpty(tuitioBaseAddress))
throw new ArgumentException("Tuitio base address must be provided.");
var guestFuncDefined = options.AuthenticateAsGuest != null;
if (guestFuncDefined && string.IsNullOrEmpty(options.GuestUserName))
throw new ArgumentException("Guest function is defined, but guest user name is not set.");
}
}
}