-
Notifications
You must be signed in to change notification settings - Fork 597
Auth 2.0 using OptionsFactory (named options) #1144
Conversation
@HaoK, |
To avoid confusion when reading the code
- do not have .NET 4.6.1 reference assemblies on all CI machines - have corrected System.XML casing issue mentioned in 7637f2e nit: sort dependencies
@Tratcher I Pushed the duplicated DataProtectionProvider code in all the RemoteAuths up to base to clean things up |
@PinpointTownes do you prefer this iteration that is more similar to the old behavior, where any scheme that implements IAuthenticationRequestHandler will see all requests? I don't have an easy way right now to implement the old callback ghetto routing with the switch to using named options |
I'll take a deeper look tomorrow, but it looks really promising, I like it 😄 |
var handler = await handlers.ResolveHandlerAsync(context, scheme.Name); | ||
if (await handler.HandleRequestAsync()) | ||
var handler = await handlers.GetHandlerAsync(context, scheme.Name) as IAuthenticationRequestHandler; | ||
if (await handler?.HandleRequestAsync()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: this line will null-ref if handler
is null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops thanks fixed
@@ -12,9 +12,6 @@ public interface IAuthenticationService | |||
Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string scheme); | |||
Task ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties, ChallengeBehavior behavior); | |||
Task ForbidAsync(HttpContext context, string scheme, AuthenticationProperties properties); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Until we sort out Challenge vs Forbid, Forbid is not a 1st class thing, it's a subset of Challenge.
Rebased to dev replaced with #1151 |
IOptionsFactory
which provides named options in addition to validation, both of which are triggered via IOptionsFactory.Get("name"). Allows normal options configuration for schemes, Validation is just a different set of Actions that run after the ConfiguresIOptionsFactory Usage:
IAuthenticationRequestHandler
interface instead, CallbackPaths routing is disabled for now (all IAuthenticationRequestHandler schemes are given a chance to handle the request)TL:DR Auth 2.0 summary
IAuthenticationService
with the sameAuthenticate/Challenge/SignIn/Out/ForbidAsync
API.AuthenticationScheme
instances which have: Name, Type, and Settings dictionary (Options instance + old description bag)app.UseXyzAuth(new XyzOptions())
=>services.AddXyzAuth(o => o.Foo = bar)
adds anAuthenticationScheme
instance for XyzUseAuthentication
is required, which takes care of the old 'automatic' authenticate, in addition to giving each scheme a chance to handle the request like the old middlewares did.Default[Authenticate/SignIn]Scheme
inAuthenticationOptions
, rather than a flag on each middleware. No ambiguity, altho there's one bit of magic where if none are specified and there's only a single scheme registered, we use that scheme as the default.Pros of the new design
IAuthenticationSchemeProvider
service.Example of using scoped DbContext
This was not really easily accomplished in the old stack since handlers were created by the middleware via
protected abstract AuthenticationHandler<TOptions> CreateHandler();
Claims Transformation
A lot simpler, new
IClaimsTransformation
service which has a single method:Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
We call this on any successful
AuthenticateAsync
call.To use a more advanced scoped claims transform that uses DbContext's, they would just need to add their own
IClaimsTransformation
before anyAddAuthentication
calls.