Esempio n. 1
0
        /// <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="signatureType">The type of signature to use</param>
        /// <returns>A base64 string of the hash value</returns>
        public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string verifier, string callback, string httpMethod, string timeStamp, string nonce, OAuthSignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters)
        {
            normalizedUrl = null;
            normalizedRequestParameters = null;

            switch (signatureType)
            {
            case OAuthSignatureTypes.PLAINTEXT:
                return(HttpUtility.UrlEncode(string.Format(CultureInfo.InvariantCulture, "{0}&{1}", consumerSecret, tokenSecret)));

            case OAuthSignatureTypes.HMACSHA1:
                string   signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, verifier, callback, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, out normalizedUrl, out normalizedRequestParameters);
                HMACSHA1 hmacsha1      = new HMACSHA1();
                hmacsha1.Key = Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0}&{1}", UrlEncode(consumerSecret), UrlEncode(tokenSecret)));
                return(GenerateSignatureUsingHash(signatureBase, hmacsha1));

            case OAuthSignatureTypes.RSASHA1:
                throw new NotImplementedException();

            default:
                throw new ArgumentException("Unknown signature type", "signatureType");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Generates a signature using the specified signatureType 
        /// </summary>
        /// <param name="request">Request details</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="callback">Redirect URL for Web apps</param>
        /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param>
        /// <param name="nonce">Unique value for this particular request</param>
        /// <param name="signatureType">The type of signature to use</param>
        /// <param name="verifier">Number if using PIN authorization</param>
        /// <param name="timeStamp">Timestamp for this request</param>
        /// <param name="normalizedUrl">Url returned to caller</param>
        /// <param name="normalizedRequestParameters">Parameters returned to caller</param>
        /// <returns>A base64 string of the hash value</returns>
        public string GenerateSignature(Request request, string consumerKey, string consumerSecret, string token, string tokenSecret, string verifier, string callback, string httpMethod, string timeStamp, string nonce, OAuthSignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters)
        {
            normalizedUrl = null;
            normalizedRequestParameters = null;

            switch (signatureType)
            {
                case OAuthSignatureTypes.Plaintext:
                    return BuildUrlHelper.UrlEncode(
                        string.Format(CultureInfo.InvariantCulture, "{0}&{1}", consumerSecret, tokenSecret));
                case OAuthSignatureTypes.Hmacsha1:
                    string signatureBase = GenerateSignatureBase(request, consumerKey, token, tokenSecret, verifier, callback, httpMethod, timeStamp, nonce, Hmacsha1SignatureType, out normalizedUrl, out normalizedRequestParameters);
#if NETFX_CORE
                    string hashKey =
                        string.Format(
                            CultureInfo.InvariantCulture, "{0}&{1}",
                            BuildUrlHelper.UrlEncode(consumerSecret),
                            BuildUrlHelper.UrlEncode(tokenSecret));
                    return HashWith(signatureBase, hashKey);
#else
                var hmacsha1 = new HMACSHA1
                    {
                        Key =
                            Encoding.UTF8.GetBytes(
                                string.Format(
                                    CultureInfo.InvariantCulture, "{0}&{1}", 
                                    BuildUrlHelper.UrlEncode(consumerSecret),
                                    BuildUrlHelper.UrlEncode(tokenSecret)))
                    };
                    return ComputeHash(hmacsha1, signatureBase);
#endif
                case OAuthSignatureTypes.Rsasha1:
                    throw new NotImplementedException();
                default:
                    throw new ArgumentException("Unknown signature type", "signatureType");
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Generates a signature using the specified signatureType
        /// </summary>
        /// <param name="request">Request details</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="callback">Redirect URL for Web apps</param>
        /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param>
        /// <param name="nonce">Unique value for this particular request</param>
        /// <param name="signatureType">The type of signature to use</param>
        /// <param name="verifier">Number if using PIN authorization</param>
        /// <param name="timeStamp">Timestamp for this request</param>
        /// <param name="normalizedUrl">Url returned to caller</param>
        /// <param name="normalizedRequestParameters">Parameters returned to caller</param>
        /// <returns>A base64 string of the hash value</returns>
        public string GenerateSignature(Request request, string consumerKey, string consumerSecret, string token, string tokenSecret, string verifier, string callback, string httpMethod, string timeStamp, string nonce, OAuthSignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters)
        {
            normalizedUrl = null;
            normalizedRequestParameters = null;

            switch (signatureType)
            {
            case OAuthSignatureTypes.Plaintext:
                return(BuildUrlHelper.UrlEncode(
                           string.Format(CultureInfo.InvariantCulture, "{0}&{1}", consumerSecret, tokenSecret)));

            case OAuthSignatureTypes.Hmacsha1:
                string signatureBase = GenerateSignatureBase(request, consumerKey, token, tokenSecret, verifier, callback, httpMethod, timeStamp, nonce, Hmacsha1SignatureType, out normalizedUrl, out normalizedRequestParameters);
#if NETFX_CORE
                string hashKey =
                    string.Format(
                        CultureInfo.InvariantCulture, "{0}&{1}",
                        BuildUrlHelper.UrlEncode(consumerSecret),
                        BuildUrlHelper.UrlEncode(tokenSecret));
                return(HashWith(signatureBase, hashKey));
#else
                var hmacsha1 = new HMACSHA1
                {
                    Key =
                        Encoding.UTF8.GetBytes(
                            string.Format(
                                CultureInfo.InvariantCulture, "{0}&{1}",
                                BuildUrlHelper.UrlEncode(consumerSecret),
                                BuildUrlHelper.UrlEncode(tokenSecret)))
                };
                return(ComputeHash(hmacsha1, signatureBase));
#endif
            case OAuthSignatureTypes.Rsasha1:
                throw new NotImplementedException();

            default:
                throw new ArgumentException("Unknown signature type", "signatureType");
            }
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="command"></param>
        /// <param name="signatureType"></param>
        /// <returns></returns>
        public static SignatureInfo GenerateSignature(Uri url, GetRequestTokenCommand command, OAuthSignatureTypes signatureType)
        {
            var           cm = command;
            SignatureInfo si = new SignatureInfo();

            switch (signatureType)
            {
            case OAuthSignatureTypes.PLAINTEXT:
                si.Signature = OAuth1Client.UrlEncode(String.Format("{0}&{1}", cm.ConsumerKeySecret, cm.TokenSecret));
                return(si);

            case OAuthSignatureTypes.HMACSHA1:
                si = GenerateSignatureBase(url, cm, Key.HMACSHA1SignatureType);
                String key = String.Format("{0}&{1}"
                                           , OAuth1Client.UrlEncode(cm.ConsumerKeySecret), String.IsNullOrEmpty(cm.TokenSecret) ? "" : OAuth1Client.UrlEncode(cm.TokenSecret));
                si.Signature = GenerateSignatureUsingHash(key, si.Signature);
                return(si);

            case OAuthSignatureTypes.RSASHA1: throw new NotImplementedException();
            }
            throw new ArgumentException("Unknown signature type", "signatureType");
        }