This repository was archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 598
Add new CookiePolicy middleware #452
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c51207e
Add new CookiePolicy middleware
HaoK 8d7a28f
Fix doc comments
HaoK e92c41c
Add a map test
HaoK 9464816
Use map test for everything
HaoK b97bed9
Nuke comment
HaoK 5219539
Update comment
HaoK d65aa60
CR feedback
HaoK 507f349
Wrap feature
HaoK 41b4039
Add test for cookie feature wrapping
HaoK aa9a68b
Remove review comment
HaoK 5b6774c
Remove options dependency
HaoK bf4fe44
Refactor tests
HaoK a7f0ba9
Clean up tests
HaoK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNet.Http; | ||
|
||
namespace Microsoft.AspNet.CookiePolicy | ||
{ | ||
public class AppendCookieContext | ||
{ | ||
public AppendCookieContext(HttpContext context, CookieOptions options, string name, string value) | ||
{ | ||
Context = context; | ||
CookieOptions = options; | ||
CookieName = name; | ||
CookieValue = value; | ||
} | ||
|
||
public HttpContext Context { get; } | ||
public CookieOptions CookieOptions { get; } | ||
public string CookieName { get; set; } | ||
public string CookieValue { get; set; } | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Microsoft.AspNet.CookiePolicy/CookiePolicyAppBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNet.CookiePolicy; | ||
|
||
namespace Microsoft.AspNet.Builder | ||
{ | ||
/// <summary> | ||
/// Extension methods provided by the cookie policy middleware | ||
/// </summary> | ||
public static class CookiePolicyAppBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a cookie policy middleware to your web application pipeline. | ||
/// </summary> | ||
/// <param name="app">The IApplicationBuilder passed to your configuration method</param> | ||
/// <param name="options">The options for the middleware</param> | ||
/// <returns>The original app parameter</returns> | ||
public static IApplicationBuilder UseCookiePolicy(this IApplicationBuilder app, CookiePolicyOptions options) | ||
{ | ||
return app.UseMiddleware<CookiePolicyMiddleware>(options); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a cookie policy middleware to your web application pipeline. | ||
/// </summary> | ||
/// <param name="app">The IApplicationBuilder passed to your configuration method</param> | ||
/// <param name="configureOptions">Used to configure the options for the middleware</param> | ||
/// <returns>The original app parameter</returns> | ||
public static IApplicationBuilder UseCookiePolicy(this IApplicationBuilder app, Action<CookiePolicyOptions> configureOptions) | ||
{ | ||
var options = new CookiePolicyOptions(); | ||
if (configureOptions != null) | ||
{ | ||
configureOptions(options); | ||
} | ||
return app.UseCookiePolicy(options); | ||
} | ||
} | ||
} |
167 changes: 167 additions & 0 deletions
167
src/Microsoft.AspNet.CookiePolicy/CookiePolicyMiddleware.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNet.Builder; | ||
using Microsoft.AspNet.Http; | ||
using Microsoft.AspNet.Http.Features; | ||
using Microsoft.AspNet.Http.Features.Internal; | ||
|
||
namespace Microsoft.AspNet.CookiePolicy | ||
{ | ||
public class CookiePolicyMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public CookiePolicyMiddleware( | ||
RequestDelegate next, | ||
CookiePolicyOptions options) | ||
{ | ||
Options = options; | ||
_next = next; | ||
} | ||
|
||
public CookiePolicyOptions Options { get; set; } | ||
|
||
public Task Invoke(HttpContext context) | ||
{ | ||
var feature = context.Features.Get<IResponseCookiesFeature>() ?? new ResponseCookiesFeature(context.Features); | ||
context.Features.Set<IResponseCookiesFeature>(new CookiesWrapperFeature(context, Options, feature)); | ||
return _next(context); | ||
} | ||
|
||
private class CookiesWrapperFeature : IResponseCookiesFeature | ||
{ | ||
public CookiesWrapperFeature(HttpContext context, CookiePolicyOptions options, IResponseCookiesFeature feature) | ||
{ | ||
Wrapper = new CookiesWrapper(context, options, feature); | ||
} | ||
|
||
public IResponseCookies Wrapper { get; } | ||
|
||
public IResponseCookies Cookies | ||
{ | ||
get | ||
{ | ||
return Wrapper; | ||
} | ||
} | ||
} | ||
|
||
private class CookiesWrapper : IResponseCookies | ||
{ | ||
public CookiesWrapper(HttpContext context, CookiePolicyOptions options, IResponseCookiesFeature feature) | ||
{ | ||
Context = context; | ||
Feature = feature; | ||
Policy = options; | ||
} | ||
|
||
public HttpContext Context { get; } | ||
|
||
public IResponseCookiesFeature Feature { get; } | ||
|
||
public IResponseCookies Cookies | ||
{ | ||
get | ||
{ | ||
return Feature.Cookies; | ||
} | ||
} | ||
|
||
public CookiePolicyOptions Policy { get; } | ||
|
||
private bool PolicyRequiresCookieOptions() | ||
{ | ||
return Policy.HttpOnly != HttpOnlyPolicy.None || Policy.Secure != SecurePolicy.None; | ||
} | ||
|
||
public void Append(string key, string value) | ||
{ | ||
if (PolicyRequiresCookieOptions() || Policy.OnAppendCookie != null) | ||
{ | ||
Append(key, value, new CookieOptions()); | ||
} | ||
else | ||
{ | ||
Cookies.Append(key, value); | ||
} | ||
} | ||
|
||
public void Append(string key, string value, CookieOptions options) | ||
{ | ||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
ApplyPolicy(options); | ||
if (Policy.OnAppendCookie != null) | ||
{ | ||
var context = new AppendCookieContext(Context, options, key, value); | ||
Policy.OnAppendCookie(context); | ||
key = context.CookieName; | ||
value = context.CookieValue; | ||
} | ||
Cookies.Append(key, value, options); | ||
} | ||
|
||
public void Delete(string key) | ||
{ | ||
if (PolicyRequiresCookieOptions() || Policy.OnDeleteCookie != null) | ||
{ | ||
Delete(key, new CookieOptions()); | ||
} | ||
else | ||
{ | ||
Cookies.Delete(key); | ||
} | ||
} | ||
|
||
public void Delete(string key, CookieOptions options) | ||
{ | ||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
ApplyPolicy(options); | ||
if (Policy.OnDeleteCookie != null) | ||
{ | ||
var context = new DeleteCookieContext(Context, options, key); | ||
Policy.OnDeleteCookie(context); | ||
key = context.CookieName; | ||
} | ||
Cookies.Delete(key, options); | ||
} | ||
|
||
private void ApplyPolicy(CookieOptions options) | ||
{ | ||
switch (Policy.Secure) | ||
{ | ||
case SecurePolicy.Always: | ||
options.Secure = true; | ||
break; | ||
case SecurePolicy.SameAsRequest: | ||
options.Secure = Context.Request.IsHttps; | ||
break; | ||
case SecurePolicy.None: | ||
break; | ||
default: | ||
throw new InvalidOperationException(); | ||
} | ||
switch (Policy.HttpOnly) | ||
{ | ||
case HttpOnlyPolicy.Always: | ||
options.HttpOnly = true; | ||
break; | ||
case HttpOnlyPolicy.None: | ||
break; | ||
default: | ||
throw new InvalidOperationException(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default throw? |
||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.AspNet.CookiePolicy | ||
{ | ||
public class CookiePolicyOptions | ||
{ | ||
public HttpOnlyPolicy HttpOnly { get; set; } = HttpOnlyPolicy.None; | ||
public SecurePolicy Secure { get; set; } = SecurePolicy.None; | ||
|
||
public Action<AppendCookieContext> OnAppendCookie { get; set; } | ||
public Action<DeleteCookieContext> OnDeleteCookie { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNet.Http; | ||
|
||
namespace Microsoft.AspNet.CookiePolicy | ||
{ | ||
public class DeleteCookieContext | ||
{ | ||
public DeleteCookieContext(HttpContext context, CookieOptions options, string name) | ||
{ | ||
Context = context; | ||
CookieOptions = options; | ||
CookieName = name; | ||
} | ||
|
||
public HttpContext Context { get; } | ||
public CookieOptions CookieOptions { get; } | ||
public string CookieName { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNet.CookiePolicy | ||
{ | ||
public enum HttpOnlyPolicy | ||
{ | ||
None, | ||
Always | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Microsoft.AspNet.CookiePolicy/Microsoft.AspNet.CookiePolicy.xproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>86183dc3-02a8-4a68-8b60-71ecec066e79</ProjectGuid> | ||
<RootNamespace>Microsoft.AspNet.CookiePolicy</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Reflection; | ||
using System.Resources; | ||
|
||
[assembly: AssemblyMetadata("Serviceable", "True")] | ||
[assembly: NeutralResourcesLanguage("en-us")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNet.CookiePolicy | ||
{ | ||
public enum SecurePolicy | ||
{ | ||
None, | ||
Always, | ||
SameAsRequest | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
argument validation?
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.
There didn't appear to be any argument validation in the base HttpAbstractions ResponseCookies so I just mimiced that behavior since we are just wrapping no?
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.
There's at least a NotNull check on options.