OLD | NEW |
1 /* | 1 /* |
2 Copyright 2011 Google Inc | 2 Copyright 2011 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0(the "License"); | 4 Licensed under the Apache License, Version 2.0(the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
11 distributed under the License is distributed on an "AS IS" BASIS, | 11 distributed under the License is distributed on an "AS IS" BASIS, |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 See the License for the specific language governing permissions and | 13 See the License for the specific language governing permissions and |
14 limitations under the License. | 14 limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 using System; | 17 using System; |
18 using System.Net; | 18 using System.Net; |
19 using System.Net.Http; | 19 using System.Net.Http; |
| 20 using System.Threading.Tasks; |
20 | 21 |
21 using DotNetOpenAuth.OAuth2; | 22 using DotNetOpenAuth.OAuth2; |
22 | 23 |
23 using Google.Apis.Http; | 24 using Google.Apis.Http; |
24 using Google.Apis.Requests; | 25 using Google.Apis.Requests; |
25 using Google.Apis.Util; | 26 using Google.Apis.Util; |
26 | 27 |
27 namespace Google.Apis.Authentication.OAuth2 | 28 namespace Google.Apis.Authentication.OAuth2 |
28 { | 29 { |
29 /// <summary> Implements an OAuth2 authenticator using the DotNetOpenAuth li
brary. </summary> | 30 /// <summary> Implements an OAuth2 authenticator using the DotNetOpenAuth li
brary. </summary> |
30 public class OAuth2Authenticator<TClient> : IAuthenticator, IHttpUnsuccessfu
lResponseHandler | 31 public class OAuth2Authenticator<TClient> : IAuthenticator, IHttpUnsuccessfu
lResponseHandler |
31 where TClient : ClientBase | 32 where TClient : ClientBase |
32 { | 33 { |
33 private readonly Func<TClient, IAuthorizationState> authProvider; | 34 private readonly Func<TClient, IAuthorizationState> authProvider; |
34 private readonly TClient tokenProvider; | 35 private readonly TClient tokenProvider; |
35 private bool noCaching; | 36 private bool noCaching; |
36 | 37 |
37 /// <summary> | 38 /// <summary>Creates a new OAuth2 authenticator.</summary> |
38 /// Creates a new OAuth2 authenticator. | |
39 /// </summary> | |
40 /// <param name="tokenProvider">The client which is used for requesting
access and refresh tokens.</param> | 39 /// <param name="tokenProvider">The client which is used for requesting
access and refresh tokens.</param> |
41 /// <param name="authProvider">The method which provides the initial aut
horization for the provider.</param> | 40 /// <param name="authProvider">The method which provides the initial aut
horization for the provider.</param> |
42 public OAuth2Authenticator(TClient tokenProvider, | 41 public OAuth2Authenticator(TClient tokenProvider, |
43 Func<TClient, IAuthorizationState> authProvid
er) | 42 Func<TClient, IAuthorizationState> authProvid
er) |
44 { | 43 { |
45 tokenProvider.ThrowIfNull("applicationName"); | 44 tokenProvider.ThrowIfNull("applicationName"); |
46 authProvider.ThrowIfNull("authProvider"); | 45 authProvider.ThrowIfNull("authProvider"); |
47 | 46 |
48 this.tokenProvider = tokenProvider; | 47 this.tokenProvider = tokenProvider; |
49 this.authProvider = authProvider; | 48 this.authProvider = authProvider; |
50 } | 49 } |
51 | 50 |
52 /// <summary> | 51 /// <summary>The current state of this authenticator.</summary> |
53 /// The current state of this authenticator | |
54 /// </summary> | |
55 public IAuthorizationState State { get; private set; } | 52 public IAuthorizationState State { get; private set; } |
56 | 53 |
57 /// <summary> | 54 /// <summary> |
58 /// If this option is set to true, the authorization state will only las
t for a single request, and | 55 /// If this option is set to true, the authorization state will only las
t for a single request, and |
59 /// authorization will be re-requested for every additional request. | 56 /// authorization will be re-requested for every additional request. |
60 /// </summary> | 57 /// </summary> |
61 /// <remarks> | 58 /// <remarks> |
62 /// Will clear the current state if set to true. Can be used by Webserve
r-Applications | 59 /// Will clear the current state if set to true. Can be used by Webserve
r-Applications |
63 /// to allow multiple AuthorizationStates/users. | 60 /// to allow multiple AuthorizationStates/users. |
64 /// </remarks> | 61 /// </remarks> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 finally | 115 finally |
119 { | 116 { |
120 if (NoCaching) | 117 if (NoCaching) |
121 { | 118 { |
122 State = null; | 119 State = null; |
123 } | 120 } |
124 } | 121 } |
125 } | 122 } |
126 | 123 |
127 /// <summary>· | 124 /// <summary>· |
128 /// Override handle response to refresh the token when Unauthorized stat
us code is received.· | 125 /// Overrides handle response to refresh the token when Unauthorized sta
tus code is received.· |
129 /// </summary> | 126 /// </summary> |
130 public bool HandleResponse(HandleUnsuccessfulResponseArgs args) | 127 public Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs arg
s) |
131 { | 128 { |
132 return args.Response.StatusCode == HttpStatusCode.Unauthorized && to
kenProvider.RefreshToken(State, null); | 129 var result = args.Response.StatusCode == HttpStatusCode.Unauthorized
&& |
| 130 tokenProvider.RefreshToken(State, null); |
| 131 |
| 132 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); |
| 133 tcs.SetResult(result); |
| 134 return tcs.Task; |
133 } | 135 } |
134 } | 136 } |
135 } | 137 } |
OLD | NEW |