/// <summary>
        /// Connects and retrieves access token from provider
        /// </summary>
        /// <param name="providerType"></param>
        /// <param name="redirectResponse"></param>
        /// <param name="overrideConnection"></param>
        /// <returns></returns>
        public bool Connect(PROVIDER_TYPE providerType, string redirectResponse, bool overrideConnection = false)
        {
            if (overrideConnection && IsConnectedWith(providerType)) return true;

            var provider = ProviderFactory.GetProvider(providerType) as Provider;
            Token token = new Token();
            token.Domain = "http://opensource.brickred.com/socialauthdemo/";
            accessGrant.Add(providerType, token);
            provider.ConnectionToken = token;
            provider.LoginCallback(Utility.GetQuerystringParameters(redirectResponse), (x,y) => { });
            return true;
        }
        public void LoadToken(Token token, string returnUrl)
        {
            //TODO: If expiresOn is specified and token has expired, refresh token!! Currently, exception thrown!
            if (token.ExpiresOn != null && token.ExpiresOn != DateTime.MinValue)
                if (token.ExpiresOn < DateTime.Now)
                {
                    _logger.Error("Token has expired");
                    throw new OAuthException("Token has expired.");
                }

            //If user is already connected with provider specified in token, ignore this request!
            if (SocialAuthUser.IsConnectedWith(token.Provider))
                return;

            //Load token
            token.UserReturnURL = returnUrl;
            SessionManager.AddConnectionToken(token);
            SetUserAsLoggedIn();
        }
 internal static void AddConnectionToken(Token token)
 {
     Token t = userSession.connectedTokens.Find(x => x.Provider == token.Provider);
     if (t != null)
         userSession.connectedTokens.Remove(t);
     userSession.connectedTokens.Add(token);
 }
        public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod, byte[] content = null, Dictionary<string, string> headers = null)
        {


            HttpWebRequest request;
            request = (HttpWebRequest)HttpWebRequest.Create(feedURL);
            request.ServicePoint.Expect100Continue = false;
            request.Method = transportMethod.ToString();

            //if headers are specified, set/append them
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    switch (header.Key)
                    {
                        case "ContentLength":
                            {
                                request.ContentLength = long.Parse(header.Value);
                                break;
                            }

                        case "ContentType":
                            {
                                request.ContentType = header.Value;
                                break;
                            }
                        default:
                            {
                                request.Headers[header.Key] = header.Value;
                                break;
                            }
                    }

                }

            }

            if (request.ContentLength == 0 & content.Length > 0)
                request.ContentLength = content.Length;
            request.GetRequestStream().Write(content, 0, content.Length);
            WebResponse wr = null;
            try
            {
                logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString());
                wr = (WebResponse)request.GetResponse();
                logger.Info("Successfully executed  " + feedURL + " using " + transportMethod.ToString());
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex);
                throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex);
            }
            return wr;

        }
        public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod)
        {
            UriBuilder ub;

            /******** retrieve standard Fields ************/
            ub = new UriBuilder(feedURL);

            ub.SetQueryparameter(AccessTokenQueryParameterKey, connectionToken.AccessToken);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ub.ToString());
            request.Method = transportMethod.ToString();

            //logger.LogContactsRequest(ub.ToString());
            WebResponse wr;
            try
            {
                logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString());
                wr = (WebResponse)request.GetResponse();
                logger.Info("Successfully executed  " + feedURL + " using " + transportMethod.ToString());
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex);
                throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, null), ex);
            }

            return wr;
        }
Пример #6
0
        public override System.Net.WebResponse ExecuteFeed(string feedURL, IProvider provider, BusinessObjects.Token connectionToken, BusinessObjects.TRANSPORT_METHOD transportMethod, byte[] content = null, Dictionary <string, string> headers = null)
        {
            string      signature   = "";
            OAuthHelper oauthHelper = new OAuthHelper();


            string          timestamp   = oauthHelper.GenerateTimeStamp();
            QueryParameters oauthParams = new QueryParameters();

            oauthParams.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParams.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParams.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParams.Add("oauth_timestamp", timestamp);
            oauthParams.Add("oauth_token", connectionToken.AccessToken);
            oauthParams.Add("oauth_version", "1.0");
            signature = oauthHelper.GenerateSignature(new Uri(feedURL), oauthParams, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, TRANSPORT_METHOD.POST, connectionToken.TokenSecret);
            oauthParams.Add("oauth_signature", signature);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(feedURL);

            request.Method = transportMethod.ToString();
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    switch (header.Key)
                    {
                    case "ContentLength":
                    {
                        request.ContentLength = long.Parse(header.Value);
                        break;
                    }

                    case "ContentType":
                    {
                        request.ContentType = header.Value;
                        break;
                    }

                    default:
                    {
                        request.Headers[header.Key] = header.Value;
                        break;
                    }
                    }
                }
            }

            request.ContentLength = (content == null) ? 0 : content.Length;
            request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParams));
            request.GetRequestStream().Write(content, 0, content.Length);
            WebResponse wr = null;

            try
            {
                logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString() + Environment.NewLine + "Request Parameters: " + oauthParams.ToString());
                wr = (WebResponse)request.GetResponse();
                logger.Info("Successfully executed  " + feedURL + " using " + transportMethod.ToString());
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex);
                throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex);
            }
            return(wr);
        }
