Ejemplo n.º 1
0
        public static AccessToken ExchangeForAccessToken(OAuthConsumerConfig config, string accessTokenUrl, string authToken, string tokenSecret, string verifier)
        {
            ValidateArguments(config);

            AccessToken result = new AccessToken();

            string timeStamp = _helpers.BuildTimestamp();
            string nonce = _helpers.BuildNonce();

            List<KeyValuePair<string, string>> requestParams = new List<KeyValuePair<string, string>>();

            requestParams.Add(new KeyValuePair<string, string>("oauth_consumer_key", config.ConsumerKey));
            requestParams.Add(new KeyValuePair<string, string>("oauth_nonce", nonce));
            requestParams.Add(new KeyValuePair<string, string>("oauth_signature_method", SIGNATURE_METHOD));
            requestParams.Add(new KeyValuePair<string, string>("oauth_timestamp", timeStamp));
            requestParams.Add(new KeyValuePair<string, string>("oauth_token", authToken));
            requestParams.Add(new KeyValuePair<string, string>("oauth_verifier", verifier));
            requestParams.Add(new KeyValuePair<string, string>("oauth_version", OAUTH_VERSION));

            string response = _requestImplementation.BuildAndExecuteRequest(accessTokenUrl, config.ConsumerSecret + "&" + tokenSecret, requestParams);

            Dictionary<string, string> args = _helpers.SplitResponseParams(response);

            if (args.ContainsKey("oauth_token")) result.OAuthToken = args["oauth_token"];
            if (args.ContainsKey("oauth_token_secret")) result.OAuthTokenSecret = args["oauth_token_secret"];

            return result;
        }
    protected void btnGenerateAuthorizeLink_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(txtConsumerKey.Text)) return;
        if (String.IsNullOrEmpty(txtConsumerSecret.Text)) return;

        OAuthConsumerConfig config = new OAuthConsumerConfig();
        config.ConsumerKey = txtConsumerKey.Text;
        config.ConsumerSecret = txtConsumerSecret.Text;

        List<KeyValuePair<string, string>> authArgs = null;

        if (PageProviderConfig.RequestLevelMode == RequestLevelModes.AtRequestToken)
        {
            string[] args = PageProviderConfig.RequestLevel.Split('=');

            authArgs = new List<KeyValuePair<string, string>>();
            authArgs.Add(new KeyValuePair<string, string>(args[0], args[1]));
        }

        AuthRequestResult authRequest = OAuthClient.GenerateUnauthorizedRequestToken(config, PageProviderConfig.RequestTokenUrl, PageProviderConfig.UserAuthUrl, authArgs);

        SetSessionValue("OAuthTokenSecret", authRequest.OAuthTokenSecret);
        SetSessionValue("ConsumerKey", txtConsumerKey.Text);
        SetSessionValue("ConsumerSecret", txtConsumerSecret.Text);

        lnkAuth.NavigateUrl = authRequest.AuthUrl + (PageProviderConfig.RequestLevelMode == RequestLevelModes.AtUserAuthPage ? "&" + PageProviderConfig.RequestLevel : String.Empty);
        ltlTokenSecret.Text = authRequest.OAuthTokenSecret;
        pnlAuthLink.Visible = true;
    }
    protected void btnExchange_Click(object sender, EventArgs e)
    {
        OAuthConsumerConfig config = new OAuthConsumerConfig();
        config.ConsumerKey = txtConsumerKey.Text;
        config.ConsumerSecret = txtConsumerSecret.Text;

        AccessToken accessToken = OAuthClient.ExchangeForAccessToken(config, PageProviderConfig.AccessTokenUrl, ltlOAuthToken.Text, ltlTokenSecretCallback.Text, ltlOAuthVerifier.Text);

        pnlAccessToken.Visible = true;
        ltlAccessToken.Text = accessToken.OAuthToken;
        ltlAccessTokenSecret.Text = accessToken.OAuthTokenSecret;
    }
