Exemplo n.º 1
0
        private string GenerateSignature(HttpWebRequest request, FormParameters formParameters)
        {
            FormParameters parameters = FormParameters.OAuthDecode(request.RequestUri.Query);

            foreach (var item in _protocolParameters)
            {
                parameters.Add(item);
            }
            if (request.ContentType == "application/x-www-form-urlencoded")
            {
                foreach (var item in formParameters)
                {
                    parameters.Add(item);
                }
            }
            parameters.Sort();

            string signature = GenerateBaseSignature(request.Method, request.RequestUri, parameters);
            string secret    = EncodeSecret();

            Trace.WriteLine("Secret: " + secret);
            Trace.WriteLine("Signature: " + signature);

            return(Sign(secret, signature));
        }
Exemplo n.º 2
0
        private void SetTempToken()
        {
            FormParameters form = new FormParameters();

            if (TempTokenRequesting != null)
            {
                TempTokenRequesting(this, new OAuthRequestTokenEventArgs(form));
            }
            //_protocolParameters.Set("oauth_callback", CallbackUrl);
            form.Add("oauth_callback", CallbackUrl);

            // TODO: check TempTokenMethod

            HttpWebResponse response = AuthorizedPOST(TemporaryCredentialsUri, form);

            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                string         text  = sr.ReadToEnd();
                FormParameters reply = FormParameters.OAuthDecode(text);
                AuthorizedToken       = reply["oauth_token"];
                AuthorizedTokenSecret = reply["oauth_token_secret"];

                // TODO pass additional parameters to impl
            }
        }
Exemplo n.º 3
0
 public OAuthClientBase(string consumerKey, string sharedSecret, string callbackUrl, string authorizedToken, string authorizedTokenSecret)
 {
     ConsumerKey           = consumerKey;
     SharedSecret          = sharedSecret;
     CallbackUrl           = callbackUrl;
     AuthorizedToken       = authorizedToken;
     AuthorizedTokenSecret = authorizedTokenSecret;
     _protocolParameters   = new FormParameters();
 }
Exemplo n.º 4
0
        private HttpWebResponse ResendRequest(HttpWebRequest request, byte[] data)
        {
            if (request.Method == "GET")
            {
                return(AuthorizedGET(request.RequestUri));
            }

            if (request.Method == "POST" && request.ContentType == "application/x-www-form-urlencoded")
            {
                FormParameters form = FormParameters.FormDecode(Encoding.UTF8.GetString(data));
                return(AuthorizedPOST(request.RequestUri, form));
            }

            throw new NotImplementedException();
        }
Exemplo n.º 5
0
        public void Verify(string verifier)
        {
            _protocolParameters.Set("oauth_verifier", verifier);

            // TODO: check AccessTokenMethod

            HttpWebResponse response = AuthorizedPOST(AccessTokenUri);

            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                var            text  = sr.ReadToEnd();
                FormParameters reply = FormParameters.OAuthDecode(text);
                AuthorizedToken       = reply["oauth_token"];
                AuthorizedTokenSecret = reply["oauth_token_secret"];

                // TODO pass additional parameters to impl
            }
        }
Exemplo n.º 6
0
        private static string GenerateBaseSignature(string method, Uri uri, FormParameters sortedParameters)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(Encode(method.ToUpper()));
            sb.Append('&');
            sb.Append(Encode(uri.Scheme.ToLower()));
            sb.Append(Encode("://"));
            sb.Append(Encode(uri.Host.ToLower()));
            if (!uri.IsDefaultPort)
            {
                sb.Append(uri.Port);
            }
            sb.Append(Encode(uri.AbsolutePath));
            sb.Append('&');
            sb.Append(sortedParameters.SignatureEncode());
            return(sb.ToString());
        }
Exemplo n.º 7
0
        public static FormParameters FormDecode(string message)
        {
            FormParameters form = new FormParameters();

            if (String.IsNullOrWhiteSpace(message))
            {
                return(form);
            }
            foreach (var pair in message.Split('&'))
            {
                string[] parts = pair.Split('=');
                string   key   = HttpUtility.UrlDecode(parts[0], Encoding.UTF8);
                string   value = parts.Length > 1
                    ? HttpUtility.UrlDecode(parts[1], Encoding.UTF8)
                    : String.Empty;
                form.Add(key, value);
            }
            return(form);
        }
Exemplo n.º 8
0
        public static FormParameters OAuthDecode(string message)
        {
            FormParameters form = new FormParameters();

            message = message.TrimStart('?');
            if (String.IsNullOrWhiteSpace(message))
            {
                return(form);
            }
            foreach (var pair in message.Split('&'))
            {
                string[] parts = pair.Split('=');
                string   key   = OAuthClientBase.Decode(parts[0]);
                string   value = parts.Length > 1
                    ? OAuthClientBase.Decode(parts[1])
                    : String.Empty;
                form.Add(key, value);
            }
            return(form);
        }
Exemplo n.º 9
0
        protected void Authorize(HttpWebRequest request, FormParameters parameters = null)
        {
            if (parameters == null)
            {
                parameters = new FormParameters();
            }

            _protocolParameters.Set("oauth_consumer_key", ConsumerKey);
            _protocolParameters.Set("oauth_token", AuthorizedToken);
            _protocolParameters.Set("oauth_signature_method", SignatureMethod);
            _protocolParameters.Set("oauth_timestamp", GenerateTimestamp());
            _protocolParameters.Set("oauth_nonce", GenerateNonce());
            _protocolParameters.Set("oauth_version", "1.0");
            _protocolParameters.Set("oauth_signature", GenerateSignature(request, parameters));

            string auth = _protocolParameters.HeaderEncode();

            Trace.WriteLine("Authorization: " + auth);
            request.Headers["Authorization"] = auth;
        }
Exemplo n.º 10
0
        protected HttpWebResponse AuthorizedPOST(Uri uri, FormParameters parameters = null)
        {
            Trace.WriteLine("POST " + uri);

            if (parameters == null)
            {
                parameters = new FormParameters();
            }

            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            Authorize(request, parameters);

            string data = parameters.FormEncode();

            Trace.WriteLine("POST payload: " + data);
            return(GetResponse(request, Encoding.UTF8.GetBytes(data)));
        }
Exemplo n.º 11
0
 public OAuthRequestTokenEventArgs(FormParameters form)
 {
     Parameters = form;
 }