22using System ;
33using System . Diagnostics . CodeAnalysis ;
44using System . Diagnostics . Contracts ;
5- using System . Security . Principal ;
5+ using System . Security . Claims ;
6+ using System . Threading . Tasks ;
67using Microsoft . AspNet . Abstractions ;
78using Microsoft . AspNet . Mvc . Core ;
89using Microsoft . AspNet . Mvc . Rendering ;
9- using System . Security . Claims ;
10- using System . Threading . Tasks ;
1110
1211namespace Microsoft . AspNet . Mvc
1312{
@@ -19,7 +18,11 @@ internal sealed class AntiForgeryWorker
1918 private readonly ITokenValidator _validator ;
2019 private readonly ITokenGenerator _generator ;
2120
22- internal AntiForgeryWorker ( IAntiForgeryTokenSerializer serializer , IAntiForgeryConfig config , ITokenStore tokenStore , ITokenGenerator generator , ITokenValidator validator )
21+ internal AntiForgeryWorker ( [ NotNull ] IAntiForgeryTokenSerializer serializer ,
22+ [ NotNull ] IAntiForgeryConfig config ,
23+ [ NotNull ] ITokenStore tokenStore ,
24+ [ NotNull ] ITokenGenerator generator ,
25+ [ NotNull ] ITokenValidator validator )
2326 {
2427 _serializer = serializer ;
2528 _config = config ;
@@ -62,12 +65,12 @@ private static ClaimsIdentity ExtractIdentity(HttpContext httpContext)
6265 if ( httpContext != null )
6366 {
6467 ClaimsPrincipal user = httpContext . User ;
65-
68+
6669 if ( user != null )
6770 {
6871 // We only support ClaimsIdentity.
6972 // Todo remove this once httpContext.User moves to ClaimsIdentity.
70- return user . Identity as ClaimsIdentity ;
73+ return user . Identity as ClaimsIdentity ;
7174 }
7275 }
7376
@@ -93,14 +96,14 @@ private AntiForgeryToken GetCookieTokenNoThrow(HttpContext httpContext)
9396 // value is the hidden input form element that should be rendered in
9497 // the <form>. This method has a side effect: it may set a response
9598 // cookie.
96- public TagBuilder GetFormInputElement ( HttpContext httpContext )
99+ public TagBuilder GetFormInputElement ( [ NotNull ] HttpContext httpContext )
97100 {
98101 CheckSSLConfig ( httpContext ) ;
99102
100- AntiForgeryToken oldCookieToken = GetCookieTokenNoThrow ( httpContext ) ;
101- AntiForgeryToken newCookieToken , formToken ;
102- GetTokens ( httpContext , oldCookieToken , out newCookieToken , out formToken ) ;
103-
103+ var oldCookieToken = GetCookieTokenNoThrow ( httpContext ) ;
104+ var tokenSet = GetTokens ( httpContext , oldCookieToken ) ;
105+ var newCookieToken = tokenSet . CookieToken ;
106+ var formToken = tokenSet . FormToken ;
104107 if ( newCookieToken != null )
105108 {
106109 // If a new cookie was generated, persist it.
@@ -129,29 +132,39 @@ public TagBuilder GetFormInputElement(HttpContext httpContext)
129132 // 'new cookie value' out param is non-null, the caller *must* persist
130133 // the new value to cookie storage since the original value was null or
131134 // invalid. This method is side-effect free.
132- public void GetTokens ( HttpContext httpContext , string serializedOldCookieToken , out string serializedNewCookieToken , out string serializedFormToken )
135+ public AntiForgeryTokenSet GetTokens ( [ NotNull ] HttpContext httpContext , string serializedOldCookieToken )
133136 {
134137 CheckSSLConfig ( httpContext ) ;
135-
136138 AntiForgeryToken oldCookieToken = DeserializeTokenNoThrow ( serializedOldCookieToken ) ;
137139 AntiForgeryToken newCookieToken , formToken ;
138- GetTokens ( httpContext , oldCookieToken , out newCookieToken , out formToken ) ;
140+ var tokenSet = GetTokens ( httpContext , oldCookieToken ) ;
139141
140- serializedNewCookieToken = Serialize ( newCookieToken ) ;
141- serializedFormToken = Serialize ( formToken ) ;
142+ var serializedNewCookieToken = Serialize ( tokenSet . CookieToken ) ;
143+ var serializedFormToken = Serialize ( tokenSet . FormToken ) ;
144+ return new AntiForgeryTokenSet ( serializedFormToken , serializedNewCookieToken ) ;
142145 }
143146
144- private void GetTokens ( HttpContext httpContext , AntiForgeryToken oldCookieToken , out AntiForgeryToken newCookieToken , out AntiForgeryToken formToken )
147+ private AntiForgeryTokenSetInternal GetTokens ( HttpContext httpContext , AntiForgeryToken oldCookieToken )
145148 {
146- newCookieToken = null ;
149+ AntiForgeryToken newCookieToken = null ;
147150 if ( ! _validator . IsCookieTokenValid ( oldCookieToken ) )
148151 {
149152 // Need to make sure we're always operating with a good cookie token.
150153 oldCookieToken = newCookieToken = _generator . GenerateCookieToken ( ) ;
151154 }
152155
153156 Contract . Assert ( _validator . IsCookieTokenValid ( oldCookieToken ) ) ;
154- formToken = _generator . GenerateFormToken ( httpContext , ExtractIdentity ( httpContext ) , oldCookieToken ) ;
157+
158+ AntiForgeryToken formToken = _generator .
159+ GenerateFormToken ( httpContext ,
160+ ExtractIdentity ( httpContext ) ,
161+ oldCookieToken ) ;
162+
163+ return new AntiForgeryTokenSetInternal ( )
164+ {
165+ CookieToken = newCookieToken ,
166+ FormToken = formToken
167+ } ;
155168 }
156169
157170 private string Serialize ( AntiForgeryToken token )
@@ -162,7 +175,7 @@ private string Serialize(AntiForgeryToken token)
162175 // [ ENTRY POINT ]
163176 // Given an HttpContext, validates that the anti-XSRF tokens contained
164177 // in the cookies & form are OK for this request.
165- public async Task ValidateAsync ( HttpContext httpContext )
178+ public async Task ValidateAsync ( [ NotNull ] HttpContext httpContext )
166179 {
167180 CheckSSLConfig ( httpContext ) ;
168181
@@ -177,7 +190,7 @@ public async Task ValidateAsync(HttpContext httpContext)
177190 // [ ENTRY POINT ]
178191 // Given the serialized string representations of a cookie & form token,
179192 // validates that the pair is OK for this request.
180- public void Validate ( HttpContext httpContext , string cookieToken , string formToken )
193+ public void Validate ( [ NotNull ] HttpContext httpContext , string cookieToken , string formToken )
181194 {
182195 CheckSSLConfig ( httpContext ) ;
183196
@@ -188,5 +201,12 @@ public void Validate(HttpContext httpContext, string cookieToken, string formTok
188201 // Validate
189202 _validator . ValidateTokens ( httpContext , ExtractIdentity ( httpContext ) , deserializedCookieToken , deserializedFormToken ) ;
190203 }
204+
205+ private class AntiForgeryTokenSetInternal
206+ {
207+ public AntiForgeryToken FormToken { get ; set ; }
208+
209+ public AntiForgeryToken CookieToken { get ; set ; }
210+ }
191211 }
192212}
0 commit comments