/* 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.Collections.Generic; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Google.Apis.Auth.OAuth2.Requests; using Google.Apis.Auth.OAuth2.Responses; using Google.Apis.Json; namespace Google.Apis.Auth.OAuth2.Flows { /// /// Google specific authorization code flow which inherits from . /// public class GoogleAuthorizationCodeFlow : AuthorizationCodeFlow { private readonly string revokeTokenUrl; /// Gets the token revocation URL. public string RevokeTokenUrl { get { return revokeTokenUrl; } } /// Gets or sets the include granted scopes indicator. /// Do not use, use instead. public readonly bool? includeGrantedScopes; /// Gets or sets the include granted scopes indicator. public bool? IncludeGrantedScopes { get { return includeGrantedScopes; } } private readonly IEnumerable> userDefinedQueryParams; /// Gets the user defined query parameters. public IEnumerable> UserDefinedQueryParams { get { return userDefinedQueryParams; } } /// Constructs a new Google authorization code flow. public GoogleAuthorizationCodeFlow(Initializer initializer) : base(initializer) { revokeTokenUrl = initializer.RevokeTokenUrl; includeGrantedScopes = initializer.IncludeGrantedScopes; userDefinedQueryParams = initializer.UserDefinedQueryParams; } /// public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri) { return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl)) { ClientId = ClientSecrets.ClientId, Scope = string.Join(" ", Scopes), RedirectUri = redirectUri, IncludeGrantedScopes = IncludeGrantedScopes.HasValue ? IncludeGrantedScopes.Value.ToString().ToLower() : null, UserDefinedQueryParams = UserDefinedQueryParams }; } /// public override async Task RevokeTokenAsync(string userId, string token, CancellationToken taskCancellationToken) { GoogleRevokeTokenRequest request = new GoogleRevokeTokenRequest(new Uri(RevokeTokenUrl)) { Token = token }; var httpRequest = new HttpRequestMessage(HttpMethod.Get, request.Build()); var response = await HttpClient.SendAsync(httpRequest, taskCancellationToken).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var error = NewtonsoftJsonSerializer.Instance.Deserialize(content); throw new TokenResponseException(error, response.StatusCode); } await DeleteTokenAsync(userId, taskCancellationToken); } /// public override bool ShouldForceTokenRetrieval() { return IncludeGrantedScopes.HasValue && IncludeGrantedScopes.Value; } /// An initializer class for Google authorization code flow. public new class Initializer : AuthorizationCodeFlow.Initializer { /// Gets or sets the token revocation URL. public string RevokeTokenUrl { get; set; } /// /// Gets or sets the optional indicator for including granted scopes for incremental authorization. /// public bool? IncludeGrantedScopes { get; set; } /// Gets or sets the optional user defined query parameters. public IEnumerable> UserDefinedQueryParams { get; set; } /// /// Constructs a new initializer. Sets Authorization server URL to /// , and Token server URL to /// . /// public Initializer() : this( GoogleAuthConsts.OidcAuthorizationUrl, GoogleAuthConsts.OidcTokenUrl, GoogleAuthConsts.RevokeTokenUrl) { } /// Constructs a new initializer. /// Authorization server URL /// Token server URL /// Revocation server URL /// /// This is mainly for internal testing at Google, where we occasionally need /// to use alternative oauth endpoints. This is not for general use. /// protected Initializer(string authorizationServerUrl, string tokenServerUrl, string revokeTokenUrl) : base(authorizationServerUrl, tokenServerUrl) { RevokeTokenUrl = revokeTokenUrl; } } } }