Index: Src/GoogleApis.Auth/OAuth2/AuthorizationCodeInstalledApp.cs =================================================================== new file mode 100644 --- /dev/null +++ b/Src/GoogleApis.Auth/OAuth2/AuthorizationCodeInstalledApp.cs @@ -0,0 +1,95 @@ +/* +Copyright 2013 Google Inc + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +using System.Threading; +using System.Threading.Tasks; + +using Google.Apis.Auth.OAuth2.Responses; +using Google.Apis.Auth.OAuth2.Requests; +using Google.Apis.Logging; + +namespace Google.Apis.Auth.OAuth2 +{ + /// + /// Thread-safe OAuth 2.0 authorization code flow for an installed application that persists end-user credentials. + /// + public class AuthorizationCodeInstalledApp : IAuthorizationCodeInstalledApp + { + private static readonly ILogger Logger = ApplicationContext.Logger.ForType(); + + private readonly IAuthorizationCodeFlow flow; + private readonly ICodeReceiver codeReceiver; + + /// + /// Constructs a new authorization code installed application with the given flow and code receiver. + /// + public AuthorizationCodeInstalledApp(IAuthorizationCodeFlow flow, ICodeReceiver codeReceiver) + { + this.flow = flow; + this.codeReceiver = codeReceiver; + } + + #region IAuthorizationCodeInstalledApp Members + + /// Gets the authorization code flow. + public IAuthorizationCodeFlow Flow + { + get { return flow; } + } + + /// Gets the code receiver which is responsible for receiving the authorization code. + public ICodeReceiver CodeReceiver + { + get { return codeReceiver; } + } + + public async Task Authorize(string userId, CancellationToken taskCancellationToken) + { + // Try to load a token from the data store. + var token = await Flow.LoadTokenAsync(userId, taskCancellationToken).ConfigureAwait(false); + + // If the stored token is null or it doesn't have a refresh token and the access token is expired we need + // to retrieve a new authorization code. + if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock))) + { + // Create a authorization code request. + var redirectUri = CodeReceiver.RedirectUri; + AuthorizationCodeRequestUrl codeRequest = Flow.CreateAuthorizationCodeRequest(redirectUri); + + // Receive the code. + var response = await CodeReceiver.ReceiveCodeAsync(codeRequest, taskCancellationToken) + .ConfigureAwait(false); + + if (string.IsNullOrEmpty(response.Code)) + { + var errorResponse = new TokenErrorResponse(response); + Logger.Info("Received an error. The response is: {0}", errorResponse); + throw new TokenResponseException(errorResponse); + } + + Logger.Debug("Received \"{0}\" code", response.Code); + + // Get the token based on the code. + token = await Flow.ExchangeCodeForTokenAsync(userId, response.Code, CodeReceiver.RedirectUri, + taskCancellationToken).ConfigureAwait(false); + } + + return new UserCredential(flow, userId, token); + } + + #endregion + } +} \ No newline at end of file