/* -------- private Methods ------------------------------------------------------------------------------------------------------------------ */ /// <summary> /// Method to add the Oauth body hash to the OAuthParameters /// </summary> /// <param name="body"></param> /// <param name="oparams"></param> /// <returns></returns> private OAuthParameters setOauthBodyHashParameter(String body, OAuthParameters oparams) { byte[] bodyStringBytes = encoder.GetBytes(body); SHA1 sha = new SHA1CryptoServiceProvider(); string encodedHash = Convert.ToBase64String(sha.ComputeHash(bodyStringBytes)); oparams.addParameter(OAUTH_BODY_HASH, encodedHash); return(oparams); }
/// <summary> /// Builds the Authorization header /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <returns></returns> private string buildAuthHeaderString(String httpsURL, String requestMethod, OAuthParameters oparams) { generateAndSignSignature(httpsURL, requestMethod, oparams); StringBuilder buffer = new StringBuilder(); buffer.Append(OAUTH_START_STRING); buffer = parseParameters(buffer, oparams); this._authHeader = buffer.ToString(); return(buffer.ToString()); }
/// <summary> /// Method to build a comma delimited list of key/value string for the signature base string and authorization header /// </summary> /// <param name="buffer"></param> /// <param name="oparams"></param> /// <returns></returns> private StringBuilder parseParameters(StringBuilder buffer, OAuthParameters oparams) { foreach (KeyValuePair <string, SortedSet <string> > key in oparams.getBaseParameters()) { SortedSet <string> value = key.Value; parseSortedSetValues(buffer, key.Key, value); buffer.Append(COMMA); } buffer.Remove(buffer.Length - 1, 1); return(buffer); }
/******************** protected and support methods ***************************************************************************************************************************/ protected virtual OAuthParameters OAuthParametersFactory() { OAuthParameters oparams = new OAuthParameters(); oparams.addParameter(OAUTH_CONSUMER_KEY, ConsumerKey); oparams.addParameter(OAUTH_NONCE, getNonce()); oparams.addParameter(OAUTH_TIMESTAMP, getTimestamp()); oparams.addParameter(OAUTH_SIGNATURE_METHOD, RSA_SHA1); oparams.addParameter(OAUTH_VERSION, ONE_POINT_ZERO); return(oparams); }
/// <summary> /// Setup the HttpWebRequest and connection headers /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <param name="body"></param> /// <returns></returns> private HttpWebRequest setupConnection(String httpsURL, String requestMethod, OAuthParameters oparams, String body) { Uri url = new Uri(httpsURL); HttpWebRequest con = (HttpWebRequest)WebRequest.Create(url); con.Method = requestMethod; con.Headers.Add(AUTHORIZATION, buildAuthHeaderString(httpsURL, requestMethod, oparams)); // Setting the Content Type header depending on the body if (body != null) { con.ContentType = APPLICATION_XML; con.ContentLength = body.Length; } return(con); }
/// <summary> /// Normalize the OAuth parameters as they are added to the signature base string /// </summary> /// <param name="httpUrl"></param> /// <param name="requestParameters"></param> /// <returns></returns> private static String normalizeParameters(String httpUrl, OAuthParameters requestParameters) { // add the querystring to the base string (if one exists) if (httpUrl.IndexOf(QUESTION_MARK) > 0) { NameValueCollection queryParameters = HttpUtility.ParseQueryString(httpUrl.Substring(httpUrl.IndexOf(QUESTION_MARK) + 1)); foreach (string key in queryParameters) { requestParameters.put(key, UrlEncodeRfc3986(queryParameters[key])); } } // piece together the base string, encoding each key and value StringBuilder paramString = new StringBuilder(); foreach (KeyValuePair <string, SortedSet <string> > kvp in requestParameters.getBaseParameters()) { if (kvp.Value.Count == 0) { continue; // Skip if key doesn't have any values } if (paramString.Length > 0) { paramString.Append(AMP); } int tempCounter = 0; foreach (string value in kvp.Value.Keys) { paramString.Append(UrlEncodeRfc3986(kvp.Key)).Append(EQUALS).Append((value)); if (tempCounter != kvp.Value.Count - 1) { paramString.Append(AMP); } tempCounter++; } } return(paramString.ToString()); }
/// <summary> /// Method to build a comma delimited list of key/value string for the signature base string and authorization header /// </summary> /// <param name="buffer"></param> /// <param name="oparams"></param> /// <returns></returns> private StringBuilder parseParameters(StringBuilder buffer, OAuthParameters oparams) { foreach (KeyValuePair<string, SortedSet<string>> key in oparams.getBaseParameters()) { SortedSet<string> value = key.Value; parseSortedSetValues(buffer, key.Key, value); buffer.Append(COMMA); } buffer.Remove(buffer.Length - 1, 1); return buffer; }
/// <summary> /// Generates the signature base string /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <returns></returns> private string generateSignatureBaseString(string httpsURL, string requestMethod, OAuthParameters oparams) { Uri requestUri = parseUrl(httpsURL); String encodedBaseString; encodedBaseString = UrlEncodeRfc3986(requestMethod.ToUpper()) + AMP + UrlEncodeRfc3986(normalizeUrl(requestUri)) + AMP + UrlEncodeRfc3986(normalizeParameters(httpsURL, oparams)); return encodedBaseString; }
/// <summary> /// Generates and signs the signature base string /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <returns></returns> private string generateAndSignSignature(String httpsURL, String requestMethod, OAuthParameters oparams) { OAuthParameters sbsParams = new OAuthParameters(); sbsParams.putAll(oparams.getBaseParameters()); string realm = null; if (sbsParams.get(REALM) != EMPTY_STRING) { realm = sbsParams.get(REALM); sbsParams.remove(REALM, null); } String baseString; baseString = generateSignatureBaseString(httpsURL, requestMethod, sbsParams); _signatureBaseString = baseString; String signature; signature = sign(baseString, privateKey); oparams.addParameter(OAUTH_SIGNATURE, signature); if (realm != null) { sbsParams.put(REALM, realm); } return signature; }
/// <summary> /// Builds the Authorization header /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <returns></returns> private string buildAuthHeaderString(String httpsURL, String requestMethod, OAuthParameters oparams) { generateAndSignSignature(httpsURL, requestMethod, oparams); StringBuilder buffer = new StringBuilder(); buffer.Append(OAUTH_START_STRING); buffer = parseParameters(buffer, oparams); this._authHeader = buffer.ToString(); return buffer.ToString(); }
/// <summary> /// Normalize the OAuth parameters as they are added to the signature base string /// </summary> /// <param name="httpUrl"></param> /// <param name="requestParameters"></param> /// <returns></returns> private static String normalizeParameters(String httpUrl, OAuthParameters requestParameters) { // add the querystring to the base string (if one exists) if (httpUrl.IndexOf(QUESTION_MARK) > 0) { NameValueCollection queryParameters = HttpUtility.ParseQueryString(httpUrl.Substring(httpUrl.IndexOf(QUESTION_MARK) + 1)); foreach (string key in queryParameters) { requestParameters.put(key, UrlEncodeRfc3986(queryParameters[key])); } } // piece together the base string, encoding each key and value StringBuilder paramString = new StringBuilder(); foreach (KeyValuePair<string, SortedSet<string>> kvp in requestParameters.getBaseParameters()) { if (kvp.Value.Count == 0) { continue; // Skip if key doesn't have any values } if (paramString.Length > 0) { paramString.Append(AMP); } int tempCounter = 0; foreach (string value in kvp.Value.Keys) { paramString.Append(UrlEncodeRfc3986(kvp.Key)).Append(EQUALS).Append((value)); if (tempCounter != kvp.Value.Count - 1) { paramString.Append(AMP); } tempCounter++; } } return paramString.ToString(); }
/******************** protected and support methods ***************************************************************************************************************************/ protected virtual OAuthParameters OAuthParametersFactory() { OAuthParameters oparams = new OAuthParameters(); oparams.addParameter(OAUTH_CONSUMER_KEY, ConsumerKey); oparams.addParameter(OAUTH_NONCE, getNonce()); oparams.addParameter(OAUTH_TIMESTAMP, getTimestamp()); oparams.addParameter(OAUTH_SIGNATURE_METHOD, RSA_SHA1); oparams.addParameter(OAUTH_VERSION, ONE_POINT_ZERO); return oparams; }
/* -------- private Methods ------------------------------------------------------------------------------------------------------------------ */ /// <summary> /// Method to add the Oauth body hash to the OAuthParameters /// </summary> /// <param name="body"></param> /// <param name="oparams"></param> /// <returns></returns> private OAuthParameters setOauthBodyHashParameter(string body, OAuthParameters oparams) { byte[] bodyStringBytes = encoder.GetBytes(body); using (var sha = new SHA1CryptoServiceProvider()) { try { string encodedHash = Convert.ToBase64String(sha.ComputeHash(bodyStringBytes)); oparams.addParameter(OAUTH_BODY_HASH, encodedHash); return oparams; } catch (CryptographicException cex) { throw new MCApiRuntimeException(cex.Message, cex); } } }
/// <summary> /// Generates the signature base string /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <returns></returns> private string generateSignatureBaseString(string httpsURL, string requestMethod, OAuthParameters oparams) { Uri requestUri = parseUrl(httpsURL); String encodedBaseString; encodedBaseString = UrlEncodeRfc3986(requestMethod.ToUpper()) + AMP + UrlEncodeRfc3986(normalizeUrl(requestUri)) + AMP + UrlEncodeRfc3986(normalizeParameters(httpsURL, oparams)); return(encodedBaseString); }
/// <summary> /// Generates and signs the signature base string /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <returns></returns> private string generateAndSignSignature(String httpsURL, String requestMethod, OAuthParameters oparams) { OAuthParameters sbsParams = new OAuthParameters(); sbsParams.putAll(oparams.getBaseParameters()); string realm = null; if (sbsParams.get(REALM) != EMPTY_STRING) { realm = sbsParams.get(REALM); sbsParams.remove(REALM, null); } String baseString; baseString = generateSignatureBaseString(httpsURL, requestMethod, sbsParams); _signatureBaseString = baseString; String signature; signature = sign(baseString, privateKey); oparams.addParameter(OAUTH_SIGNATURE, signature); if (realm != null) { sbsParams.put(REALM, realm); } return(signature); }
/* -------- private Methods ------------------------------------------------------------------------------------------------------------------ */ /// <summary> /// Method to add the Oauth body hash to the OAuthParameters /// </summary> /// <param name="body"></param> /// <param name="oparams"></param> /// <returns></returns> private OAuthParameters setOauthBodyHashParameter(String body, OAuthParameters oparams) { byte[] bodyStringBytes = encoder.GetBytes(body); SHA1 sha = new SHA1CryptoServiceProvider(); string encodedHash = Convert.ToBase64String(sha.ComputeHash(bodyStringBytes)); oparams.addParameter(OAUTH_BODY_HASH, encodedHash); return oparams; }
/// <summary> /// Setup the HttpWebRequest and connection headers /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <param name="body"></param> /// <returns></returns> private HttpWebRequest setupConnection(String httpsURL, String requestMethod, OAuthParameters oparams, String body) { Uri url = new Uri(httpsURL); HttpWebRequest con = (HttpWebRequest)WebRequest.Create(url); con.Method = requestMethod; con.Headers.Add(AUTHORIZATION, buildAuthHeaderString(httpsURL, requestMethod, oparams)); // Setting the Content Type header depending on the body if (body != null) { con.ContentType = APPLICATION_XML; con.ContentLength = body.Length; } return con; }
/// <summary> /// Method to handle all Api connection details. /// </summary> /// <param name="httpsURL"></param> /// <param name="requestMethod"></param> /// <param name="oparams"></param> /// <param name="body"></param> /// <returns></returns> protected Dictionary<string, string> doRequest(String httpsURL, String requestMethod, OAuthParameters oparams, String body) { try { if (privateKey == null) { throw new MCApiRuntimeException(NULL_PRIVATEKEY_ERROR_MESSAGE); } if (body != null && body.Length > 0) { oparams = setOauthBodyHashParameter(body, oparams); } HttpWebRequest con = setupConnection(httpsURL, requestMethod, oparams, body); if (body != null) { writeBodyToConnection(body, con); } return checkForErrorsAndReturnRepsonse(con); } catch (Exception e) { throw new MCApiRuntimeException(e.Message, e); } }