@@ -13,8 +13,9 @@ namespace DotNetOpenAuth.OAuth2 {
1313 using System . Net . Http ;
1414 using System . Security ;
1515 using System . Text ;
16+ using System . Threading ;
17+ using System . Threading . Tasks ;
1618 using System . Xml ;
17-
1819 using DotNetOpenAuth . Messaging ;
1920 using DotNetOpenAuth . OAuth2 . ChannelElements ;
2021 using DotNetOpenAuth . OAuth2 . Messages ;
@@ -116,11 +117,11 @@ public static void AuthorizeRequest(WebHeaderCollection requestHeaders, string a
116117 /// </summary>
117118 /// <param name="request">The request for protected resources from the service provider.</param>
118119 /// <param name="authorization">The authorization for this request previously obtained via OAuth.</param>
119- public void AuthorizeRequest ( HttpWebRequest request , IAuthorizationState authorization ) {
120+ public Task AuthorizeRequestAsync ( HttpWebRequest request , IAuthorizationState authorization , CancellationToken cancellationToken ) {
120121 Requires . NotNull ( request , "request" ) ;
121122 Requires . NotNull ( authorization , "authorization" ) ;
122123
123- this . AuthorizeRequest ( request . Headers , authorization ) ;
124+ return this . AuthorizeRequestAsync ( request . Headers , authorization , cancellationToken ) ;
124125 }
125126
126127 /// <summary>
@@ -129,15 +130,15 @@ public void AuthorizeRequest(HttpWebRequest request, IAuthorizationState authori
129130 /// </summary>
130131 /// <param name="requestHeaders">The headers on the request for protected resources from the service provider.</param>
131132 /// <param name="authorization">The authorization for this request previously obtained via OAuth.</param>
132- public void AuthorizeRequest ( WebHeaderCollection requestHeaders , IAuthorizationState authorization ) {
133+ public async Task AuthorizeRequestAsync ( WebHeaderCollection requestHeaders , IAuthorizationState authorization , CancellationToken cancellationToken ) {
133134 Requires . NotNull ( requestHeaders , "requestHeaders" ) ;
134135 Requires . NotNull ( authorization , "authorization" ) ;
135136 Requires . That ( ! string . IsNullOrEmpty ( authorization . AccessToken ) , "authorization" , "AccessToken required." ) ;
136137 ErrorUtilities . VerifyProtocol ( ! authorization . AccessTokenExpirationUtc . HasValue || authorization . AccessTokenExpirationUtc >= DateTime . UtcNow || authorization . RefreshToken != null , ClientStrings . AuthorizationExpired ) ;
137138
138139 if ( authorization . AccessTokenExpirationUtc . HasValue && authorization . AccessTokenExpirationUtc . Value < DateTime . UtcNow ) {
139140 ErrorUtilities . VerifyProtocol ( authorization . RefreshToken != null , ClientStrings . AccessTokenRefreshFailed ) ;
140- this . RefreshAuthorization ( authorization ) ;
141+ await this . RefreshAuthorizationAsync ( authorization , cancellationToken : cancellationToken ) ;
141142 }
142143
143144 AuthorizeRequest ( requestHeaders , authorization . AccessToken ) ;
@@ -180,7 +181,7 @@ public DelegatingHandler CreateAuthorizingHandler(IAuthorizationState authorizat
180181 /// the <paramref name="authorization"/> parameter if the authorization server has cycled out your refresh token.
181182 /// If the parameter value was updated, this method calls <see cref="IAuthorizationState.SaveChanges"/> on that instance.
182183 /// </remarks>
183- public bool RefreshAuthorization ( IAuthorizationState authorization , TimeSpan ? skipIfUsefulLifeExceeds = null ) {
184+ public async Task < bool > RefreshAuthorizationAsync ( IAuthorizationState authorization , TimeSpan ? skipIfUsefulLifeExceeds = null , CancellationToken cancellationToken = default ( CancellationToken ) ) {
184185 Requires . NotNull ( authorization , "authorization" ) ;
185186 Requires . That ( ! string . IsNullOrEmpty ( authorization . RefreshToken ) , "authorization" , "RefreshToken required." ) ;
186187
@@ -200,7 +201,7 @@ public bool RefreshAuthorization(IAuthorizationState authorization, TimeSpan? sk
200201
201202 this . ApplyClientCredential ( request ) ;
202203
203- var response = this . Channel . Request < AccessTokenSuccessResponse > ( request ) ;
204+ var response = await this . Channel . RequestAsync < AccessTokenSuccessResponse > ( request , cancellationToken ) ;
204205 UpdateAuthorizationWithResponse ( authorization , response ) ;
205206 return true ;
206207 }
@@ -216,7 +217,7 @@ public bool RefreshAuthorization(IAuthorizationState authorization, TimeSpan? sk
216217 /// If the return value includes a new refresh token, the old refresh token should be discarded and
217218 /// replaced with the new one.
218219 /// </remarks>
219- public IAuthorizationState GetScopedAccessToken ( string refreshToken , HashSet < string > scope ) {
220+ public async Task < IAuthorizationState > GetScopedAccessTokenAsync ( string refreshToken , HashSet < string > scope , CancellationToken cancellationToken ) {
220221 Requires . NotNullOrEmpty ( refreshToken , "refreshToken" ) ;
221222 Requires . NotNull ( scope , "scope" ) ;
222223
@@ -227,7 +228,7 @@ public IAuthorizationState GetScopedAccessToken(string refreshToken, HashSet<str
227228
228229 this . ApplyClientCredential ( request ) ;
229230
230- var response = this . Channel . Request < AccessTokenSuccessResponse > ( request ) ;
231+ var response = await this . Channel . RequestAsync < AccessTokenSuccessResponse > ( request , cancellationToken ) ;
231232 var authorization = new AuthorizationState ( ) ;
232233 UpdateAuthorizationWithResponse ( authorization , response ) ;
233234
@@ -241,7 +242,7 @@ public IAuthorizationState GetScopedAccessToken(string refreshToken, HashSet<str
241242 /// <param name="password">The resource owner's account password.</param>
242243 /// <param name="scopes">The desired scope of access.</param>
243244 /// <returns>The result, containing the tokens if successful.</returns>
244- public IAuthorizationState ExchangeUserCredentialForToken ( string userName , string password , IEnumerable < string > scopes = null ) {
245+ public Task < IAuthorizationState > ExchangeUserCredentialForTokenAsync ( string userName , string password , IEnumerable < string > scopes = null , CancellationToken cancellationToken = default ( CancellationToken ) ) {
245246 Requires . NotNullOrEmpty ( userName , "userName" ) ;
246247 Requires . NotNull ( password , "password" ) ;
247248
@@ -250,17 +251,17 @@ public IAuthorizationState ExchangeUserCredentialForToken(string userName, strin
250251 Password = password ,
251252 } ;
252253
253- return this . RequestAccessToken ( request , scopes ) ;
254+ return this . RequestAccessTokenAsync ( request , scopes , cancellationToken ) ;
254255 }
255256
256257 /// <summary>
257258 /// Obtains an access token for accessing client-controlled resources on the resource server.
258259 /// </summary>
259260 /// <param name="scopes">The desired scopes.</param>
260261 /// <returns>The result of the authorization request.</returns>
261- public IAuthorizationState GetClientAccessToken ( IEnumerable < string > scopes = null ) {
262+ public Task < IAuthorizationState > GetClientAccessTokenAsync ( IEnumerable < string > scopes = null , CancellationToken cancellationToken = default ( CancellationToken ) ) {
262263 var request = new AccessTokenClientCredentialsRequest ( this . AuthorizationServer . TokenEndpoint , this . AuthorizationServer . Version ) ;
263- return this . RequestAccessToken ( request , scopes ) ;
264+ return this . RequestAccessTokenAsync ( request , scopes , cancellationToken ) ;
264265 }
265266
266267 /// <summary>
@@ -322,7 +323,7 @@ internal static void UpdateAuthorizationWithResponse(IAuthorizationState authori
322323 /// </summary>
323324 /// <param name="authorizationState">The authorization state to update.</param>
324325 /// <param name="authorizationSuccess">The authorization success message obtained from the authorization server.</param>
325- internal void UpdateAuthorizationWithResponse ( IAuthorizationState authorizationState , EndUserAuthorizationSuccessAuthCodeResponse authorizationSuccess ) {
326+ internal async Task UpdateAuthorizationWithResponseAsync ( IAuthorizationState authorizationState , EndUserAuthorizationSuccessAuthCodeResponse authorizationSuccess , CancellationToken cancellationToken ) {
326327 Requires . NotNull ( authorizationState , "authorizationState" ) ;
327328 Requires . NotNull ( authorizationSuccess , "authorizationSuccess" ) ;
328329
@@ -332,7 +333,7 @@ internal void UpdateAuthorizationWithResponse(IAuthorizationState authorizationS
332333 AuthorizationCode = authorizationSuccess . AuthorizationCode ,
333334 } ;
334335 this . ApplyClientCredential ( accessTokenRequest ) ;
335- IProtocolMessage accessTokenResponse = this . Channel . Request ( accessTokenRequest ) ;
336+ IProtocolMessage accessTokenResponse = await this . Channel . RequestAsync ( accessTokenRequest , cancellationToken ) ;
336337 var accessTokenSuccess = accessTokenResponse as AccessTokenSuccessResponse ;
337338 var failedAccessTokenResponse = accessTokenResponse as AccessTokenFailedResponse ;
338339 if ( accessTokenSuccess != null ) {
@@ -388,7 +389,7 @@ private static double ProportionalLifeRemaining(IAuthorizationState authorizatio
388389 /// <param name="request">The request message.</param>
389390 /// <param name="scopes">The scopes requested by the client.</param>
390391 /// <returns>The result of the request.</returns>
391- private IAuthorizationState RequestAccessToken ( ScopedAccessTokenRequest request , IEnumerable < string > scopes = null ) {
392+ private async Task < IAuthorizationState > RequestAccessTokenAsync ( ScopedAccessTokenRequest request , IEnumerable < string > scopes , CancellationToken cancellationToken ) {
392393 Requires . NotNull ( request , "request" ) ;
393394
394395 var authorizationState = new AuthorizationState ( scopes ) ;
@@ -397,7 +398,7 @@ private IAuthorizationState RequestAccessToken(ScopedAccessTokenRequest request,
397398 this . ApplyClientCredential ( request ) ;
398399 request . Scope . UnionWith ( authorizationState . Scope ) ;
399400
400- var response = this . Channel . Request ( request ) ;
401+ var response = await this . Channel . RequestAsync ( request , cancellationToken ) ;
401402 var success = response as AccessTokenSuccessResponse ;
402403 var failure = response as AccessTokenFailedResponse ;
403404 ErrorUtilities . VerifyProtocol ( success != null || failure != null , MessagingStrings . UnexpectedMessageReceivedOfMany ) ;
0 commit comments