예제 #1
0
        public IHttpActionResult Google(JObject value)
        {
            var authInfo = value.ToObject <ExternalAuthInfo>();

            if (authInfo == null || String.IsNullOrEmpty(authInfo.Code))
            {
                return(NotFound());
            }

            if (String.IsNullOrEmpty(Settings.Current.GoogleAppId) || String.IsNullOrEmpty(Settings.Current.GoogleAppSecret))
            {
                return(NotFound());
            }

            var client = new GoogleClient(new RequestFactory(), new RuntimeClientConfiguration {
                ClientId     = Settings.Current.GoogleAppId,
                ClientSecret = Settings.Current.GoogleAppSecret,
                RedirectUri  = authInfo.RedirectUri
            });

            UserInfo userInfo;

            try {
                userInfo = client.GetUserInfo(authInfo.Code);
            } catch (Exception ex) {
                Log.Error().Exception(ex).Write();
                //ex.ToExceptionless().MarkAsCritical().AddTags("External Login", "Google").AddObject(authInfo).Submit();
                return(BadRequest("Unable to get user info."));
            }

            User user;

            try {
                user = AddExternalLogin(userInfo);
            } catch (ApplicationException) {
                return(BadRequest("Account Creation is currently disabled."));
            } catch (Exception ex) {
                Log.Error().Exception(ex).Write();
                //ex.ToExceptionless().MarkAsCritical().AddTags("External Login", "Google").AddObject(authInfo).AddObject(userInfo).Submit();
                return(BadRequest("An error occurred while processing user info."));
            }

            if (user == null)
            {
                //_exceptionless.CreateLog(typeof(AuthController).Name, "Unable to process user info.", "Error").AddTags("External Login", "Google").AddObject(authInfo).AddObject(userInfo).Submit();
                return(BadRequest("Unable to process user info."));
            }

            if (!String.IsNullOrWhiteSpace(authInfo.InviteToken))
            {
                AddInvitedUserToOrganization(authInfo.InviteToken, user);
            }

            return(Ok(new TokenResult {
                Token = GetToken(user)
            }));
        }
예제 #2
0
        public static bool AutenticateGoogleUser(UserLoginInfo userInfo)
        {
            var googleClient = new GoogleClient(userInfo.Email, userInfo.Password);
            var result       = Task.Run(async() => await googleClient.GetUserInfo());

            //if the user is not correct return false.
            //call another method to check if the user exist in the database, and/or update.

            return(true);
        }
예제 #3
0
        public IHttpActionResult Google(JObject value)
        {
            var authInfo = value.ToObject <ExternalAuthInfo>();

            if (authInfo == null || String.IsNullOrEmpty(authInfo.Code))
            {
                return(NotFound());
            }

            if (String.IsNullOrEmpty(Settings.Current.GoogleAppId) || String.IsNullOrEmpty(Settings.Current.GoogleAppSecret))
            {
                return(NotFound());
            }

            var client = new GoogleClient(new RequestFactory(), new RuntimeClientConfiguration {
                ClientId     = Settings.Current.GoogleAppId,
                ClientSecret = Settings.Current.GoogleAppSecret,
                RedirectUri  = authInfo.RedirectUri
            });

            UserInfo userInfo;

            try {
                userInfo = client.GetUserInfo(authInfo.Code);
            } catch (Exception ex) {
                return(BadRequest("Unable to get user info."));
            }

            User user;

            try {
                user = AddExternalLogin(userInfo);
            } catch (Exception ex) {
                return(BadRequest("An error occurred while processing user info."));
            }

            if (user == null)
            {
                return(BadRequest("Unable to process user info."));
            }

            if (!String.IsNullOrEmpty(authInfo.InviteToken))
            {
                AddInvitedUserToOrganization(authInfo.InviteToken, user);
            }

            return(Ok(new { Token = GetToken(user) }));
        }
예제 #4
0
        /// <summary>
        /// Renders information received from authentication service.
        /// </summary>
        public ActionResult GoogleAuth(string code, string error) //TODO refactor this
        {
            UserInfo userInfo;

            try
            {
                userInfo = googleClient.GetUserInfo(googleClient.GetAccessToken(code, error));
            }
            catch
            {
                return(RedirectToAction("LogOn"));
            }

            var user = repository.Query <User>(x => x.RelatedPerson, x => x.Teams)
                       .SingleOrDefault(x => x.GoogleId == userInfo.Id);

            if (null == user)
            {
                user = new User
                {
                    GoogleId      = userInfo.Id,
                    RelatedPerson = repository.Query <Person>().SingleOrDefault(x => x.Email == userInfo.Email)
                };
                if (null == user.RelatedPerson)
                {
                    var person = new Person
                    {
                        Email        = userInfo.Email,
                        FirstName    = userInfo.FirstName,
                        LastName     = userInfo.LastName,
                        PhotoUri     = userInfo.PhotoUri,
                        Role         = PersonRole.Employee,
                        CreationDate = DateTime.Now
                    };
                    user.RelatedPerson = person;
                    repository.Save(person);
                }
                repository.Save(user);
            }

            appContext.User = user;

            return(RedirectToRoute("Default"));
        }