public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string userAccessToken)
        {
            string id   = previousAuthResult.ProviderUserId;
            string name = previousAuthResult.UserName;

            return(new AuthenticationResult(true, ProviderName, id, name, previousAuthResult.ExtraData));
        }
        public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string token, string userAccessSecret, string returnUrl)
        {
            var    client          = Build(providerConfiguration) as LinkedInOAuth2Client;
            string userAccessToken = client.GetAccessToken(new Uri(returnUrl), token);

            return(GetUserData(providerConfiguration, previousAuthResult, userAccessToken));
        }
        public IAuthenticationClient Build(ProviderConfigurationViewModel providerConfiguration)
        {
            string ClientId     = providerConfiguration.ProviderIdKey;
            string ClientSecret = providerConfiguration.ProviderSecret;
            var    client       = new GoogleOAuth2Client(ClientId, ClientSecret);

            return(client);
        }
        public IAuthenticationClient Build(ProviderConfigurationViewModel providerConfiguration)
        {
            if (string.IsNullOrWhiteSpace(providerConfiguration.ProviderIdentifier))
            {
                throw new Exception("Client Identifier must be known if Provider is unknown.");
            }

            return(new OpenIdClient(_providerName, Identifier.Parse(providerConfiguration.ProviderIdentifier)));
        }
        public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string token, string userAccessSecret, string returnUrl)
        {
            var client = Build(providerConfiguration) as GoogleOAuth2Client;
            //Logger.Error("Inizio chiamata Google");
            string userAccessToken = client.GetAccessToken(new Uri(returnUrl), token);

            //Logger.Error("access token: {0}", userAccessToken);
            return(GetUserData(providerConfiguration, previousAuthResult, userAccessToken));
        }
Beispiel #6
0
        public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string userAccessToken)
        {
            var userData = (Build(providerConfiguration) as InstagramOAuth2Client).GetUserDataDictionary(userAccessToken);

            userData["accesstoken"] = userAccessToken;
            string id       = userData["id"];
            string username = userData["username"];

            return(new AuthenticationResult(true, this.ProviderName, id, username, userData));
        }
Beispiel #7
0
        public IAuthenticationClient BuildMobile(ProviderConfigurationViewModel providerConfiguration)
        {
            var    attrbutesDictionary = new Dictionary <string, string>();
            var    providerAttributes  = providerConfiguration.Attributes.ToDictionary(k => k.AttributeKey, v => v.AttributeValue);
            string clientId            = providerAttributes["BundleID"];
            string clientSecret        = GetClientSecret(providerAttributes, true);
            var    client = new AppleOAuth2Client(clientId, clientSecret, Logger);

            return(client);
        }
        public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string userAccessToken)
        {
            string secret = "";

            if (previousAuthResult.ExtraData.ContainsKey("secret"))
            {
                secret = previousAuthResult.ExtraData["secret"];
            }
            return(GetUserData(providerConfiguration, previousAuthResult, userAccessToken, secret, ""));
        }
Beispiel #9
0
 public static void ToViewModel(this ProviderConfigurationRecord record, ProviderConfigurationViewModel model)
 {
     model.Id                 = record.Id;
     model.DisplayName        = record.DisplayName;
     model.ProviderIdentifier = record.ProviderIdentifier;
     model.ProviderIdKey      = record.ProviderIdKey;
     model.ProviderName       = record.ProviderName;
     model.ProviderSecret     = record.ProviderSecret;
     model.UserIdentifier     = record.UserIdentifier;
     model.IsEnabled          = record.IsEnabled;
 }
        public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previosAuthResult, string userAccessToken)
        {
            var userData = (Build(providerConfiguration) as GoogleOAuth2Client).GetUserDataDictionary(userAccessToken);

            //Logger.Error("user data count: {0}", userData.Count);
            userData["accesstoken"] = userAccessToken;
            string id   = userData["id"];
            string name = userData["email"];

            userData["name"] = userData["email"];
            return(new AuthenticationResult(true, this.ProviderName, id, name, userData));
        }