Ejemplo n.º 4
0
        public static AuthRequestResult GenerateUnauthorizedRequestToken(OAuthConsumerConfig config, string requestTokenUrl, string userAuthUrl, List<KeyValuePair<string, string>> authArgs = null)
        {
            ValidateArguments(config);

            AuthRequestResult result = new AuthRequestResult();

            string timeStamp = _helpers.BuildTimestamp();
            string nonce = _helpers.BuildNonce();

            List<KeyValuePair<string, string>> requestParams = authArgs ?? new List<KeyValuePair<string, string>>();
            requestParams.Add(new KeyValuePair<string, string>("oauth_consumer_key", config.ConsumerKey));
            requestParams.Add(new KeyValuePair<string, string>("oauth_nonce", nonce));
            requestParams.Add(new KeyValuePair<string, string>("oauth_signature_method", SIGNATURE_METHOD));
            requestParams.Add(new KeyValuePair<string, string>("oauth_timestamp", timeStamp));
            requestParams.Add(new KeyValuePair<string, string>("oauth_version", OAUTH_VERSION));

            string response = _requestImplementation.BuildAndExecuteRequest(requestTokenUrl, config.ConsumerSecret + "&", requestParams);
            Dictionary<string, string> args = _helpers.SplitResponseParams(response);

            if (args.ContainsKey("oauth_token"))
            {
                // the &permission is an artifact of twitter's auth
                // it isn't break other auths so i'm going to leave it here for now
                result.AuthUrl = userAuthUrl + "?oauth_token=" + args["oauth_token"];
                result.OAuthTokenSecret = args["oauth_token_secret"];
            }

            return result;
        }
Ejemplo n.º 5
0
 internal static void ValidateArguments(OAuthConsumerConfig config)
 {
     if(String.IsNullOrEmpty(config.ConsumerKey)) throw new ArgumentNullException();
     if(String.IsNullOrEmpty(config.ConsumerSecret)) throw new ArgumentNullException();
 }
Ejemplo n.º 6
0
        internal static dynamic JsonMethod(OAuthConsumerConfig config, string url, string authToken, string tokenSecret, JavaScriptConverter jsConverter, List<KeyValuePair<string, string>> requestParams = null)
        {
            dynamic result = null;

            if (requestParams == null) requestParams = new List<KeyValuePair<string, string>>();

            bool useAuthorized = false;

            if (!String.IsNullOrEmpty(authToken) && !String.IsNullOrEmpty(tokenSecret))
            {
                useAuthorized = true;
                string timeStamp = _helpers.BuildTimestamp();
                string nonce = _helpers.BuildNonce();

                requestParams.Add(new KeyValuePair<string, string>("oauth_consumer_key", config.ConsumerKey));
                requestParams.Add(new KeyValuePair<string, string>("oauth_nonce", nonce));
                requestParams.Add(new KeyValuePair<string, string>("oauth_signature_method", SIGNATURE_METHOD));
                requestParams.Add(new KeyValuePair<string, string>("oauth_timestamp", timeStamp));
                requestParams.Add(new KeyValuePair<string, string>("oauth_token", authToken));
                requestParams.Add(new KeyValuePair<string, string>("oauth_version", OAUTH_VERSION));
            }
            string response = _requestImplementation.BuildAndExecuteRequest(url, config.ConsumerSecret + "&" + tokenSecret, requestParams, useAuthorized);

            if (!String.IsNullOrEmpty(response))
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                jss.RegisterConverters(new JavaScriptConverter[] { jsConverter });

                try
                {
                    result = jss.Deserialize(response, typeof(object)) as dynamic;
                }
                catch (Exception exc)
                {
                    throw new InvalidJsonInputException();
                }
            }

            return result;
        }
Ejemplo n.º 7
0
 public static dynamic JsonMethod(OAuthConsumerConfig config, string url, string authToken, string tokenSecret, List<KeyValuePair<string, string>> requestParams = null)
 {
     return JsonMethod(config, url, authToken, tokenSecret, new Json.DynamicJsonObject.DynamicJsonConverter(), requestParams);
 }
