Exemple #1
0
        protected T OAuth3rdClient <T>(
            IOAuthTokenSecretPost dataPost
            ) where T : OAuthClient
        {
            OAuthCryptoSet oAuthConsumer, oAuthToken =
                new OAuthCryptoSet(dataPost.Token, dataPost.TokenSecret);

            if (typeof(T) == typeof(TwitterClient))
            {
                oAuthConsumer = new OAuthCryptoSet(
                    Settings.Default.TwitterClientConsumerKey,
                    Settings.Default.TwitterClientConsumerSecret
                    );
            }
            else
            {
                throw new InvalidOperationException("Third Party OAuth Provider not supported");
            }

            return(Activator.CreateInstance(
                       typeof(T),
                       new object[] {
                oAuthConsumer,
                oAuthToken,
                null
            }
                       ) as T);
        }
Exemple #2
0
 /// <summary>
 ///     Crea una nueva instancia del Cliente del Twitter API.
 /// </summary>
 /// <param name="consumer">
 ///     Conjunto de API-Key y API-Secret.
 /// </param>
 /// <param name="token">
 ///     Conjunto de Token y Token Secret.
 /// </param>
 public TwitterClient(OAuthCryptoSet consumer, OAuthCryptoSet token = null, Uri callbackUri = null)
     : base(new OAuthClientUris {
     BaseUri = new Uri("https://api.twitter.com/"),
     AuthorizationResource   = "oauth/authorize",
     ExchangeTokenResource   = "oauth/access_token",
     RequestTokenResource    = "oauth/request_token",
     CallbackRequestTokenUri = callbackUri
 },
            consumer,
            token)
 {
 }
Exemple #3
0
        protected T OAuth3rdClient <T>(
            IOAuthTokenPost dataPost
            ) where T : FacebookClient
        {
            OAuthCryptoSet oAuthConsumer =
                new OAuthCryptoSet(
                    Settings.Default.FacebookClientConsumerKey,
                    Settings.Default.FacebookClientConsumerSecret
                    );

            return(Activator.CreateInstance(
                       typeof(T),
                       new object[] {
                oAuthConsumer,
                null,
                null
            }
                       ) as T);
        }
Exemple #4
0
        public FacebookClient(OAuthCryptoSet consumer, OAuthCryptoSet token = null, Uri callbackRequestTokenUri = null)
        {
            ClientUris = new OAuthClientUris {
                BaseUri = new Uri("https://graph.facebook.com/"),
                AuthorizationResource   = "https://www.facebook.com/dialog/oauth",
                RequestTokenResource    = "https://www.facebook.com/dialog/oauth",
                ExchangeTokenResource   = "oauth/access_token",
                CallbackRequestTokenUri =
                    callbackRequestTokenUri ?? new Uri("https://www.facebook.com/connect/login_success.html"),
            };

            ConsumerCredentials = consumer;
            Token = token;

            if (Token != null)
            {
                CurrentlyHasAccessToken = true;
            }
        }
Exemple #5
0
        /// <summary>
        ///     Registra una nueva cuenta en la Nube de KMS.
        /// </summary>
        /// <param name="accountData">
        ///     Información de la cuenta a registrar.
        /// </param>
        public OAuthCryptoSet RegisterAccount(Dictionary <string, string> accountData)
        {
            Token = null;
            GetRequestToken();

            var parameters = new NameValueCollection();

            parameters.AddFromDictionary(accountData);

            var response = RequestSimpleNameValue(
                HttpRequestMethod.POST,
                ((KMSCloudUris)ClientUris).KmsRegisterAccountResource,
                parameters,
                null,
                new Dictionary <HttpRequestHeader, string> {
                {
                    HttpRequestHeader.AcceptLanguage, CultureInfo.CurrentCulture.Name
                }
            });

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new OAuthUnexpectedResponse <NameValueCollection>(response);
            }

            var tokenSet = new OAuthCryptoSet(
                response.Response.Get("oauth_token"),
                response.Response.Get("oauth_token_secret"));

            if (string.IsNullOrEmpty(tokenSet.Key) || string.IsNullOrEmpty(tokenSet.Secret))
            {
                throw new OAuthUnexpectedResponse <NameValueCollection>(
                          response,
                          "Server responded OK but no OAuth Credentials received.");
            }

            Token = tokenSet;
            return(tokenSet);
        }