Beispiel #11
0
 public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string token, string userAccessSecretKey, string returnUrl)
 {
     if (previousAuthResult != null && previousAuthResult.IsSuccessful && !string.IsNullOrWhiteSpace(previousAuthResult.Provider))
     {
         return(previousAuthResult);
     }
     else
     {
         var    client          = BuildMobile(providerConfiguration) as AppleOAuth2Client;
         string userAccessToken = client.GetAccessToken(new Uri(returnUrl), token);
         return(GetUserData(providerConfiguration, previousAuthResult, userAccessToken));
     }
 }
Beispiel #12
0
 public static void ToRecord(this ProviderConfigurationViewModel model, ProviderConfigurationRecord record)
 {
     if (record == null)
     {
         return;
     }
     record.DisplayName        = model.DisplayName;
     record.ProviderIdentifier = model.ProviderIdentifier;
     record.ProviderIdKey      = model.ProviderIdKey;
     record.ProviderName       = model.ProviderName;
     record.ProviderSecret     = model.ProviderSecret;
     record.UserIdentifier     = model.UserIdentifier;
     record.IsEnabled          = model.IsEnabled;
 }
        public AuthenticationResult GetUserData(ProviderConfigurationViewModel clientConfiguration, AuthenticationResult previousAuthResult, string userAccessToken)
        {
            var serializer = new DataContractJsonSerializer(typeof(FacebookGraphData));
            FacebookGraphData graphData;
            var request =
                WebRequest.Create(
                    "https://graph.facebook.com/me?fields=id,email,birthday,first_name,last_name,name,locale,link,gender,timezone,updated_time,verified&access_token=" + userAccessToken);

            try {
                using (var response = request.GetResponse()) {
                    using (var responseStream = response.GetResponseStream()) {
                        graphData = (FacebookGraphData)serializer.ReadObject(responseStream);
                    }
                }
            }
            catch {
                return(AuthenticationResult.Failed);
            }
            // this dictionary must contains
            var userData = new Dictionary <string, string>();

            userData["id"]       = graphData.Id;
            userData["username"] = graphData.Email;
            userData["email"]    = graphData.Email;
            userData["name"]     = graphData.Name;
            userData["link"]     = graphData.Link == null ? null : graphData.Link.AbsoluteUri;
            userData["gender"]   = graphData.Gender;
            userData["birthday"] = graphData.Birthday;

            if (userData == null)
            {
                return(AuthenticationResult.Failed);
            }

            string id = userData["id"];
            string name;

            // Some oAuth providers do not return value for the 'username' attribute.
            // In that case, try the 'name' attribute. If it's still unavailable, fall back to 'id'
            if (!userData.TryGetValue("username", out name) && !userData.TryGetValue("name", out name))
            {
                name = id;
            }

            // add the access token to the user data dictionary just in case page developers want to use it
            userData["accesstoken"] = userAccessToken;

            return(new AuthenticationResult(
                       isSuccessful: true, provider: this.ProviderName, providerUserId: id, userName: name, extraData: userData));
        }
Beispiel #14
0
 public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string userAccessToken)
 {
     if (previousAuthResult != null && previousAuthResult.IsSuccessful && !string.IsNullOrWhiteSpace(previousAuthResult.Provider))
     {
         return(previousAuthResult);
     }
     else
     {
         var userData = (Build(providerConfiguration) as AppleOAuth2Client).GetUserDataDictionary(userAccessToken);
         userData["accesstoken"] = userAccessToken;
         var id    = userData["sub"];
         var email = userData["email"];
         return(new AuthenticationResult(true, this.ProviderName, id, email, userData));
     }
 }
