/// <summary> /// Creates a request elements concatentation value to send with a request. /// This is also known as the signature base. /// </summary> /// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.1.3"/> /// <seealso cref="http://oauth.net/core/1.0#sig_base_example"/> /// <param name="method">The request's HTTP method type</param> /// <param name="url">The request URL</param> /// <param name="parameters">The request's parameters</param> /// <returns>A signature base string</returns> public static string ConcatenateRequestElements(string method, string url, WebParameterCollection parameters) { StringBuilder sb = new StringBuilder(); // Separating &'s are not URL encoded string requestMethod = method.ToUpper().Then("&"); string requestUrl = UrlEncodeRelaxed(ConstructRequestUrl(url.AsUri())).Then("&"); string requestParameters = UrlEncodeRelaxed(NormalizeRequestParameters(parameters)); sb.Append(requestMethod); sb.Append(requestUrl); sb.Append(requestParameters); return(sb.ToString()); }
/// <summary> /// Sorts a <see cref="WebParameterCollection"/> by name, and then value if equal. /// </summary> /// <param name="parameters">A collection of parameters to sort</param> /// <returns>A sorted parameter collection</returns> public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters) { WebParameterCollection copy = new WebParameterCollection(parameters); IEnumerable <WebPair> exclusions = copy.Where(n => n.Name.EqualsIgnoreCase("oauth_signature")); copy.RemoveAll(exclusions); copy.ForEach(p => { p.Name = UrlEncodeStrict(p.Name); p.Value = UrlEncodeStrict(p.Value); }); copy.Sort((x, y) => string.CompareOrdinal(x.Name, y.Name) != 0 ? string.CompareOrdinal(x.Name, y.Name) : string.CompareOrdinal(x.Value, y.Value)); return(copy); }
private void AddXAuthParameters(ICollection <WebPair> parameters, string timestamp, string nonce) { WebParameterCollection authParameters = new WebParameterCollection { new WebPair("x_auth_username", this.ClientUsername), new WebPair("x_auth_password", this.ClientPassword), new WebPair("x_auth_mode", "client_auth"), new WebPair("oauth_consumer_key", this.ConsumerKey), new WebPair("oauth_signature_method", this.SignatureMethod.ToRequestValue()), new WebPair("oauth_timestamp", timestamp), new WebPair("oauth_nonce", nonce), new WebPair("oauth_version", this.Version ?? "1.0") }; foreach (WebPair authParameter in authParameters) { parameters.Add(authParameter); } }
/// <summary> /// Generates a <see cref="OAuthWebQueryInfo"/> instance to pass to an /// <see cref="IAuthenticator" /> for the purpose of exchanging user credentials /// for an access token authorized by the user at the Service Provider site. /// </summary> /// <param name="method">The HTTP method for the intended request</param> /// <seealso cref="http://tools.ietf.org/html/draft-dehora-farrell-oauth-accesstoken-creds-00#section-4"/> /// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param> public virtual OAuthWebQueryInfo BuildClientAuthAccessTokenInfo(string method, WebParameterCollection parameters) { this.ValidateClientAuthAccessRequestState(); if (parameters == null) { parameters = new WebParameterCollection(); } Uri uri = new Uri(this.AccessTokenUrl); string timestamp = OAuthTools.GetTimestamp(); string nonce = OAuthTools.GetNonce(); this.AddXAuthParameters(parameters, timestamp, nonce); string signatureBase = OAuthTools.ConcatenateRequestElements(method, uri.ToString(), parameters); string signature = OAuthTools.GetSignature(this.SignatureMethod, this.SignatureTreatment, signatureBase, this.ConsumerSecret); OAuthWebQueryInfo info = new OAuthWebQueryInfo { WebMethod = method, ParameterHandling = this.ParameterHandling, ClientMode = "client_auth", ClientUsername = this.ClientUsername, ClientPassword = this.ClientPassword, ConsumerKey = this.ConsumerKey, SignatureMethod = this.SignatureMethod.ToRequestValue(), SignatureTreatment = this.SignatureTreatment, Signature = signature, Timestamp = timestamp, Nonce = nonce, Version = this.Version ?? "1.0", TokenSecret = this.TokenSecret, ConsumerSecret = this.ConsumerSecret }; return(info); }
private void AddAuthParameters(ICollection <WebPair> parameters, string timestamp, string nonce) { WebParameterCollection authParameters = new WebParameterCollection { new WebPair("oauth_consumer_key", this.ConsumerKey), new WebPair("oauth_nonce", nonce), new WebPair("oauth_signature_method", this.SignatureMethod.ToRequestValue()), new WebPair("oauth_timestamp", timestamp), new WebPair("oauth_version", this.Version ?? "1.0") }; if (!this.Token.IsNullOrBlank()) { authParameters.Add(new WebPair("oauth_token", this.Token)); } if (!this.CallbackUrl.IsNullOrBlank()) { authParameters.Add(new WebPair("oauth_callback", this.CallbackUrl)); } if (!this.Verifier.IsNullOrBlank()) { authParameters.Add(new WebPair("oauth_verifier", this.Verifier)); } if (!this.SessionHandle.IsNullOrBlank()) { authParameters.Add(new WebPair("oauth_session_handle", this.SessionHandle)); } foreach (WebPair authParameter in authParameters) { parameters.Add(authParameter); } }
public virtual OAuthWebQueryInfo BuildProtectedResourceInfo(string method, WebParameterCollection parameters, string url) { this.ValidateProtectedResourceState(); if (parameters == null) { parameters = new WebParameterCollection(); } // Include url parameters in query pool Uri uri = new Uri(url); #if !SILVERLIGHT && !WINDOWS_PHONE NameValueCollection urlParameters = HttpUtility.ParseQueryString(uri.Query); #else IDictionary <string, string> urlParameters = uri.Query.ParseQueryString(); #endif #if !SILVERLIGHT && !WINDOWS_PHONE foreach (string parameter in urlParameters.AllKeys) #else foreach (string parameter in urlParameters.Keys) #endif { switch (method.ToUpperInvariant()) { case "POST": parameters.Add(new HttpPostParameter(parameter, urlParameters[parameter])); break; default: parameters.Add(parameter, urlParameters[parameter]); break; } } string timestamp = OAuthTools.GetTimestamp(); string nonce = OAuthTools.GetNonce(); this.AddAuthParameters(parameters, timestamp, nonce); string signatureBase = OAuthTools.ConcatenateRequestElements(method, url, parameters); string signature = OAuthTools.GetSignature(this.SignatureMethod, this.SignatureTreatment, signatureBase, this.ConsumerSecret, this.TokenSecret); OAuthWebQueryInfo info = new OAuthWebQueryInfo { WebMethod = method, ParameterHandling = this.ParameterHandling, ConsumerKey = this.ConsumerKey, Token = this.Token, SignatureMethod = this.SignatureMethod.ToRequestValue(), SignatureTreatment = this.SignatureTreatment, Signature = signature, Timestamp = timestamp, Nonce = nonce, Version = this.Version ?? "1.0", Callback = this.CallbackUrl, ConsumerSecret = this.ConsumerSecret, TokenSecret = this.TokenSecret }; return(info); }