public static string GenerateHeader(Uri uri, string httpMethod, OAuthParameters parameters) { parameters.Timestamp = OAuthBase.GenerateTimeStamp(); parameters.Nonce = OAuthBase.GenerateNonce(); string signature = OAuthBase.GenerateSignature(uri, httpMethod, parameters); var sb = new StringBuilder(); sb.Append($@"Authorization: OAuth {OAuthBase.OAuthVersionKey}=""{OAuthBase.OAuthVersion}"","); sb.Append($@"{OAuthBase.OAuthNonceKey}=""{OAuthBase.EncodingPerRfc3986(parameters.Nonce)}"","); sb.Append($@"{OAuthBase.OAuthTimestampKey}=""{OAuthBase.EncodingPerRfc3986(parameters.Timestamp)}"","); sb.Append($@"{OAuthBase.OAuthConsumerKeyKey}=""{OAuthBase.EncodingPerRfc3986(parameters.ConsumerKey)}"","); if (parameters.BaseProperties.ContainsKey(OAuthBase.OAuthVerifierKey)) { sb.Append($@"{OAuthBase.OAuthVerifierKey}=""{OAuthBase.EncodingPerRfc3986(parameters.BaseProperties[OAuthBase.OAuthVerifierKey])}"","); } if (!string.IsNullOrEmpty(parameters.Token)) { sb.Append($@"{OAuthBase.OAuthTokenKey}=""{OAuthBase.EncodingPerRfc3986(parameters.Token)}"","); } if (parameters.BaseProperties.ContainsKey(OAuthBase.OAuthCallbackKey)) { sb.Append($@"{OAuthBase.OAuthCallbackKey}=""{OAuthBase.EncodingPerRfc3986(parameters.BaseProperties[OAuthBase.OAuthCallbackKey])}"","); } sb.Append($@"{OAuthBase.OAuthSignatureMethodKey}=""{OAuthBase.HmacSha1SignatureType}"","); sb.Append($@"{OAuthBase.OAuthSignatureKey}=""{OAuthBase.EncodingPerRfc3986(signature)}"""); return(sb.ToString()); }
public static string GenerateHeader(Uri uri, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod) { var parameters = new OAuthParameters { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, Token = token, TokenSecret = tokenSecret, SignatureMethod = OAuthBase.HmacSha1SignatureType }; return(GenerateHeader(uri, httpMethod, parameters)); }
/// <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="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> /// <param name="parameters">The OAuth parameters</param> /// <returns>The signature base</returns> public static string GenerateSignatureBase(Uri url, string httpMethod, OAuthParameters parameters) { if (string.IsNullOrEmpty(parameters.ConsumerKey)) { throw new ArgumentException("consumerKey"); } if (string.IsNullOrEmpty(httpMethod)) { throw new ArgumentException(nameof(httpMethod)); } if (string.IsNullOrEmpty(parameters.SignatureMethod)) { throw new ArgumentException("signatureMethod"); } SortedDictionary <string, string> allParameters = GetQueryParameters(url.Query, parameters.BaseProperties); if (!allParameters.ContainsKey(OAuthVersionKey)) { allParameters.Add(OAuthVersionKey, OAuthVersion); } var normalizedUrl = $"{url.Scheme}://{url.Host}"; if (!(url.Scheme == "http" && url.Port == 80 || url.Scheme == "https" && url.Port == 443)) { normalizedUrl += ":" + url.Port; } normalizedUrl += url.AbsolutePath; var normalizedRequestParameters = NormalizeRequestParameters(allParameters); StringBuilder signatureBase = new StringBuilder(); signatureBase.AppendFormat(CultureInfo.InvariantCulture, "{0}&", httpMethod.ToUpper()); signatureBase.AppendFormat(CultureInfo.InvariantCulture, "{0}&", EncodingPerRfc3986(normalizedUrl)); signatureBase.AppendFormat(CultureInfo.InvariantCulture, "{0}", EncodingPerRfc3986(normalizedRequestParameters)); return(signatureBase.ToString()); }
/// <summary> /// Generates a signature using the specified signatureMethod /// </summary> /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param> /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param> /// <param name="parameters">The OAuth parameters</param> /// <returns>A base64 string of the hash value</returns> public static string GenerateSignature(Uri url, string httpMethod, OAuthParameters parameters) { switch (parameters.SignatureMethod) { case PlainTextSignatureType: return(HttpUtility.UrlEncode($"{parameters.ConsumerKey}&{parameters.TokenSecret}")); case HmacSha1SignatureType: string signatureBase = GenerateSignatureBase(url, httpMethod, parameters); HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = Encoding.ASCII.GetBytes(GenerateOAuthSignature(parameters.ConsumerSecret, parameters.TokenSecret)); return(GenerateSignatureUsingHash(signatureBase, hmacsha1)); case RsaSha1SignatureType: throw new NotImplementedException(); default: throw new ArgumentException("Unknown signature type"); } }