|
| 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 <input type="hidden"> |
| 43 | + /// element. This element should be put inside a <form>.</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 <form>. 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 | + |
0 commit comments