Ejemplo n.º 8
0
        public void JsonMethodInvalidJson()
        {
            MockRepository m = new MockRepository();

            IOAuthHelpers helpers = m.DynamicMock<IOAuthHelpers>();
            IOAuthRequestImplementation request = m.DynamicMock<IOAuthRequestImplementation>();

            OAuthClient.SetHelperImplementation(helpers);
            OAuthClient.SetRequestImplementation(request);

            OAuthConsumerConfig config = new OAuthConsumerConfig();
            config.ConsumerKey = CONSUMER_KEY;
            config.ConsumerSecret = CONSUMER_SECRET;

            string tokenSecret = "tokenSecret";
            string authToken = "authToken";

            string methodUrl = "methodUrl";

            using (m.Record())
            {
                helpers.Expect(h => h.BuildTimestamp()).Return(timeStamp);
                helpers.Expect(h => h.BuildNonce()).Return(nonce);

                request.Expect(r => r.BuildAndExecuteRequest(ACCESS_TOKEN_URL, CONSUMER_SECRET, null)).IgnoreArguments().Return("response");

            }
            using (m.Playback())
            {
                Assert.Throws(typeof(InvalidJsonInputException), delegate
                {
                    OAuthClient.JsonMethod(config, methodUrl, authToken, tokenSecret, new MockJavaScriptConverter());
                });
            }
        }
Ejemplo n.º 9
0
        public void JsonMethod()
        {
            MockRepository m = new MockRepository();

            IOAuthHelpers helpers = m.DynamicMock<IOAuthHelpers>();
            IOAuthRequestImplementation request = m.DynamicMock<IOAuthRequestImplementation>();

            OAuthClient.SetHelperImplementation(helpers);
            OAuthClient.SetRequestImplementation(request);

            OAuthConsumerConfig config = new OAuthConsumerConfig();
            config.ConsumerKey = CONSUMER_KEY;
            config.ConsumerSecret = CONSUMER_SECRET;

            string tokenSecret = "tokenSecret";
            string authToken = "authToken";

            string methodUrl = "methodUrl";
            string response = "{key: 'value'}";

            using (m.Record())
            {
                helpers.Expect(h => h.BuildTimestamp()).Return(timeStamp);
                helpers.Expect(h => h.BuildNonce()).Return(nonce);

                request.Expect(r => r.BuildAndExecuteRequest(ACCESS_TOKEN_URL, CONSUMER_SECRET, null)).IgnoreArguments().Return(response);

            }
            using (m.Playback())
            {
                dynamic result = OAuthClient.JsonMethod(config, methodUrl, authToken, tokenSecret, new MockJavaScriptConverter());

                Assert.AreEqual("value", result["key"]);
            }
        }
Ejemplo n.º 10
0
        public void GenerateUnauthorizedRequestWithoutArguments()
        {
            MockRepository mr = new MockRepository();

            OAuthConsumerConfig config = new OAuthConsumerConfig();

            using (mr.Record())
            {
                Assert.Throws(typeof(ArgumentNullException), delegate
                {
                    OAuthClient.GenerateUnauthorizedRequestToken(config, REQUEST_TOKEN_URL, USER_AUTH_URL);
                });
            }
        }