Exemple #6
0
        public OAuthCryptoSet ExchangeCodeForToken(string code)
        {
            if (string.IsNullOrEmpty(ConsumerCredentials.Secret))
            {
                throw new InvalidOperationException(
                          "Current Facebook Client configuration does not include Client Secret.");
            }

            if (Token != null)
            {
                throw new InvalidOperationException(
                          "Current Facebook Client configuration already contains a Token exchanged obtained via Code.");
            }

            var response = RequestSimpleNameValue(
                HttpRequestMethod.GET,
                string.Format(
                    "oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}",
                    ConsumerCredentials.Key,
                    Uri.EscapeDataString(ClientUris.CallbackRequestTokenUri.AbsoluteUri),
                    ConsumerCredentials.Secret,
                    code));

            var token = response.Response.Get("access_token");

            if (string.IsNullOrEmpty(token))
            {
                throw new OAuthUnexpectedResponse();
            }

            Token = new OAuthCryptoSet(response.Response.Get("access_token"));
            Code  = code;
            CurrentlyHasAccessToken = true;

            return(Token);
        }
Exemple #7
0
        /// <summary>
        ///     Realizar un Inicio de Sesión en la Nube de KMS con un Servicio OAuth de Terceros.
        /// </summary>
        /// <param name="oAuthClient">
        ///     Objeto de Cliente IOAuthSocial.
        /// </param>
        /// <remarks>
        ///     Facebook, y todos los futuros servicios que implementan OAuth 2.0 no utilizan el Token
        ///     Secret del protocolo OAuth 1.0a, por lo que DEBE omitirase en esos casos.
        /// </remarks>
        /// <returns>
        ///     Devuelve el conjunto de Token y Token Secret generados por la Nube de KMS. El conjunto
        ///     también se establece automáticamente de forma interna para peticiones subsecuentes.
        /// </returns>
        public OAuthCryptoSet Login3rdParty <T>(T oAuthClient) where T : IOAuthSocialClient
        {
            if (ConsumerCredentials == null)
            {
                throw new OAuthUnexpectedRequest("No Consumer Credentials set before calling.");
            }

            Token = null;
            NameValueCollection requestParameters;

            if (oAuthClient is FacebookClient)
            {
                requestParameters = new NameValueCollection {
                    {
                        "ID", oAuthClient.UserID
                    }, {
                        "Code", (oAuthClient as FacebookClient).Code
                    }
                };
            }
            else
            {
                requestParameters = new NameValueCollection {
                    {
                        "Token", oAuthClient.Token.Key
                    }, {
                        "TokenSecret", oAuthClient.Token.Secret
                    }, {
                        "ID", oAuthClient.UserID
                    }
                };
            }

            string requestUrl = string.Format(
                ((KMSCloudUris)ClientUris).KmsOAuth3rdLogin,
                oAuthClient.ProviderName.ToLower(CultureInfo.InvariantCulture));

            GetRequestToken();

            var response = RequestSimpleNameValue(
                HttpRequestMethod.POST,
                requestUrl,
                requestParameters);

            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created &&
                response.StatusCode != HttpStatusCode.NotFound)
            {
                throw new OAuthUnexpectedResponse <NameValueCollection>(response);
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new KMSWrongUserCredentials("User not found");
            }

            var kmsOAuthToken       = response.Response.Get("oauth_token");
            var kmsOAuthTokenSecret = response.Response.Get("oauth_token_secret");

            if (string.IsNullOrEmpty(kmsOAuthToken) || string.IsNullOrEmpty(kmsOAuthTokenSecret))
            {
                throw new OAuthUnexpectedResponse <NameValueCollection>(
                          response,
                          "Server responded OK but no OAuth Credentials received.");
            }

            Token = new OAuthCryptoSet(kmsOAuthToken, kmsOAuthTokenSecret);
            return(Token);
        }
Exemple #8
0
 public KMSCloudClient(KMSCloudUris clientUris, OAuthCryptoSet consumer, OAuthCryptoSet token = null)
     : base(clientUris, consumer, token)
 {
 }