Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(196)

Delta Between Two Patch Sets: Src/GoogleApis.Authentication.OAuth2/OAuth2Authenticator.cs

Issue 13412046: Reimplement OAuth2 library - Step 1 (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Left Patch Set: Created 10 years, 6 months ago
Right Patch Set: minor Created 10 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 using System.Threading.Tasks;
27 27
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
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 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b