LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 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.Http; | 18 using System.Net.Http; |
19 using System.Threading; | 19 using System.Threading; |
20 using System.Threading.Tasks; | 20 using System.Threading.Tasks; |
| 21 |
21 using Google.Apis.Auth.OAuth2.Requests; | 22 using Google.Apis.Auth.OAuth2.Requests; |
22 using Google.Apis.Auth.OAuth2.Responses; | 23 using Google.Apis.Auth.OAuth2.Responses; |
23 using Google.Apis.Json; | 24 using Google.Apis.Json; |
24 | 25 |
25 namespace Google.Apis.Auth.OAuth2.Flows | 26 namespace Google.Apis.Auth.OAuth2.Flows |
26 { | 27 { |
27 /// <summary> | 28 /// <summary> |
28 /// Google specific authorization code flow which inherits from <see cref="A
uthorizationCodeFlow"/>. | 29 /// Google specific authorization code flow which inherits from <seealso cre
f="AuthorizationCodeFlow"/>. |
29 /// </summary> | 30 /// </summary> |
30 public class GoogleAuthorizationCodeFlow : AuthorizationCodeFlow | 31 public class GoogleAuthorizationCodeFlow : AuthorizationCodeFlow |
31 { | 32 { |
32 private readonly string revokeTokenUrl; | 33 private readonly string revokeTokenUrl; |
33 | 34 |
34 /// <summary>Gets the revoke token URL.</summary> | 35 /// <summary>Gets the token revocation URL.</summary> |
35 public string RevokeTokenUrl { get { return revokeTokenUrl; } } | 36 public string RevokeTokenUrl { get { return revokeTokenUrl; } } |
36 | 37 |
37 /// <summary>Constructs a new Google authorization code flow.</summary> | 38 /// <summary>Constructs a new Google authorization code flow.</summary> |
38 public GoogleAuthorizationCodeFlow(Initializer initializer) | 39 public GoogleAuthorizationCodeFlow(Initializer initializer) |
39 : base(initializer) | 40 : base(initializer) |
40 { | 41 { |
41 revokeTokenUrl = initializer.RevokeTokenUrl; | 42 revokeTokenUrl = initializer.RevokeTokenUrl; |
42 } | 43 } |
43 | 44 |
44 public override AuthorizationCodeRequestUrl CreateAuthorizationCodeReque
st(string redirectUri) | 45 public override AuthorizationCodeRequestUrl CreateAuthorizationCodeReque
st(string redirectUri) |
45 { | 46 { |
46 return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationSe
rverUrl)) | 47 return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationSe
rverUrl)) |
47 { | 48 { |
48 ClientId = ClientSecrets.ClientId, | 49 ClientId = ClientSecrets.ClientId, |
49 Scope = string.Join(" ", Scopes), | 50 Scope = string.Join(" ", Scopes), |
50 RedirectUri = redirectUri | 51 RedirectUri = redirectUri |
51 }; | 52 }; |
52 } | 53 } |
53 | 54 |
54 public override async Task RevokeTokenAsync(string userId, string token,
CancellationToken taskCancellationToken) | 55 public override async Task RevokeTokenAsync(string userId, string token, |
| 56 CancellationToken taskCancellationToken) |
55 { | 57 { |
56 GoogleRevokeTokenRequest request = new GoogleRevokeTokenRequest(new
Uri(RevokeTokenUrl)) | 58 GoogleRevokeTokenRequest request = new GoogleRevokeTokenRequest(new
Uri(RevokeTokenUrl)) |
57 { | 59 { |
58 Token = token | 60 Token = token |
59 }; | 61 }; |
60 var httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Bu
ild()); | 62 var httpRequest = new HttpRequestMessage(HttpMethod.Get, request.Bui
ld()); |
61 | 63 |
62 var response = await HttpClient.SendAsync(httpRequest, taskCancellat
ionToken).ConfigureAwait(false); | 64 var response = await HttpClient.SendAsync(httpRequest, taskCancellat
ionToken).ConfigureAwait(false); |
63 | |
64 if (!response.IsSuccessStatusCode) | 65 if (!response.IsSuccessStatusCode) |
65 { | 66 { |
66 var content = await response.Content.ReadAsStringAsync().Configu
reAwait(false); | 67 var content = await response.Content.ReadAsStringAsync().Configu
reAwait(false); |
67 var error = NewtonsoftJsonSerializer.Instance.Deserialize<TokenE
rrorResponse>(content); | 68 var error = NewtonsoftJsonSerializer.Instance.Deserialize<TokenE
rrorResponse>(content); |
68 throw new TokenResponseException(error); | 69 throw new TokenResponseException(error); |
69 } | 70 } |
70 | 71 |
71 await DeleteTokenAsync(userId, taskCancellationToken); | 72 await DeleteTokenAsync(userId, taskCancellationToken); |
72 } | 73 } |
73 | 74 |
74 /// <summary>An initializer class for Google authorization code flow. </
summary> | 75 /// <summary>An initializer class for Google authorization code flow. </
summary> |
75 public new class Initializer : AuthorizationCodeFlow.Initializer | 76 public new class Initializer : AuthorizationCodeFlow.Initializer |
76 { | 77 { |
77 /// <summary>Gets or sets the revoke token URL.</summary> | 78 /// <summary>Gets or sets the token revocation URL.</summary> |
78 public string RevokeTokenUrl { get; set; } | 79 public string RevokeTokenUrl { get; set; } |
79 | 80 |
80 /// <summary> | 81 /// <summary> |
81 /// Constructs a new initializer. Sets Authorization server URL to· | 82 /// Constructs a new initializer. Sets Authorization server URL to· |
82 /// <see cref="Google.Apis.Auth.OAuth2.GoogleAuthConsts.Authorizatio
nUrl"/>, and Token server URL to· | 83 /// <seealso cref="GoogleAuthConsts.AuthorizationUrl"/>, and Token s
erver URL to· |
83 /// <see cref="Google.Apis.Auth.OAuth2.GoogleAuthConsts.TokenUrl"/>. | 84 /// <seealso cref="GoogleAuthConsts.TokenUrl"/>. |
84 /// </summary> | 85 /// </summary> |
85 public Initializer() | 86 public Initializer() |
86 : base(GoogleAuthConsts.AuthorizationUrl, GoogleAuthConsts.Token
Url) | 87 : base(GoogleAuthConsts.AuthorizationUrl, GoogleAuthConsts.Token
Url) |
87 { | 88 { |
88 RevokeTokenUrl = GoogleAuthConsts.RevokeTokenUrl; | 89 RevokeTokenUrl = GoogleAuthConsts.RevokeTokenUrl; |
89 } | 90 } |
90 } | 91 } |
91 } | 92 } |
92 } | 93 } |
LEFT | RIGHT |