async private Task <String> Login()
        {
            using (HttpClient client = _handler == null ? new HttpClient() : new HttpClient(_handler))
            {
                Random      rand = new Random();
                TwitterAuth auth = new TwitterAuthBuilder(rand)
                                   .SetConsumerKeys(CONSUMER_KEY, CONSUMER_SECRET)
                                   .SetSignatureMethod("HMAC-SHA1")
                                   .SetVersion("1.0")
                                   .SetUrl(API_REQUEST_TOKEN)
                                   .BuildAndSign();

                string        callback = Uri.EscapeUriString("/api/login/callback");
                StringContent content  = new StringContent("{ \"oauth_callback\", \"" + callback + "\"}", Encoding.UTF8, "application/json");
                client.DefaultRequestHeaders.Add("Authorization", auth.AuthenticationHeader);

                HttpResponseMessage response = await client.PostAsync(API_REQUEST_TOKEN, content);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return("");
                }

                // Response looks like: oauth_token=TOKEN&oauth_token_secret=SECRET&oauth_callback_confirmed=true
                return(await response.Content.ReadAsStringAsync());
            }
        }
        async private Task <UserCredential> GetUserTokens(string oauth_token, string oauth_verifier)
        {
            using (HttpClient client = _handler == null ? new HttpClient() : new HttpClient(_handler))
            {
                Random      rand = new Random();
                TwitterAuth auth = new TwitterAuthBuilder(rand)
                                   .SetConsumerKeys(CONSUMER_KEY, CONSUMER_SECRET)
                                   .SetSignatureMethod("HMAC-SHA1")
                                   .SetUrl(API_ACCESS_TOKEN)
                                   .SetVersion("1.0")
                                   .SetOauthToken(oauth_token)
                                   .BuildAndSign();

                List <KeyValuePair <string, string> > data = new List <KeyValuePair <string, string> >();
                data.Add(new KeyValuePair <string, string>("oauth_verifier", oauth_verifier));
                FormUrlEncodedContent content = new FormUrlEncodedContent(data);
                client.DefaultRequestHeaders.Add("oauth_verifier", oauth_verifier);
                client.DefaultRequestHeaders.Add("Authorization", auth.AuthenticationHeader);

                HttpResponseMessage response = await client.PostAsync(API_ACCESS_TOKEN, content);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(null);
                }

                string responseString = await response.Content.ReadAsStringAsync();

                string accessToken  = responseString.Split('&')[0].Split('=')[1];
                string accessSecret = responseString.Split('&')[1].Split('=')[1];
                string screenName   = responseString.Split('&')[3].Split('=')[1];

                User user = _userRepo.Find(u => u.Handle == screenName && u.Type == "ADMIN").SingleOrDefault();
                if (user != null)
                {
                    UserCredential uc = new UserCredential(screenName, accessToken, accessSecret);
                    uc.GenerateHash();
                    streamManager.AddUserCredentials(uc);
                    return(uc);
                }

                return(null);
            }
        }
Exemplo n.º 3
0
        public void CheckSignatureDifferentEachIteration()
        {
            Random      rand = new Random();
            TwitterAuth auth = new TwitterAuthBuilder(rand)
                               .SetConsumerKeys("test", "test2")
                               .SetSignatureMethod("HMAC-SHA1")
                               .SetUrl("http://hdfiusdnf")
                               .SetVersion("1.0")
                               .BuildAndSign();

            TwitterAuth auth2 = new TwitterAuthBuilder(rand)
                                .SetConsumerKeys("test", "test2")
                                .SetSignatureMethod("HMAC-SHA1")
                                .SetUrl("http://hdfiusdnf")
                                .SetVersion("1.0")
                                .BuildAndSign();

            Assert.NotEqual(auth.Signature, auth2.Signature);
        }