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

Commit 314b0d8

Browse files
committed
AntiForgery Interfaces.
Also contains some code ported over. This Commit is only for review purpose.
1 parent 1536daa commit 314b0d8

18 files changed

+1075
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2+
3+
using System;
4+
using System.ComponentModel;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Security.Claims;
7+
using System.Security.Principal;
8+
using Microsoft.AspNet.Abstractions;
9+
using Microsoft.AspNet.Mvc.Rendering;
10+
using Microsoft.AspNet.Security.DataProtection;
11+
12+
namespace Microsoft.AspNet.Mvc
13+
{
14+
/// <summary>
15+
/// Provides access to the anti-forgery system, which provides protection against
16+
/// Cross-site Request Forgery (XSRF, also called CSRF) attacks.
17+
/// </summary>
18+
public class AntiForgery
19+
{
20+
private static readonly AntiForgeryWorker _worker = CreateSingletonAntiForgeryWorker();
21+
private static readonly string _purpose = "Microsoft.AspNet.Mvc.AntiXsrf.AntiForgeryToken.v1" ;
22+
23+
private static AntiForgeryWorker CreateSingletonAntiForgeryWorker()
24+
{
25+
// initialize the dependency chain
26+
IAntiForgeryConfig config = new AntiForgeryConfigWrapper();
27+
28+
// TODO populate the IDataProtectionProvider using DI.
29+
IDataProtectionProvider dataProtectionProvider = DataProtectionProvider.CreateNew();
30+
IAntiForgeryTokenSerializer serializer = new AntiForgeryTokenSerializer(dataProtectionProvider.CreateProtector(_purpose));
31+
ITokenStore tokenStore = new AntiForgeryTokenStore(config, serializer);
32+
IClaimUidExtractor claimUidExtractor = new DefaultClaimUidExtractor(config);
33+
var tokenProvider = new TokenValidator(config, claimUidExtractor);
34+
35+
return new AntiForgeryWorker(serializer, config, tokenStore, tokenProvider, tokenProvider);
36+
}
37+
38+
/// <summary>
39+
/// Generates an anti-forgery token for this request. This token can
40+
/// be validated by calling the Validate() method.
41+
/// </summary>
42+
/// <returns>An HTML string corresponding to an &lt;input type="hidden"&gt;
43+
/// element. This element should be put inside a &lt;form&gt;.</returns>
44+
/// <remarks>
45+
/// This method has a side effect: it may set a response cookie.
46+
/// </remarks>
47+
public static HtmlString GetHtml(HttpContext context)
48+
{
49+
TagBuilder retVal = _worker.GetFormInputElement(context);
50+
return retVal.ToHtmlString(TagRenderMode.SelfClosing);
51+
}
52+
53+
/// <summary>
54+
/// Generates an anti-forgery token pair (cookie and form token) for this request.
55+
/// This method is similar to GetHtml(), but this method gives the caller control
56+
/// over how to persist the returned values. To validate these tokens, call the
57+
/// appropriate overload of Validate.
58+
/// </summary>
59+
/// <param name="oldCookieToken">The anti-forgery token - if any - that already existed
60+
/// for this request. May be null. The anti-forgery system will try to reuse this cookie
61+
/// value when generating a matching form token.</param>
62+
/// <param name="newCookieToken">Will contain a new cookie value if the old cookie token
63+
/// was null or invalid. If this value is non-null when the method completes, the caller
64+
/// must persist this value in the form of a response cookie, and the existing cookie value
65+
/// should be discarded. If this value is null when the method completes, the existing
66+
/// cookie value was valid and needn't be modified.</param>
67+
/// <param name="formToken">The value that should be stored in the &lt;form&gt;. The caller
68+
/// should take care not to accidentally swap the cookie and form tokens.</param>
69+
/// <remarks>
70+
/// Unlike the GetHtml() method, this method has no side effect. The caller
71+
/// is responsible for setting the response cookie and injecting the returned
72+
/// form token as appropriate.
73+
/// </remarks>
74+
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "1#",
75+
Justification = "Method is intended for advanced audiences.")]
76+
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "2#",
77+
Justification = "Method is intended for advanced audiences.")]
78+
[EditorBrowsable(EditorBrowsableState.Advanced)]
79+
public static void GetTokens(HttpContext context, string oldCookieToken, out string newCookieToken, out string formToken)
80+
{
81+
_worker.GetTokens(context, oldCookieToken, out newCookieToken, out formToken);
82+
}
83+
84+
/// <summary>
85+
/// Validates an anti-forgery token that was supplied for this request.
86+
/// The anti-forgery token may be generated by calling GetHtml().
87+
/// </summary>
88+
/// <remarks>
89+
/// Throws an HttpAntiForgeryException if validation fails.
90+
/// </remarks>
91+
public static void Validate(HttpContext context)
92+
{
93+
_worker.Validate(context);
94+
}
95+
96+
/// <summary>
97+
/// Validates an anti-forgery token pair that was generated by the GetTokens method.
98+
/// </summary>
99+
/// <param name="cookieToken">The token that was supplied in the request cookie.</param>
100+
/// <param name="formToken">The token that was supplied in the request form body.</param>
101+
/// <remarks>
102+
/// Throws an HttpAntiForgeryException if validation fails.
103+
/// </remarks>
104+
[EditorBrowsable(EditorBrowsableState.Advanced)]
105+
public static void Validate(HttpContext context, string cookieToken, string formToken)
106+
{
107+
_worker.Validate(context, cookieToken, formToken);
108+
}
109+
}
110+
}
111+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2+
3+
using System.ComponentModel;
4+
using System.Text;
5+
6+
namespace Microsoft.AspNet.Mvc
7+
{
8+
/// <summary>
9+
/// Provides programmatic configuration for the anti-forgery token system.
10+
/// </summary>
11+
public static class AntiForgeryConfig
12+
{
13+
internal const string AntiForgeryTokenFieldName = "__RequestVerificationToken";
14+
15+
private static string _cookieName;
16+
private static string _uniqueClaimTypeIdentifier;
17+
18+
/// <summary>
19+
/// Specifies an object that can provide additional data to put into all
20+
/// generated tokens and that can validate additional data in incoming
21+
/// tokens.
22+
/// </summary>
23+
public static IAntiForgeryAdditionalDataProvider AdditionalDataProvider
24+
{
25+
get;
26+
set;
27+
}
28+
29+
/// <summary>
30+
/// Specifies the name of the cookie that is used by the anti-forgery
31+
/// system.
32+
/// </summary>
33+
/// <remarks>
34+
/// If an explicit name is not provided, the system will automatically
35+
/// generate a name.
36+
/// </remarks>
37+
public static string CookieName
38+
{
39+
get
40+
{
41+
if (_cookieName == null)
42+
{
43+
_cookieName = GetAntiForgeryCookieName();
44+
}
45+
return _cookieName;
46+
}
47+
set
48+
{
49+
_cookieName = value;
50+
}
51+
}
52+
53+
/// <summary>
54+
/// Specifies whether SSL is required for the anti-forgery system
55+
/// to operate. If this setting is 'true' and a non-SSL request
56+
/// comes into the system, all anti-forgery APIs will fail.
57+
/// </summary>
58+
public static bool RequireSsl
59+
{
60+
get;
61+
set;
62+
}
63+
64+
/// <summary>
65+
/// Specifies whether to suppress the generation of X-Frame-Options header
66+
/// which is used to prevent ClickJacking. By default, the X-Frame-Options
67+
/// header is generated with the value SAMEORIGIN. If this setting is 'true',
68+
/// the X-Frame-Options header will not be generated for the response.
69+
/// </summary>
70+
public static bool SuppressXFrameOptionsHeader
71+
{
72+
get;
73+
set;
74+
}
75+
76+
/// <summary>
77+
/// Specifies whether the anti-forgery system should skip checking
78+
/// for conditions that might indicate misuse of the system. Please
79+
/// use caution when setting this switch, as improper use could open
80+
/// security holes in the application.
81+
/// </summary>
82+
/// <remarks>
83+
/// Setting this switch will disable several checks, including:
84+
/// - Identity.IsAuthenticated = true without Identity.Name being set
85+
/// - special-casing claims-based identities
86+
/// </remarks>
87+
[EditorBrowsable(EditorBrowsableState.Never)]
88+
public static bool SuppressIdentityHeuristicChecks
89+
{
90+
get;
91+
set;
92+
}
93+
94+
// TODO: Replace the stub.
95+
private static string GetAntiForgeryCookieName()
96+
{
97+
return AntiForgeryTokenFieldName;
98+
}
99+
}
100+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2+
3+
namespace Microsoft.AspNet.Mvc
4+
{
5+
internal sealed class AntiForgeryConfigWrapper : IAntiForgeryConfig
6+
{
7+
public IAntiForgeryAdditionalDataProvider AdditionalDataProvider
8+
{
9+
get
10+
{
11+
return AntiForgeryConfig.AdditionalDataProvider;
12+
}
13+
}
14+
15+
public string CookieName
16+
{
17+
get { return AntiForgeryConfig.CookieName; }
18+
}
19+
20+
public string FormFieldName
21+
{
22+
get { return AntiForgeryConfig.AntiForgeryTokenFieldName; }
23+
}
24+
25+
public bool RequireSSL
26+
{
27+
get { return AntiForgeryConfig.RequireSsl; }
28+
}
29+
30+
public bool SuppressIdentityHeuristicChecks
31+
{
32+
get { return AntiForgeryConfig.SuppressIdentityHeuristicChecks; }
33+
}
34+
35+
public bool SuppressXFrameOptionsHeader
36+
{
37+
get { return AntiForgeryConfig.SuppressXFrameOptionsHeader; }
38+
}
39+
}
40+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.AspNet.Mvc
8+
{
9+
public class AntiForgeryToken
10+
{
11+
internal const int SecurityTokenBitLength = 128;
12+
internal const int ClaimUidBitLength = 256;
13+
14+
private string _additionalData;
15+
private BinaryBlob _securityToken;
16+
private string _username;
17+
18+
public string AdditionalData
19+
{
20+
get
21+
{
22+
return _additionalData ?? String.Empty;
23+
}
24+
set
25+
{
26+
_additionalData = value;
27+
}
28+
}
29+
30+
public BinaryBlob ClaimUid { get; set; }
31+
32+
public bool IsSessionToken { get; set; }
33+
34+
public BinaryBlob SecurityToken
35+
{
36+
get
37+
{
38+
if (_securityToken == null)
39+
{
40+
_securityToken = new BinaryBlob(SecurityTokenBitLength);
41+
}
42+
return _securityToken;
43+
}
44+
set
45+
{
46+
_securityToken = value;
47+
}
48+
}
49+
50+
public string Username
51+
{
52+
get
53+
{
54+
return _username ?? String.Empty;
55+
}
56+
set
57+
{
58+
_username = value;
59+
}
60+
}
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2+
3+
using System;
4+
using Microsoft.AspNet.Security.DataProtection;
5+
6+
namespace Microsoft.AspNet.Mvc
7+
{
8+
// TODO: Stub :Replace with actual implementation
9+
internal sealed class AntiForgeryTokenSerializer : IAntiForgeryTokenSerializer
10+
{
11+
private readonly IDataProtector _cryptoSystem;
12+
13+
internal AntiForgeryTokenSerializer(IDataProtector cryptoSystem)
14+
{
15+
_cryptoSystem = cryptoSystem;
16+
}
17+
18+
public AntiForgeryToken Deserialize(string serializedToken)
19+
{
20+
throw new NotImplementedException();
21+
}
22+
23+
public string Serialize(AntiForgeryToken token)
24+
{
25+
throw new NotImplementedException();
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)