/// <summary>
        /// Generates the url which the user should visit in order to authenticate and
        /// authorize with the Service Provider.
        /// When successful, updates the OAuthParameter instance passed as parameter by setting
        /// Token and TokenSecret.
        /// </summary>
        /// <param name="parameters">The OAuth parameters</param>
        /// <returns>The full authorization url the user should visit</returns>
        public static string CreateUserAuthorizationUrl(OAuthParameters parameters)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(userAuthorizationUrl);
            sb.AppendFormat("?{0}={1}", OAuthBase.OAuthTokenKey, OAuthBase.EncodingPerRFC3986(parameters.Token));
            if (!string.IsNullOrEmpty(parameters.Callback))
            {
                sb.AppendFormat("&{0}={1}", OAuthBase.OAuthCallbackKey, OAuthBase.EncodingPerRFC3986(parameters.Callback));
            }

            return(sb.ToString());
        }
        /// <summary>
        /// Generates an OAuth header.
        /// </summary>
        /// <param name="uri">The URI of the request</param>
        /// <param name="consumerKey">The consumer key</param>
        /// <param name="consumerSecret">The consumer secret</param>
        /// <param name="token">The OAuth token</param>
        /// <param name="tokenSecret">The OAuth token secret</param>
        /// <param name="httpMethod">The http method</param>
        /// <returns>The OAuth authorization header</returns>
        public static string GenerateHeader(Uri uri, String consumerKey, String consumerSecret, String token,
                                            String tokenSecret, String httpMethod)
        {
            OAuthParameters parameters = new OAuthParameters()
            {
                ConsumerKey     = consumerKey,
                ConsumerSecret  = consumerSecret,
                Token           = token,
                TokenSecret     = tokenSecret,
                SignatureMethod = OAuthBase.HMACSHA1SignatureType
            };

            return(GenerateHeader(uri, httpMethod, parameters));
        }
 /// <summary>
 /// a constructor for 3-legged OAuth
 /// </summary>
 /// <param name="applicationName">The name of the application</param>
 /// <param name="consumerKey">the consumerKey to use</param>
 /// <param name="consumerSecret">the consumerSecret to use</param>
 /// <param name="token">The token to be used</param>
 /// <param name="tokenSecret">The tokenSecret to be used</param>
 /// <returns></returns>
 public OAuth3LeggedAuthenticator(
     string applicationName,
     string consumerKey,
     string consumerSecret,
     string token,
     string tokenSecret,
     string scope,
     string signatureMethod)
     : base(applicationName, consumerKey, consumerSecret)
 {
     _parameters = new OAuthParameters()
     {
         ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, Token = token, TokenSecret = tokenSecret, Scope = scope, SignatureMethod = signatureMethod
     };
 }
        /// <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="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="timeStamp">The OAuth timestamp. Must be a valid timestamp and equal or greater than
        /// timestamps used in previous requests</param>
        /// <param name="nonce">The OAuth nonce. A random string uniquely generated for each request</param>
        /// <param name="signatureType">The signature type.</param>
        /// <returns>The signature base</returns>
        public static string GenerateSignatureBase(Uri url, string consumerKey, string token, string tokenSecret,
                                                   string httpMethod, string timeStamp, string nonce, string signatureType)
        {
            OAuthParameters parameters = new OAuthParameters()
            {
                ConsumerKey     = consumerKey,
                Token           = token,
                TokenSecret     = tokenSecret,
                Timestamp       = timeStamp,
                Nonce           = nonce,
                SignatureMethod = signatureType
            };

            return(GenerateSignatureBase(url, httpMethod, parameters));
        }
        /// <summary>
        /// Helper method which parses a querystring for the OAuth related parameters.
        /// It updates the OAuthParameter instance passed as parameter by setting
        /// Token, TokenSecret and Verifier (if present).
        /// </summary>
        /// <param name="parameters">The OAuth parameters</param>
        public static void UpdateOAuthParametersFromCallback(string queryString, OAuthParameters parameters)
        {
            // split results and update parameters
            SortedDictionary <string, string> responseValues = OAuthBase.GetQueryParameters(queryString);

            parameters.Token = responseValues[OAuthBase.OAuthTokenKey];
            if (responseValues.ContainsKey(OAuthBase.OAuthTokenSecretKey))
            {
                parameters.TokenSecret = responseValues[OAuthBase.OAuthTokenSecretKey];
            }

            if (responseValues.ContainsKey(OAuthBase.OAuthVerifierKey))
            {
                parameters.Verifier = responseValues[OAuthBase.OAuthVerifierKey];
            }
        }
 /// <summary>
 /// a constructor for 2-legged OAuth
 /// </summary>
 /// <param name="applicationName">The name of the application</param>
 /// <param name="consumerKey">the consumerKey to use</param>
 /// <param name="consumerSecret">the consumerSecret to use</param>
 /// <param name="user">the username to use</param>
 /// <param name="domain">the domain to use</param>
 /// <returns></returns>
 public OAuth2LeggedAuthenticator(
     string applicationName,
     string consumerKey,
     string consumerSecret,
     string user,
     string domain,
     string signatureMethod)
     : base(applicationName, consumerKey, consumerSecret)
 {
     _oAuthUser   = user;
     _oAuthDomain = domain;
     _parameters  = new OAuthParameters()
     {
         ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, SignatureMethod = signatureMethod
     };
 }
        /// <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 ArgumentNullException("consumerKey");
            }

            if (string.IsNullOrEmpty(httpMethod))
            {
                throw new ArgumentNullException("httpMethod");
            }

            if (string.IsNullOrEmpty(parameters.SignatureMethod))
            {
                throw new ArgumentNullException("signatureMethod");
            }

            string normalizedUrl = null;
            string normalizedRequestParameters = null;

            SortedDictionary <string, string> allParameters = GetQueryParameters(url.Query, parameters.BaseProperties);

            if (!allParameters.ContainsKey(OAuthVersionKey))
            {
                allParameters.Add(OAuthVersionKey, OAuthVersion);
            }

            normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host);
            if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443)))
            {
                normalizedUrl += ":" + url.Port;
            }

            normalizedUrl += url.AbsolutePath;
            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>
        /// Contacts Google for a request token, first step of the OAuth authentication process.
        /// When successful, updates the OAuthParameter instance passed as parameter by setting
        /// Token and TokenSecret.
        /// </summary>
        /// <param name="parameters">The OAuth parameters</param>
        public static void GetUnauthorizedRequestToken(OAuthParameters parameters)
        {
            Uri requestUri = new Uri(string.Format("{0}?scope={1}", requestTokenUrl, OAuthBase.EncodingPerRFC3986(parameters.Scope)));

            // callback is only needed when getting the request token
            bool callbackExists = false;

            if (!string.IsNullOrEmpty(parameters.Callback))
            {
                parameters.BaseProperties.Add(OAuthBase.OAuthCallbackKey, parameters.Callback);
                callbackExists = true;
            }

            string     headers = GenerateHeader(requestUri, "GET", parameters);
            WebRequest request = WebRequest.Create(requestUri);

            request.Headers.Add(headers);

            WebResponse response = request.GetResponse();
            string      result   = "";

            if (response != null)
            {
                Stream       responseStream = response.GetResponseStream();
                StreamReader reader         = new StreamReader(responseStream);
                result = reader.ReadToEnd();
            }

            if (callbackExists)
            {
                parameters.BaseProperties.Remove(OAuthBase.OAuthCallbackKey);
            }

            // split results and update parameters
            SortedDictionary <string, string> responseValues = OAuthBase.GetQueryParameters(result);

            parameters.Token       = responseValues[OAuthBase.OAuthTokenKey];
            parameters.TokenSecret = responseValues[OAuthBase.OAuthTokenSecretKey];
        }
        /// <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(string.Format("{0}&{1}", 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", "signatureType");
            }
        }
示例#10
0
        /// <summary>
        /// overloaded constructor that sets parameters from an OAuthParameter instance.
        /// </summary>
        public GOAuthRequestFactory(string service, string applicationName, OAuthParameters parameters)
            : base(service, applicationName)
        {
            if (parameters.ConsumerKey != null)
            {
                ConsumerKey = parameters.ConsumerKey;
            }

            if (parameters.ConsumerSecret != null)
            {
                ConsumerSecret = parameters.ConsumerSecret;
            }

            if (parameters.Token != null)
            {
                Token = parameters.Token;
            }

            if (parameters.TokenSecret != null)
            {
                TokenSecret = parameters.TokenSecret;
            }
        }