Index: Src/GoogleApis.Auth/OAuth2/AuthorizationCodeWebApp.cs =================================================================== new file mode 100644 --- /dev/null +++ b/Src/GoogleApis.Auth/OAuth2/AuthorizationCodeWebApp.cs @@ -0,0 +1,127 @@ +/* +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; +using System.Threading; +using System.Threading.Tasks; + +using Google.Apis.Auth.OAuth2.Requests; + +namespace Google.Apis.Auth.OAuth2 +{ + /// + /// Thread safe OAuth 2.0 authorization code flow for a web application that persists end-user credentials. + /// + public class AuthorizationCodeWebApp + { + /// + /// The state key. As part of making the request for authorization code we save the original request to verify + /// that this server create the original request. + /// + public const string StateKey = "oauth_"; + + /// The length of the random number which will be added to the end of the state parameter. + public const int StateRandomLength = 8; + + /// + /// AuthResult which contains the user's credentials if it was loaded successfully from the store. Otherwise + /// it contains the redirect URI for the authorization server. + /// + public class AuthResult + { + /// + /// Gets or sets the user's credentials or null in case the end user needs to authorize. + /// + public UserCredential Credential { get; set; } + + /// + /// Gets or sets the redirect URI to for the user to authorize against the authorization server or + /// null in case the was loaded from the data + /// store. + /// + public string RedirectUri { get; set; } + } + + private readonly IAuthorizationCodeFlow flow; + private readonly string redirectUri; + private readonly string state; + + /// Gets the authorization code flow. + public IAuthorizationCodeFlow Flow + { + get { return flow; } + } + + /// Gets the OAuth2 callback redirect URI. + public string RedirectUri + { + get { return redirectUri; } + } + + /// Gets the state which is used to navigate back to the page that started the OAuth flow. + public string State + { + get { return state; } + } + + /// + /// Constructs a new authorization code installed application with the given flow and code receiver. + /// + public AuthorizationCodeWebApp(IAuthorizationCodeFlow flow, string redirectUri, string state) + { + // TODO(peleyal): should we provide a way to disable to random number in the end of the state parameter? + this.flow = flow; + this.redirectUri = redirectUri; + this.state = state; + } + + /// Authorizes the web application to access user's protected data. + /// User identifier + /// Cancellation token to cancel an operation + /// + /// Auth result object which contains the user's credential or redirect URI for the authorization server + /// + 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 access token. + if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock))) + { + // Create a authorization code request. + AuthorizationCodeRequestUrl codeRequest = Flow.CreateAuthorizationCodeRequest(redirectUri); + + // Add a random number to the end of the state so we can indicate the original request was made by this + // call. + var oauthState = state; + if (Flow.DataStore != null) + { + var rndString = new string('9', StateRandomLength); + var random = new Random().Next(int.Parse(rndString)).ToString("D" + StateRandomLength); + oauthState += random; + await Flow.DataStore.StoreAsync(StateKey + userId, oauthState); + } + codeRequest.State = oauthState; + + return new AuthResult { RedirectUri = codeRequest.Build().ToString() }; + } + + return new AuthResult { Credential = new UserCredential(flow, userId, token) }; + } + } +}