Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Switch to services.Initialize<TOptions> #1206

Closed
wants to merge 5 commits into from
Closed

Switch to services.Initialize<TOptions> #1206

wants to merge 5 commits into from

Conversation

HaoK
Copy link
Member

@HaoK HaoK commented May 8, 2017

Goal is to eliminate the virtual AuthenticationHandler.InitializeOptions and the double check locking logic.

Depends on: aspnet/Options#187

Doesn't seem too bad but I had to add a global ensure dataprotection initializer to make things work, definitely is a bit subtle.

I only did Cookies + OAuth and its children for a proof of concept first step. I'll do OIDC next.

cc @Tratcher @ajcvickers

@dnfclas
Copy link

dnfclas commented May 8, 2017

@HaoK,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by .NET Foundation. We will now review your pull request.
Thanks,
.NET Foundation Pull Request Bot

@@ -48,6 +48,8 @@ protected HttpResponse Response
/// </summary>
protected virtual object Events { get; set; }

protected virtual string ClaimsIssuer => Options.ClaimsIssuer ?? Scheme.Name;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we have to decide whether we require all AddXyz's to chaining to a new AddAuthenticationHandler method which adds an Initializer that just defaults ClaimsIssuer to scheme.Name, or add this new property on the handler which does the defaulting.

I'm leaning towards the property. But we still are going to have chaining for each virtual level of InitializeOptions we have today, so there's going to be an AddRemoteAuthenticationHandler in the next iteration.

@@ -72,6 +73,8 @@ public string CookieName

/// <summary>
/// If set this will be used by the CookieAuthenticationMiddleware for data protection.
/// he default data protection service is based on machine key when running on ASP.NET,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just remove this comment, that's an implementation detail of DataProtection.

@HaoK
Copy link
Member Author

HaoK commented May 8, 2017

Updated with OIDC/Twitter changes, one nice sideeffect is some of the dependencies for the handlers are no longer needed, namely dataprotection/shared AuthenticationOptions, as those can now just live in the options initializers

Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. What's left?


public static IServiceCollection AddRemoteScheme<TOptions, THandler>(this IServiceCollection services, string authenticationScheme, string displayName, Action<TOptions> configureOptions)
where TOptions : RemoteAuthenticationOptions, new()
where THandler : AuthenticationHandler<TOptions>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RemoteAuthenticationHandler?

@kevinchalet
Copy link
Contributor

I really love what's happening here 😄

That said, is there any particular reason we can't get rid of the initialization lock in AuthenticationSchemeOptions with this approach? 😮

@HaoK
Copy link
Member Author

HaoK commented May 12, 2017

Yeah that's the plan @PinpointTownes to get rid of that lock, I just got interrupted and need to finish removing whatevers left. I'll update the PR with the final iteration today

@kevinchalet
Copy link
Contributor

Brilliant!

{
// Makes sure that DataProtectionProvider is always set.
services.TryAddEnumerable(ServiceDescriptor.Singleton<IInitializeOptions<CookieAuthenticationOptions>, EnsureDataProtection>());
services.Initialize<CookieAuthenticationOptions>(authenticationScheme, options =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having the initialization in two places (in a delegate and in a private class), maybe it would be worth consolidating the two and moving everything to a public class? (e.g CookieAuthenticationInitializer)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, do we want to allow people to replace/customize this behavior? They already can via adding their own Initializer after, or Configuring it before. I guess it doesn't hurt to leave it public, altho on an unrelated note, all of our handlers are still internal which probably should change now that they actually can be extended

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, do we want to allow people to replace/customize this behavior?

Actually, that's not the scenario I had in mind. I was just wondering if making the initialization logic wouldn't prevent folks from registering the handler manually (i.e without using the extension), e.g if you don't want the IConfiguration importation thingy. But since the handlers are not public, this is not possible yet, even if you make the initializers public.

@HaoK HaoK changed the title [Prototype] Switch to services.Initialize<TOptions> Switch to services.Initialize<TOptions> May 12, 2017
@HaoK
Copy link
Member Author

HaoK commented May 12, 2017

Updated with final changes to JwtBearer and removed locking, also combined initializers into a single public type per Auth package. Will merge Options PR + this next week


if (options.StateDataFormat == null)
{
if (options.DataProtectionProvider == null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HaoK now that the two initialization routines have been merged, I guess this can be safely removed, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep good catch, thanks I'll remove

@HaoK
Copy link
Member Author

HaoK commented May 17, 2017

1f5a27e

@HaoK HaoK closed this May 17, 2017
@HaoK HaoK mentioned this pull request May 18, 2017
15 tasks
@HaoK HaoK deleted the haok/init branch August 7, 2017 16:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants