예제 #1
0
파일: oauthutil.cs 프로젝트: Zelxin/RPiKeg
        /// <summary>
        /// Generates an OAuth header.
        /// </summary>
        /// <param name="uri">The URI of the request</param>
        /// <param name="httpMethod">The http method</param>
        /// <param name="parameters">The OAuth parameters</param>
        /// <returns>The OAuth authorization header</returns>
        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);

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Authorization: OAuth {0}=\"{1}\",", OAuthBase.OAuthVersionKey, OAuthBase.OAuthVersion);
            sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthNonceKey, OAuthBase.EncodingPerRFC3986(parameters.Nonce));
            sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthTimestampKey, OAuthBase.EncodingPerRFC3986(parameters.Timestamp));
            sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthConsumerKeyKey, OAuthBase.EncodingPerRFC3986(parameters.ConsumerKey));
            if (parameters.BaseProperties.ContainsKey(OAuthBase.OAuthVerifierKey)) {
                sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthVerifierKey, OAuthBase.EncodingPerRFC3986(parameters.BaseProperties[OAuthBase.OAuthVerifierKey]));
            }
            if (!String.IsNullOrEmpty(parameters.Token)) {
                sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthTokenKey, OAuthBase.EncodingPerRFC3986(parameters.Token));
            }
            if (parameters.BaseProperties.ContainsKey(OAuthBase.OAuthCallbackKey)) {
                sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthCallbackKey, OAuthBase.EncodingPerRFC3986(parameters.BaseProperties[OAuthBase.OAuthCallbackKey]));
            }
            sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthSignatureMethodKey, OAuthBase.HMACSHA1SignatureType);
            sb.AppendFormat("{0}=\"{1}\"", OAuthBase.OAuthSignatureKey, OAuthBase.EncodingPerRFC3986(signature));

            return sb.ToString();
        }
예제 #2
0
파일: oauthutil.cs 프로젝트: Zelxin/RPiKeg
 /// <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);
 }
예제 #3
0
        /// <summary>
        /// Generates an OAuth header.
        /// </summary>
        /// <param name="uri">The URI of the request</param>
        /// <param name="httpMethod">The http method</param>
        /// <param name="parameters">The OAuth parameters</param>
        /// <returns>The OAuth authorization header</returns>
        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);

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("Authorization: OAuth {0}=\"{1}\",", OAuthBase.OAuthVersionKey, OAuthBase.OAuthVersion);
            sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthNonceKey, OAuthBase.EncodingPerRFC3986(parameters.Nonce));
            sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthTimestampKey, OAuthBase.EncodingPerRFC3986(parameters.Timestamp));
            sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthConsumerKeyKey, OAuthBase.EncodingPerRFC3986(parameters.ConsumerKey));
            if (parameters.BaseProperties.ContainsKey(OAuthBase.OAuthVerifierKey))
            {
                sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthVerifierKey, OAuthBase.EncodingPerRFC3986(parameters.BaseProperties[OAuthBase.OAuthVerifierKey]));
            }
            if (!String.IsNullOrEmpty(parameters.Token))
            {
                sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthTokenKey, OAuthBase.EncodingPerRFC3986(parameters.Token));
            }
            if (parameters.BaseProperties.ContainsKey(OAuthBase.OAuthCallbackKey))
            {
                sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthCallbackKey, OAuthBase.EncodingPerRFC3986(parameters.BaseProperties[OAuthBase.OAuthCallbackKey]));
            }
            sb.AppendFormat("{0}=\"{1}\",", OAuthBase.OAuthSignatureMethodKey, OAuthBase.HMACSHA1SignatureType);
            sb.AppendFormat("{0}=\"{1}\"", OAuthBase.OAuthSignatureKey, OAuthBase.EncodingPerRFC3986(signature));

            return(sb.ToString());
        }
예제 #4
0
        /// <summary>
        /// Exchanges the user-authorized request token for an access token.
        /// 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 GetAccessToken(OAuthParameters parameters)
        {
            Uri requestUri = new Uri(accessTokenUrl);

            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();
            }

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

            parameters.Token       = responseValues[OAuthBase.OAuthTokenKey];
            parameters.TokenSecret = responseValues[OAuthBase.OAuthTokenSecretKey];
        }
예제 #5
0
        /// <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));
        }
예제 #6
0
        /// <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));
        }
예제 #7
0
        /// <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>
 /// 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)
 {
     this.parameters = new OAuthParameters()
     {
         ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, Token = token, TokenSecret = tokenSecret, Scope = scope, SignatureMethod = signatureMethod
     };
 }
