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

Replace configure method on TwitterOptions and OpenIdConnectOptions with CookieBuilder #1284

Merged
merged 1 commit into from
Jul 5, 2017

Conversation

natemcmaster
Copy link
Contributor

Addresses aspnet/HttpAbstractions#853.

Uses the new CookieBuilder API added here: aspnet/HttpAbstractions#882

Part 1- look for a separate PR to remove duplication from CookieAuthenticationOptions.

@natemcmaster natemcmaster requested review from Tratcher and HaoK June 29, 2017 23:43
@dnfclas
Copy link

dnfclas commented Jun 29, 2017

@natemcmaster,
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

};

Options.ConfigureNonceCookie?.Invoke(Context, cookieOptions);
cookieOptions.Path = OriginalPathBase + Options.CallbackPath;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just do this instead? cookieOptions.Path = cookieOptions.Path ?? OriginalPathBase + Options.CallbackPath;

There's also a subtle bug here, since even if Path == null, the Builder could set the cookie.Path, which would get blasted away

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's why this deliberately checks if (Options.NonceCookie.Path == null) first.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah so what if they have CustomCookieBuilder which sets cookieOptions.Path = "/mypath" in Build(), this code will still stomp on the path. The check should be checking the real cookieOptions.Path, not the builder property

Copy link
Contributor Author

@natemcmaster natemcmaster Jun 30, 2017

Choose a reason for hiding this comment

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

Hmm. This is problematic. The default ctor for CookieOptions always sets Path to "/". If someone implements a custom cookie builder, it will be hard for us to detect if they indend to overwrite the default value or not.

var builder = new CookieBuilder();
  // builder.Path= null
var options = builder.Build(context)
  // options.Path= "/"

 // builder.Path== null means we can assume the user does not indent to change 
 // the default path, therefore we set it to OriginalPathBase + Options.CallbackPath
var builder = new MyCustomCookieBuilder();
  // builder.Path= null
var options = builder.Build(context)
  // options.Path= "/mypath"

  // builder.Path== null means ????

Copy link
Member

@HaoK HaoK Jun 30, 2017

Choose a reason for hiding this comment

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

Just check for options.Path == "/" and builder.Path== null?

Copy link
Member

Choose a reason for hiding this comment

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

We are talking about Path not name right?

Copy link
Member

Choose a reason for hiding this comment

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

Or we can just check for options.Path == "/" since we can't really assume anything based on the property alone.

if (cookieOptions.Path == "/") {
    cookieOptions.Path = OriginalPathBase + Options.CallbackPath;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured out a way to do that. I override the build method to detect the value of Path before. Should allow user to override.

Expires = Clock.UtcNow.Add(Options.ProtocolValidator.NonceLifetime)
};
var cookieOptions = Options.NonceCookie.Build(Context);
cookieOptions.Expires = Clock.UtcNow.Add(Options.ProtocolValidator.NonceLifetime);
Copy link
Member

Choose a reason for hiding this comment

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

This isn't honoring the Expiration from CookieBuilder. Only set it if not null?

Any way to resolve Options.ProtocolValidator.NonceLifetime in the builder? Then you could incorporate it in a way that could be overridden.

Copy link
Member

Choose a reason for hiding this comment

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

Yup, just add a Property in a OpenIdConnectCookieBuilder for it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@@ -50,7 +49,8 @@ public TwitterHandler(IOptionsSnapshot<TwitterOptions> options, ILoggerFactory l
{
AuthenticationProperties properties = null;
var query = Request.Query;
var protectedRequestToken = Request.Cookies[StateCookie];
var cookieName = Options.StateCookie.Name ?? StateCookieName;
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this gets simpler if we prevent Name from being null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@Tratcher
Copy link
Member

Add support for the correlation cookie, or file a followup bug since this isn't currently configurable.

@HaoK
Copy link
Member

HaoK commented Jun 30, 2017

Should just make the change in this PR for correlation cookie

@natemcmaster natemcmaster force-pushed the namc/replace-cookie-builder branch from 2cbf29b to 0a740b2 Compare June 30, 2017 18:33
@natemcmaster
Copy link
Contributor Author

Rebased and added the correlation cookie change to this PR.

var path = Path;
if (path == null)
{
var originalPathBase = context.Features.Get<IAuthenticationFeature>()?.OriginalPathBase ?? context.Request.PathBase;
Copy link
Member

Choose a reason for hiding this comment

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

Should we consider having the handlers set a OriginalPathBase property on the CookieBuilder instead of having the builder pull it from the context?

var path = Path;
if (path == null)
{
var originalPathBase = context.Features.Get<IAuthenticationFeature>()?.OriginalPathBase ?? context.Request.PathBase;
Copy link
Member

Choose a reason for hiding this comment

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

Commenting again in the right version: Should we make this a property on RequestPathBaseCookieBuilder that the handlers set instead of making the builder pull it out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly racy. The builder is shared across multiple contexts.

@natemcmaster
Copy link
Contributor Author

🔔

{
Logger.CorrelationPropertyNotFound(CorrelationPrefix);
Logger.CorrelationPropertyNotFound(RemoteAuthenticationOptions.CorrelationPrefix);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be something like Options.CorrelationCookie.Name now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup. Updated.

@natemcmaster natemcmaster force-pushed the namc/replace-cookie-builder branch from a565a24 to 968237d Compare July 5, 2017 16:58
@natemcmaster natemcmaster merged commit 968237d into dev Jul 5, 2017
@natemcmaster natemcmaster deleted the namc/replace-cookie-builder branch July 5, 2017 17:09

Response.Cookies.Delete(StateCookie, cookieOptions);
Response.Cookies.Delete(Options.StateCookie.Name, cookieOptions);
Copy link
Member

Choose a reason for hiding this comment

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

I'm trying to think if a user would Build their options any differently if they knew they were building a delete cookie.

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