Beispiel #15
0
 private IEnumerable <ProviderConfigurationViewModel> GetProviders()
 {
     try {
         return(_cacheManager.Get(
                    "Laser.Orchard.OpenAuthentication.Providers",
                    ctx => {
             ctx.Monitor(_signals.When("Laser.Orchard.OpenAuthentication.Providers.Changed"));
             var configuration = _repository.Table.ToList()
                                 .Select(x => {
                 var cfg = new ProviderConfigurationViewModel();
                 x.ToViewModel(cfg);
                 cfg.Attributes = GetProviderConfigurationAttributes(cfg.Id, cfg.ProviderName);
                 return cfg;
             });
             return configuration.ToList();
         }));
     }
     catch (Exception ex) {
         Logger.Error(ex, "An unexpected error occurred in GetProviders method");
         return(new List <ProviderConfigurationViewModel>());
     }
 }
        public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string userAccessToken)
        {
            Dictionary <string, string> userData = new Dictionary <string, string>();
            string uri        = "https://apis.live.net/v5.0/me?access_token=" + userAccessToken;
            var    webRequest = (HttpWebRequest)WebRequest.Create(uri);

            using (var webResponse = webRequest.GetResponse()) {
                using (var stream = webResponse.GetResponseStream()) {
                    if (stream == null)
                    {
                        return(null);
                    }

                    using (var textReader = new StreamReader(stream)) {
                        var json   = textReader.ReadToEnd();
                        var valori = JObject.Parse(json);
                        var data   = valori.Root; // .SelectToken("data");
                        userData.Add("id", data.Value <string>("id"));
                        userData.Add("name", data.Value <string>("name"));
                        userData.Add("first_name", data.Value <string>("first_name"));
                        userData.Add("last_name", data.Value <string>("last_name"));
                        userData.Add("gender", data.Value <string>("gender"));
                        userData.Add("locale", data.Value <string>("locale"));
                        userData.Add("updated_time", data.Value <string>("updated_time"));
                        var emails = valori.SelectToken("emails");
                        userData.Add("email", emails.Value <string>("preferred"));
                    }
                }
            }

            string id   = userData["id"];
            string name = userData["name"];

            // add the access token to the user data dictionary just in case page developers want to use it
            userData["accesstoken"] = userAccessToken;
            return(new AuthenticationResult(true, ProviderName, id, name, userData));
        }
 public IAuthenticationClient Build(ProviderConfigurationViewModel providerConfiguration)
 {
     return(new YahooOpenIdClient());
 }
 public IAuthenticationClient Build(ProviderConfigurationViewModel providerConfiguration)
 {
     return(new TwitterCustomClient(providerConfiguration.ProviderIdKey, providerConfiguration.ProviderSecret));
 }
        public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previousAuthResult, string token, string userAccessSecret, string returnUrl)
        {
            var userAccessToken = token;

            if (String.IsNullOrWhiteSpace(userAccessSecret))
            {
                if (previousAuthResult.ExtraData.ContainsKey("accesstoken") == false)
                {
                    previousAuthResult.ExtraData.Add("accesstoken", userAccessToken);
                }
                return(new AuthenticationResult(true, this.ProviderName, previousAuthResult.ProviderUserId, previousAuthResult.UserName, previousAuthResult.ExtraData));
            }
            var twitterUserSerializer = new DataContractJsonSerializer(typeof(TwitterUserData));

            TwitterUserData twitterUserData;
            // recupero lo User_Name e la mail relativi al token
            var accountSettingsRequest = PrepareAuthorizedRequestGet(userAccessToken,
                                                                     userAccessSecret,
                                                                     providerConfiguration.ProviderIdKey,
                                                                     providerConfiguration.ProviderSecret,
                                                                     "https://api.twitter.com/1.1/account/verify_credentials.json?skip_status=true&include_email=true");

            try {
                using (var response = accountSettingsRequest.GetResponse()) {
                    using (var responseStream = response.GetResponseStream()) {
                        twitterUserData = (TwitterUserData)twitterUserSerializer.ReadObject(responseStream);
                        if (String.IsNullOrWhiteSpace(twitterUserData.Screen_Name))
                        {
                            Logger.Error("Twitter: missing screen_name");
                            return(AuthenticationResult.Failed);
                        }
                    }
                }
            }
            catch (Exception ex) {
                Logger.Error(ex, "Twitter verify_credentials");
                return(AuthenticationResult.Failed);
            }

            var userData = new Dictionary <string, string>();

            userData["id"]       = userAccessToken.Split('-')[0];
            userData["username"] = twitterUserData.Screen_Name;
            userData["email"]    = twitterUserData.Email;

            string id = userData["id"];
            string name;

            // Some oAuth providers do not return value for the 'username' attribute.
            // In that case, try the 'name' attribute. If it's still unavailable, fall back to 'id'
            if (!userData.TryGetValue("username", out name) && !userData.TryGetValue("name", out name))
            {
                name = id;
            }

            // add the access token to the user data dictionary just in case page developers want to use it
            userData["accesstoken"] = userAccessToken;

            return(new AuthenticationResult(
                       isSuccessful: true, provider: this.ProviderName, providerUserId: id, userName: name, extraData: userData));
        }
 public AuthenticationResult GetUserData(ProviderConfigurationViewModel providerConfiguration, AuthenticationResult previosAuthResult, string token, string userAccessSecret, string returnUrl)
 {
     return(GetUserData(providerConfiguration, previosAuthResult, token));
 }
 public IAuthenticationClient Build(ProviderConfigurationViewModel providerConfiguration)
 {
     return(new MicrosoftClient(providerConfiguration.ProviderIdKey, providerConfiguration.ProviderSecret, "wl.basic", "wl.emails"));
 }