예제 #9
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) {
         this.ConsumerKey = parameters.ConsumerKey;
     }
     if (parameters.ConsumerSecret != null) {
         this.ConsumerSecret = parameters.ConsumerSecret;
     }
     if (parameters.Token != null) {
         this.Token = parameters.Token;
     }
     if (parameters.TokenSecret != null) {
         this.TokenSecret = parameters.TokenSecret;
     }
 }
예제 #10
0
        /// <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)
 {
     this.oAuthUser   = user;
     this.oAuthDomain = domain;
     this.parameters  = new OAuthParameters()
     {
         ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, SignatureMethod = signatureMethod
     };
 }
예제 #12
0
        /// <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");
            }
        }
예제 #13
0
        /// <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());
        }
예제 #14
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)
     {
         this.ConsumerKey = parameters.ConsumerKey;
     }
     if (parameters.ConsumerSecret != null)
     {
         this.ConsumerSecret = parameters.ConsumerSecret;
     }
     if (parameters.Token != null)
     {
         this.Token = parameters.Token;
     }
     if (parameters.TokenSecret != null)
     {
         this.TokenSecret = parameters.TokenSecret;
     }
 }
예제 #15
0
        /// <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];
        }
예제 #16
0
        public void OAuth3LeggedAuthenticationTest() {
            Tracing.TraceMsg("Entering OAuth3LeggedAuthenticationTest");

            CalendarService service = new CalendarService("OAuthTestcode");

            OAuthParameters parameters = new OAuthParameters() {
                ConsumerKey = this.oAuthConsumerKey,
                ConsumerSecret = this.oAuthConsumerSecret,
                Token = this.oAuthToken,
                TokenSecret = this.oAuthTokenSecret,
                Scope = this.oAuthScope,
                SignatureMethod = this.oAuthSignatureMethod
            };

            GOAuthRequestFactory requestFactory = new GOAuthRequestFactory("cl", "OAuthTestcode", parameters);
            service.RequestFactory = requestFactory;

            CalendarEntry calendar = new CalendarEntry();
            calendar.Title.Text = "Test OAuth";

            Uri postUri = new Uri("https://www.google.com/calendar/feeds/default/owncalendars/full");
            CalendarEntry createdCalendar = (CalendarEntry)service.Insert(postUri, calendar);

            // delete the new entry
            createdCalendar.Delete();
        }
예제 #17
0
        /// <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");
            }
        }
예제 #18
0
 /// <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="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="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="signatureMethod">The type of signature to use</param>
 /// <returns>A base64 string of the hash value</returns>
 public static string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token,
     string tokenSecret, string httpMethod, string timeStamp, string nonce, string signatureMethod) {
     OAuthParameters parameters = new OAuthParameters() {
         ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, Token = token, TokenSecret = tokenSecret,
         Timestamp = timeStamp, Nonce = nonce, SignatureMethod = signatureMethod
     };
     return GenerateSignature(url, httpMethod, parameters);
 }
예제 #19
0
        /// <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();
        }
예제 #20
0
파일: oauthutil.cs 프로젝트: Zelxin/RPiKeg
        /// <summary>
        /// Exchanges the user-authorized request token for an access token.
        /// 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 GetAccessToken(OAuthParameters parameters) {
            Uri requestUri = new Uri(accessTokenUrl);

            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();
            }

            //split results and update parameters
            SortedDictionary<string, string> responseValues = OAuthBase.GetQueryParameters(result);
            parameters.Token = responseValues[OAuthBase.OAuthTokenKey];
            parameters.TokenSecret = responseValues[OAuthBase.OAuthTokenSecretKey];
        }
예제 #21
0
파일: oauthutil.cs 프로젝트: Zelxin/RPiKeg
 /// <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];
     }
 }
예제 #22
0
파일: oauthutil.cs 프로젝트: Zelxin/RPiKeg
 /// <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();
 }
예제 #23
0
파일: oauthutil.cs 프로젝트: Zelxin/RPiKeg
        /// <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];
        }
예제 #24
0
 /// <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) {
     this.parameters = new OAuthParameters() { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, Token = token, TokenSecret = tokenSecret, Scope = scope, SignatureMethod = signatureMethod };
 }
예제 #25
0
 /// <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) {
     this.oAuthUser = user;
     this.oAuthDomain = domain;
     this.parameters = new OAuthParameters() { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, SignatureMethod = signatureMethod };
 }