Ejemplo n.º 11
0
        public void GenerateUnauthorizedRequestToken()
        {
            MockRepository m = new MockRepository();

            IOAuthHelpers helpers = m.DynamicMock<IOAuthHelpers>();
            IOAuthRequestImplementation request = m.DynamicMock<IOAuthRequestImplementation>();

            OAuthClient.SetHelperImplementation(helpers);
            OAuthClient.SetRequestImplementation(request);

            OAuthConsumerConfig config = new OAuthConsumerConfig();
            config.ConsumerKey = CONSUMER_KEY;
            config.ConsumerSecret = CONSUMER_SECRET;

            string tokenSecret = "tokenSecret";
            string responseToken = "responseToken";
            string responseTokenSecret = "responseTokenSecret";

            Dictionary<string, string> responseParameters = new Dictionary<string, string>();
            responseParameters.Add("oauth_token", responseToken);
            responseParameters.Add("oauth_token_secret", responseTokenSecret);

            using (m.Record())
            {
                helpers.Expect(h => h.BuildTimestamp()).Return(timeStamp);
                helpers.Expect(h => h.BuildNonce()).Return(nonce);

                request.Expect(r => r.BuildAndExecuteRequest(ACCESS_TOKEN_URL, CONSUMER_SECRET, null)).IgnoreArguments().Return("response");

                helpers.Expect(h => h.SplitResponseParams(null)).IgnoreArguments().Return(responseParameters);
            }
            using (m.Playback())
            {
                AuthRequestResult result = OAuthClient.GenerateUnauthorizedRequestToken(config, REQUEST_TOKEN_URL, USER_AUTH_URL);

                //TODO: need to add test coverage around the permission scope stuff added
                //Assert.AreEqual(USER_AUTH_URL + "?oauth_token=" + responseToken + "&permission=read", result.AuthUrl);
                Assert.AreEqual(USER_AUTH_URL + "?oauth_token=" + responseToken, result.AuthUrl);
                Assert.AreEqual(responseTokenSecret, result.OAuthTokenSecret);
            }
        }
Ejemplo n.º 12
0
        public void ExchangeForAccessToken()
        {
            MockRepository m = new MockRepository();

            IOAuthHelpers _helpers = m.DynamicMock<IOAuthHelpers>();
            IOAuthRequestImplementation _request = m.DynamicMock<IOAuthRequestImplementation>();

            OAuthClient.SetHelperImplementation(_helpers);
            OAuthClient.SetRequestImplementation(_request);

            OAuthConsumerConfig config = new OAuthConsumerConfig();
            config.ConsumerSecret = CONSUMER_SECRET;
            config.ConsumerKey = CONSUMER_KEY;

            string tokenSecret = "tokenSecret";
            string responseToken = "responseToken";
            string responseTokenSecret = "responseTokenSecret";

            Dictionary<string, string> responseParameters = new Dictionary<string, string>();
            responseParameters.Add("oauth_token", responseToken);
            responseParameters.Add("oauth_token_secret", responseTokenSecret);

            using (m.Record())
            {
                _helpers.Expect(h => h.BuildTimestamp()).Return(timeStamp);
                _helpers.Expect(h => h.BuildNonce()).Return(nonce);

                _request.Expect(r => r.BuildAndExecuteRequest(ACCESS_TOKEN_URL, CONSUMER_SECRET + "&" + tokenSecret, null)).IgnoreArguments().Return("response");

                _helpers.Expect(h => h.SplitResponseParams(null)).IgnoreArguments().Return(responseParameters);
            }
            using (m.Playback())
            {
                AccessToken result = OAuthClient.ExchangeForAccessToken(config, ACCESS_TOKEN_URL, "authToken", tokenSecret, "verifier");

                Assert.AreEqual(responseToken, result.OAuthToken);
                Assert.AreEqual(responseTokenSecret, result.OAuthTokenSecret);
            }
        }
    protected void btnGetTweets_Click(object sender, EventArgs e)
    {
        OAuthConsumerConfig config = new OAuthConsumerConfig();
        config.ConsumerKey = txtConsumerKey.Text;
        config.ConsumerSecret = txtConsumerSecret.Text;

        dynamic tweets = OAuthClient.JsonMethod(config, PageProviderConfig.DemoMethodUrl, ltlAccessToken.Text, ltlAccessTokenSecret.Text, PageProviderConfig.DemoMethodArguments);

        grdTweets.DataSource = PageProviderConfig.TransposeResults(tweets);
        grdTweets.DataBind();
    }