Beispiel #22
0
 private static IAuthenticationClient CreateOpenIdClient(ProviderConfigurationViewModel providerConfiguration)
 {
     return(new CustomOpenIdAuthenticationClient(providerConfiguration.ProviderName).Build(providerConfiguration));
 }
Beispiel #23
0
        private IEnumerable <ProviderConfigurationViewModel> GetProviders()
        {
            try {
                return(_cacheManager.Get(
                           "Laser.Orchard.OpenAuthentication.Providers",
                           true, //prevent concurrent calls to this
                           ctx => {
                    ctx.Monitor(_signals.When("Laser.Orchard.OpenAuthentication.Providers.Changed"));
                    var configuration = _repository.Table.ToList()
                                        .Select(x => {
                        var cfg = new ProviderConfigurationViewModel();
                        x.ToViewModel(cfg);
                        // https://stackoverflow.com/a/6064422/2669614
                        // Basically, by doing a "subquery" here, we were trying to execute a query while iterating
                        // over the results of another. This causes an exception:
                        // System.InvalidOperationException: A Command è già associato un DataReader aperto, che deve essere chiuso.
                        //cfg.Attributes = GetProviderConfigurationAttributes(cfg.Id, cfg.ProviderName);
                        return cfg;
                    });
                    //foreach (var cfg in configuration) {
                    //    // so we moved the "subquery" here.
                    //    // This could be further optimized, because as is we are doing a query for each
                    //    // provider, rather than a single one for all of them.
                    //    cfg.Attributes.AddRange(GetProviderConfigurationAttributes(cfg.Id, cfg.ProviderName));

                    //}
                    // A different solution would be to join everything in a single query, like below
                    // but nhibernate does not implement GroupJoin
                    //var myConfigQuery = _repository.Table
                    //    .GroupJoin(
                    //        _repositoryAttributes.Table,
                    //        pcr => pcr.Id,
                    //        par => par.ProviderId,
                    //        (pcr, pars) => new {
                    //            ProviderConfigurationRecord = pcr,
                    //            ProviderAttributeRecords = pars
                    //        })
                    //    .ToList();
                    //var myConfig = myConfigQuery
                    //    .Select(obj => {
                    //        var cfg = new ProviderConfigurationViewModel();
                    //        obj.ProviderConfigurationRecord.ToViewModel(cfg);
                    //        var client = _authenticationClients
                    //            .FirstOrDefault(x => x.ProviderName == obj.ProviderConfigurationRecord.ProviderName);
                    //        if (client != null && client.GetAttributeKeys().Any()) {
                    //            foreach (var item in client.GetAttributeKeys()) {
                    //                var origValue = obj.ProviderAttributeRecords
                    //                    .FirstOrDefault(x => x.AttributeKey == item.Key);
                    //                cfg.Attributes.Add(new ProviderAttributeViewModel() {
                    //                    AttributeKey = item.Key,
                    //                    AttributeValue = origValue != null ? origValue.AttributeValue : "",
                    //                    AttributeDescription = item.Value
                    //                });
                    //            }
                    //        }
                    //        return cfg;
                    //    });
                    return configuration
                    .Select(cfg => new ProviderConfigurationViewModel(cfg, GetProviderConfigurationAttributes(cfg.Id, cfg.ProviderName)))
                    .ToList();
                }));
            }
            catch (Exception ex) {
                Logger.Error(ex, "An unexpected error occurred in GetProviders method");
                return(new List <ProviderConfigurationViewModel>());
            }
        }
 public IAuthenticationClient Build(ProviderConfigurationViewModel providerConfiguration)
 {
     return(new FacebookOAuth2Client(providerConfiguration.ProviderIdKey, providerConfiguration.ProviderSecret));
 }