/// <summary> /// Generate the signature base that is used to produce the signature /// </summary> /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> /// <param name="consumerKey">The consumer key</param> /// <param name="token">The token, if available. If not available pass null or an empty String</param> /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> /// <param name="nonce"></param> /// <param name="signatureType">The signature type. To use the default values use <see cref="OAuthClient.SignatureTypes">OAuthClient.SignatureTypes</see>.</param> /// <param name="timeStamp"></param> /// <returns>The signature base</returns> public static SignatureInfo GenerateSignatureBase(Uri url, String consumerKey, String token, String httpMethod, String timeStamp, String nonce, String signatureType) { SignatureInfo si = new SignatureInfo(); if (token == null) { token = String.Empty; } if (timeStamp == null) { throw new ArgumentNullException("timeStamp"); } if (String.IsNullOrEmpty(consumerKey)) { throw new ArgumentNullException("consumerKey"); } if (String.IsNullOrEmpty(httpMethod)) { throw new ArgumentNullException("httpMethod"); } if (String.IsNullOrEmpty(signatureType)) { throw new ArgumentNullException("signatureType"); } List <QueryParameter> parameters = OAuthClient.GetQueryParameters(url.Query); parameters.Add(new QueryParameter(Key.OAuthVersion, Key.OAuthVersionNo)); parameters.Add(new QueryParameter(Key.OAuthNonce, nonce)); parameters.Add(new QueryParameter(Key.OAuthTimestamp, timeStamp)); parameters.Add(new QueryParameter(Key.OAuthSignatureMethod, signatureType)); parameters.Add(new QueryParameter(Key.OAuthConsumerKey, consumerKey)); if (!String.IsNullOrEmpty(token)) { parameters.Add(new QueryParameter(Key.OAuthToken, token)); } parameters.Sort(new QueryParameterComparer()); si.NormalizedUrl = String.Format("{0}://{1}", url.Scheme, url.Host); if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443))) { si.NormalizedUrl += ":" + url.Port; } si.NormalizedUrl += url.AbsolutePath; si.NormalizedRequestParameters = NormalizeRequestParameters(parameters); StringBuilder sb = new StringBuilder(1000); sb.AppendFormat("{0}&", httpMethod.ToUpper()); sb.AppendFormat("{0}&", OAuthClient.UrlEncode(si.NormalizedUrl)); sb.AppendFormat("{0}", OAuthClient.UrlEncode(si.NormalizedRequestParameters)); si.Signature = sb.ToString(); return(si); }
private HttpClient CreateHttpClientRequestHeaderMode(HttpMethodName methodName, String url, String token, String tokenSecret, IDictionary <String, String> queryString) { String timeStamp = OAuthClient.GenerateTimeStamp(); String nonce = OAuthClient.GenerateNonce(); Dictionary <String, String> pp = OAuthClient.GenerateParameters(ConsumerKey, token, timeStamp, nonce); Uri u = new Uri(HttpClient.CreateQueryString(url, queryString, OAuthClient.UrlEncode)); SignatureInfo si = GenerateSignature(u, this.ConsumerKey, this.ConsumerSecret, token, tokenSecret , methodName.ToString().ToUpper(), timeStamp, nonce); pp.Add("oauth_signature", OAuthClient.UrlEncode(si.Signature)); //NOTE:NET2.0 HttpClient cl = new HttpClient(HttpClient.CreateQueryString(url, queryString, HttpClient.UrlEncode)); cl.MethodName = methodName; cl.Headers[HttpRequestHeader.Authorization] = this.CreateOAuthHeader(pp); return(cl); }
/// <summary> /// Generates a signature using the specified signatureType /// </summary> /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> /// <param name="consumerKey">The consumer key</param> /// <param name="consumerSecret">The consumer seceret</param> /// <param name="token">The token, if available. If not available pass null or an empty String</param> /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty String</param> /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> /// <param name="nonce"></param> /// <param name="signatureType">The type of signature to use</param> /// <param name="timeStamp"></param> /// <returns>A base64 String of the hash value</returns> public static SignatureInfo GenerateSignature(Uri url, String consumerKey, String consumerSecret, String token, String tokenSecret, String httpMethod, String timeStamp, String nonce, SignatureTypes signatureType) { SignatureInfo si = new SignatureInfo(); switch (signatureType) { case SignatureTypes.PLAINTEXT: si.Signature = OAuthClient.UrlEncode(String.Format("{0}&{1}", consumerSecret, tokenSecret)); return(si); case SignatureTypes.HMACSHA1: si = GenerateSignatureBase(url, consumerKey, token, httpMethod, timeStamp, nonce, Key.HMACSHA1SignatureType); HMACSHA1 hs = new HMACSHA1(); hs.Key = OAuthClient.GenerateSignatureEncoding.GetBytes(String.Format("{0}&{1}", OAuthClient.UrlEncode(consumerSecret), String.IsNullOrEmpty(tokenSecret) ? "" : OAuthClient.UrlEncode(tokenSecret))); si.Signature = GenerateSignatureUsingHash(si.Signature, hs); return(si); case SignatureTypes.RSASHA1: throw new NotImplementedException(); } throw new ArgumentException("Unknown signature type", "signatureType"); }
private HttpClient CreateHttpClientQueryStringMode(HttpMethodName methodName, String url, String token, String tokenSecret, IEnumerable <KeyValuePair <string, string> > queryString) { String timeStamp = OAuthClient.GenerateTimeStamp(); String nonce = OAuthClient.GenerateNonce(); Dictionary <String, String> pp = OAuthClient.GenerateParameters(ConsumerKey, token, timeStamp, nonce); foreach (KeyValuePair <string, string> p in queryString) { pp.Add(p.Key, p.Value); } //NOTE:NET2.0 Uri u = new Uri(HttpClient.CreateQueryString(url, pp, OAuthClient.UrlEncode)); SignatureInfo si = GenerateSignature(u, this.ConsumerKey, this.ConsumerSecret, token, tokenSecret , methodName.ToString().ToUpper(), timeStamp, nonce); pp.Add("oauth_signature", OAuthClient.UrlEncode(si.Signature)); //NOTE:NET2.0 HttpClient cl = new HttpClient(HttpClient.CreateQueryString(url, pp, HttpClient.UrlEncode)); cl.MethodName = methodName; return(cl); }