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 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 { | 49 { |
50 get { return flow; } | 50 get { return flow; } |
51 } | 51 } |
52 | 52 |
53 /// <summary>Gets the code receiver which is responsible for receiving t
he authorization code.</summary> | 53 /// <summary>Gets the code receiver which is responsible for receiving t
he authorization code.</summary> |
54 public ICodeReceiver CodeReceiver | 54 public ICodeReceiver CodeReceiver |
55 { | 55 { |
56 get { return codeReceiver; } | 56 get { return codeReceiver; } |
57 } | 57 } |
58 | 58 |
59 public async Task<Credential> Authorize(string userId, CancellationToken
taskCancellationToken) | 59 public async Task<UserCredential> Authorize(string userId, CancellationT
oken taskCancellationToken) |
60 { | 60 { |
61 // Try to load a token from the data store. | 61 // Try to load a token from the data store. |
62 var token = await Flow.LoadTokenAsync(userId, taskCancellationToken)
.ConfigureAwait(false); | 62 var token = await Flow.LoadTokenAsync(userId, taskCancellationToken)
.ConfigureAwait(false); |
63 | 63 |
64 // If the stored token is null or it doesn't have a refresh token an
d the access token is expired we need· | 64 // If the stored token is null or it doesn't have a refresh token an
d the access token is expired we need· |
65 // to retrieve a new access token. | 65 // to retrieve a new authorization code. |
66 if (token == null || (token.RefreshToken == null && token.IsExpired(
flow.Clock))) | 66 if (token == null || (token.RefreshToken == null && token.IsExpired(
flow.Clock))) |
67 { | 67 { |
68 // Create a authorization code request. | 68 // Create a authorization code request. |
69 var redirectUri = CodeReceiver.RedirectUri; | 69 var redirectUri = CodeReceiver.RedirectUri; |
70 AuthorizationCodeRequestUrl codeRequest = Flow.CreateAuthorizati
onCodeRequest(redirectUri); | 70 AuthorizationCodeRequestUrl codeRequest = Flow.CreateAuthorizati
onCodeRequest(redirectUri); |
71 | 71 |
72 // Receive the code. | 72 // Receive the code. |
73 var response = await CodeReceiver.ReceiveCodeAsync(codeRequest,
taskCancellationToken) | 73 var response = await CodeReceiver.ReceiveCodeAsync(codeRequest,
taskCancellationToken) |
74 .ConfigureAwait(false); | 74 .ConfigureAwait(false); |
75 | 75 |
76 if (string.IsNullOrEmpty(response.Code)) | 76 if (string.IsNullOrEmpty(response.Code)) |
77 { | 77 { |
78 var errorResponse = new TokenErrorResponse(response); | 78 var errorResponse = new TokenErrorResponse(response); |
79 Logger.Info("Received an error. The response is: {0}", error
Response); | 79 Logger.Info("Received an error. The response is: {0}", error
Response); |
80 throw new TokenResponseException(errorResponse); | 80 throw new TokenResponseException(errorResponse); |
81 } | 81 } |
82 | 82 |
83 Logger.Debug("Received \"{0}\" code", response.Code); | 83 Logger.Debug("Received \"{0}\" code", response.Code); |
84 | 84 |
85 // Get the token based on the code. | 85 // Get the token based on the code. |
86 token = await Flow.ExchangeCodeForTokenAsync(userId, response.Co
de, CodeReceiver.RedirectUri, | 86 token = await Flow.ExchangeCodeForTokenAsync(userId, response.Co
de, CodeReceiver.RedirectUri, |
87 taskCancellationToken).ConfigureAwait(false); | 87 taskCancellationToken).ConfigureAwait(false); |
88 } | 88 } |
89 | 89 |
90 return new Credential(flow, userId, token); | 90 return new UserCredential(flow, userId, token); |
91 } | 91 } |
92 | 92 |
93 #endregion | 93 #endregion |
94 } | 94 } |
95 } | 95 } |
LEFT | RIGHT |