Index: Src/GoogleApis.Auth/OAuth2/Responses/AuthorizationCodeResponseUrl.cs =================================================================== new file mode 100644 --- /dev/null +++ b/Src/GoogleApis.Auth/OAuth2/Responses/AuthorizationCodeResponseUrl.cs @@ -0,0 +1,107 @@ +/* +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; + +namespace Google.Apis.Auth.OAuth2.Responses +{ + /// + /// Authorization Code response for the redirect URL after end user grants or denies authorization as specified + /// in http://tools.ietf.org/html/rfc6749#section-4.1.2. + /// + /// Check that is not null or empty to verify the end-user granted authorization. + /// + /// + public class AuthorizationCodeResponseUrl + { + /// Gets or sets the authorization code generated by the authorization server. + public string Code { get; set; } + + /// + /// Gets or sets the state parameter matching the state parameter in the authorization request. + /// + public string State { get; set; } + + /// + /// Gets or sets the error code (e.g. "invalid_request", "unauthorized_client", "access_denied", + /// "unsupported_response_type", "invalid_scope", "server_error", "temporarily_unavailable") as specified in + /// http://tools.ietf.org/html/rfc6749#section-4.1.2.1. + /// + public string Error { get; set; } + + /// + /// Gets or sets the human-readable text which provides additional information used to assist the client + /// developer in understanding the error occurred. + /// + public string ErrorDescription { get; set; } + + /// + /// Gets or sets the URI identifying a human-readable web page with provides information about the error. + /// + public string ErrorUri { get; set; } + + /// Constructs a new authorization code response URL from the specified dictionary. + public AuthorizationCodeResponseUrl(IDictionary queryString) + { + InitFromDictionary(queryString); + } + + #region Constructs + + /// Constructs a new authorization code response URL from the specified query string. + public AuthorizationCodeResponseUrl(string query) + { + var pairs = query.Split('&'); + var queryString = new Dictionary(); + foreach (var pair in pairs) + { + var keyValue = pair.Split('='); + queryString[keyValue[0]] = keyValue[1]; + } + + InitFromDictionary(queryString); + } + + /// Initializes this instance from the input dictionary. + private void InitFromDictionary(IDictionary queryString) + { + //TODO(peleyal): improve the following code and make it a utility + IDictionary> setters = new Dictionary>(); + setters["code"] = v => Code = v; + setters["state"] = v => State = v; + setters["error"] = v => Error = v; + setters["error_description"] = v => ErrorDescription = v; + setters["error_uri"] = v => ErrorUri = v; + + Action setter; + foreach (var pair in queryString) + { + if (setters.TryGetValue(pair.Key, out setter)) + { + setter(pair.Value); + } + } + } + + /// Constructs a new empty authorization code response URL. + public AuthorizationCodeResponseUrl() + { + } + + #endregion + } +}