Пример #7
0
        public override System.Net.WebResponse ExecuteFeed(string feedURL, IProvider provider, BusinessObjects.Token connectionToken, BusinessObjects.TRANSPORT_METHOD transportMethod)
        {
            string          signature   = "";
            OAuthHelper     oauthHelper = new OAuthHelper();
            QueryParameters oauthParams = new QueryParameters();

            oauthParams.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParams.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParams.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParams.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp());
            oauthParams.Add("oauth_token", connectionToken.AccessToken);
            oauthParams.Add("oauth_version", "1.0");


            ////1. Generate Signature
            signature = oauthHelper.GenerateSignature(new Uri(feedURL), oauthParams, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, TRANSPORT_METHOD.GET, connectionToken.TokenSecret);
            oauthParams.Add("oauth_signature", signature);


            //3.Connect and Execute Feed

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(feedURL);

            request.Method = transportMethod.ToString();
            request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParams));
            //request.ContentType = "application/atom+xml";
            request.ContentLength = 0;
            WebResponse wr;

            try
            {
                logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString() + Environment.NewLine + "Request Parameters: " + oauthParams.ToString());
                wr = (WebResponse)request.GetResponse();
                logger.Info("Successfully executed  " + feedURL + " using " + transportMethod.ToString());
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex);
                throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex);
            }
            return(wr);
        }
Пример #8
0
 public virtual void Connect(Token connectionToken)
 {
 }
        private List<Contact> Friends(string friendUserIDs, Token token)
        {
            string lookupUrl = "http://api.twitter.com/1/users/lookup.json?user_id=" + friendUserIDs;
            OAuthHelper helper = new OAuthHelper();
            string friendsData = "";
            try
            {
                Stream responseStream = AuthenticationStrategy.ExecuteFeed(lookupUrl, this, token, TRANSPORT_METHOD.GET).GetResponseStream();
                friendsData = new StreamReader(responseStream).ReadToEnd();
            }
            catch { throw; }

            List<Contact> friends = new List<Contact>();

            try
            {
                JArray j = JArray.Parse(friendsData);
                j.ToList().ForEach(f =>
                {
                    friends.Add(
                      new Contact()
                      {
                          Name = (string)f["name"],
                          ID = (string)f["id_str"],
                          ProfileURL = "http://twitter.com/#!/" + (string)f["screen_name"]
                      });

                });
            }
            catch
            {
                throw;
            }
            return friends.ToList<Contact>();

        }
Пример #10
0
 /// <summary>
 /// Called by Authentication Strategy at end of authentication process
 /// </summary>
 /// <param name="isSuccess">Is authentication successful</param>
 internal static void OnAuthneticationProcessCompleted(bool isSuccess, Token token)
 {
     SessionManager.AddConnectionToken(SessionManager.InProgressToken);
     //SetUserAsLoggedIn();
 }
 /// <summary>
 /// Allow user to add a connection token. Useful, when user is using a separate persistence for tokens.
 /// </summary>
 /// <param name="connectionToken"></param>
 /// <param name="overrideExisting"></param>
 public void LoadConnection(Token connectionToken, bool overrideExisting = false)
 {
     if (IsConnectedWith(connectionToken.Provider) || overrideExisting)
         accessGrant[connectionToken.Provider] = connectionToken;
     else
         throw new Exception("There is an already established connection with specified provider. If you wish to replace it, set the override argument to true");
 }
Пример #12
0
 public void ProcessAccessToken(QueryParameters responseCollection, Token connectionToken)
 {
     if (!string.IsNullOrEmpty(connectionToken.AccessToken))
     {
         connectionToken.Profile.DisplayName = connectionToken.ResponseCollection["screen_name"];
         connectionToken.Profile.ID = connectionToken.ResponseCollection["user_id"];
     }
 }
Пример #13
0
 public void OnAuthenticationCompleting(bool isSuccess, Token connectionToken)
 {
     isAuthenticated = isSuccess;
     connectionToken.Profile.DisplayName = connectionToken.ResponseCollection["screen_name"];
     connectionToken.Profile.ID = connectionToken.ResponseCollection["user_id"];
 }
Пример #14
0
        public override WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod)
        {
            string signature = "";
            OAuthHelper oauthHelper = new OAuthHelper();
            QueryParameters oauthParams = new QueryParameters();
            oauthParams.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParams.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParams.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParams.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp());
            oauthParams.Add("oauth_token", connectionToken.AccessToken);
            oauthParams.Add("oauth_version", "1.0");


            ////1. Generate Signature
            signature = oauthHelper.GenerateSignature(new Uri(feedURL), oauthParams, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, TRANSPORT_METHOD.GET, connectionToken.TokenSecret);
            oauthParams.Add("oauth_signature", signature);


            //3.Connect and Execute Feed

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(feedURL);
            request.Method = transportMethod.ToString();
            request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParams));
            //request.ContentType = "application/atom+xml";
            request.ContentLength = 0;
            WebResponse wr;
            try
            {
                logger.Debug("Executing " + feedURL + " using " + transportMethod.ToString());
                wr = (WebResponse)request.GetResponse();
                logger.Info("Successfully executed  " + feedURL + " using " + transportMethod.ToString());
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex);
                throw new OAuthException(ErrorMessages.CustomFeedExecutionError(feedURL, oauthParams), ex);
            }
            return wr;
        }
 public virtual WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod, byte[] content = null, Dictionary<string, string> headers = null)
 {
     throw new NotImplementedException();
 }
 public abstract WebResponse ExecuteFeed(string feedURL, IProvider provider, Token connectionToken, TRANSPORT_METHOD transportMethod);