LEFT | RIGHT |
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 |
(...skipping 17 matching lines...) Expand all Loading... |
28 namespace Google.Apis.Authentication.OAuth2 | 28 namespace Google.Apis.Authentication.OAuth2 |
29 { | 29 { |
30 /// <summary> Implements an OAuth2 authenticator using the DotNetOpenAuth li
brary. </summary> | 30 /// <summary> Implements an OAuth2 authenticator using the DotNetOpenAuth li
brary. </summary> |
31 public class OAuth2Authenticator<TClient> : IAuthenticator, IHttpUnsuccessfu
lResponseHandler | 31 public class OAuth2Authenticator<TClient> : IAuthenticator, IHttpUnsuccessfu
lResponseHandler |
32 where TClient : ClientBase | 32 where TClient : ClientBase |
33 { | 33 { |
34 private readonly Func<TClient, IAuthorizationState> authProvider; | 34 private readonly Func<TClient, IAuthorizationState> authProvider; |
35 private readonly TClient tokenProvider; | 35 private readonly TClient tokenProvider; |
36 private bool noCaching; | 36 private bool noCaching; |
37 | 37 |
38 /// <summary> | 38 /// <summary>Creates a new OAuth2 authenticator.</summary> |
39 /// Creates a new OAuth2 authenticator. | |
40 /// </summary> | |
41 /// <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> |
42 /// <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> |
43 public OAuth2Authenticator(TClient tokenProvider, | 41 public OAuth2Authenticator(TClient tokenProvider, |
44 Func<TClient, IAuthorizationState> authProvid
er) | 42 Func<TClient, IAuthorizationState> authProvid
er) |
45 { | 43 { |
46 tokenProvider.ThrowIfNull("applicationName"); | 44 tokenProvider.ThrowIfNull("applicationName"); |
47 authProvider.ThrowIfNull("authProvider"); | 45 authProvider.ThrowIfNull("authProvider"); |
48 | 46 |
49 this.tokenProvider = tokenProvider; | 47 this.tokenProvider = tokenProvider; |
50 this.authProvider = authProvider; | 48 this.authProvider = authProvider; |
51 } | 49 } |
52 | 50 |
53 /// <summary> | 51 /// <summary>The current state of this authenticator.</summary> |
54 /// The current state of this authenticator | |
55 /// </summary> | |
56 public IAuthorizationState State { get; private set; } | 52 public IAuthorizationState State { get; private set; } |
57 | 53 |
58 /// <summary> | 54 /// <summary> |
59 /// 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 |
60 /// authorization will be re-requested for every additional request. | 56 /// authorization will be re-requested for every additional request. |
61 /// </summary> | 57 /// </summary> |
62 /// <remarks> | 58 /// <remarks> |
63 /// 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 |
64 /// to allow multiple AuthorizationStates/users. | 60 /// to allow multiple AuthorizationStates/users. |
65 /// </remarks> | 61 /// </remarks> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 finally | 115 finally |
120 { | 116 { |
121 if (NoCaching) | 117 if (NoCaching) |
122 { | 118 { |
123 State = null; | 119 State = null; |
124 } | 120 } |
125 } | 121 } |
126 } | 122 } |
127 | 123 |
128 /// <summary>· | 124 /// <summary>· |
129 /// 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.· |
130 /// </summary> | 126 /// </summary> |
131 public Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs arg
s) | 127 public Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs arg
s) |
132 { | 128 { |
133 var result = args.Response.StatusCode == HttpStatusCode.Unauthorized
&& | 129 var result = args.Response.StatusCode == HttpStatusCode.Unauthorized
&& |
134 tokenProvider.RefreshToken(State, null); | 130 tokenProvider.RefreshToken(State, null); |
135 | 131 |
136 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); | 132 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); |
137 tcs.SetResult(result); | 133 tcs.SetResult(result); |
138 return tcs.Task; | 134 return tcs.Task; |
139 } | 135 } |
140 } | 136 } |
141 } | 137 } |
LEFT | RIGHT |