Skip to content

Instantly share code, notes, and snippets.

@HaoK
Last active December 12, 2016 01:02
Show Gist options
  • Save HaoK/0197141b932bcf45d99dcb5e6eb70374 to your computer and use it in GitHub Desktop.
Save HaoK/0197141b932bcf45d99dcb5e6eb70374 to your computer and use it in GitHub Desktop.
Auth 2.0
public interface IAuthenticationManager
{
Task<AuthenticateInfo> AuthenticateAsync(string scheme);
Task ChallengeAsync(string scheme, AuthenticationProperties properties, ChallengeBehavior behavior);
Task ForbidAsync(string scheme, AuthenticationProperties properties);
// Should SignIn/SignOut live in a separate service? If yes, we could have a parallel stack of
// SignInScheme/Builder/SignInHandler
Task SignInAsync(string scheme, ClaimsPrincipal principal);
Task SignOutAsync(string scheme, AuthenticationProperties properties);
}
// Should probably revisit the (auto) challenge behavior as part of this
public enum ChallengeBehavior
{
Automatic,
Unauthorized,
Forbidden
}
public class AuthenticateInfo
{
public ClaimsPrincipal Principal { get; set; }
public AuthenticationProperties Properties { get; set; }
}
public class AuthenticationOptions {
public IDictionary<Name, AuthenticationScheme> SchemeMap { get; }
public void AddScheme(string name, Action<AuthenticationSchemeBuilder> configureBuilder) {
var builder = new AuthenticationSchemeBuilder(name);
configureBuilder(builder);
SchemeMap[name] = builder.Build();
}
}
public class AuthenticationSchemeBuilder
{
public AuthenticationScheme Build();
}
public class AuthenticationScheme
{
public string Name { get; }
public IAuthenticationSchemeHandler Handler { get; }
public Type HandlerType { get; }
}
public interface IAuthenticationSchemeProvider {
AuthenticationScheme GetScheme(string name);
}
public interface IAuthenticationSchemeHandler
{
Task AuthenticateAsync(AuthenticateContext context);
Task ChallengeAsync(ChallengeContext context);
Task SignInAsync(SignInContext context);
Task SignOutAsync(SignOutContext context);
}
public static IServiceCollection AddAuthentication(static this IServiceCollection services, Action<AuthenticationOptions> configure) {
services.TryAddSingleton<IAuthenticationManager, DefaultAuthenticationManager>();
services.TryAddSingleton<IAuthenticationSchemeProvider, DefaultAuthenticationSchemeProvider>();
services.Configure(configure);
return services;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment