public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model)
        {
            //TODO handle exception from model.Exception
            Repository repo = new Repository();

            User user = repo.GetByMail(model.AuthenticatedClient.UserInformation.Email);

            if (user == null)
            {
                user = new User
                {
                    Id                 = Guid.NewGuid(),
                    CreateDate         = DateTime.UtcNow,
                    Email              = model.AuthenticatedClient.UserInformation.Email,
                    Name               = model.AuthenticatedClient.UserInformation.Name,
                    Picture            = model.AuthenticatedClient.UserInformation.Picture,
                    Provider           = model.AuthenticatedClient.ProviderName,
                    ProviderExternalId = model.AuthenticatedClient.UserInformation.Id
                };

                repo.Add(user);
            }


            return(nancyModule.LoginAndRedirect(user.Id));
        }
Ejemplo n.º 2
0
        public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model)
        {
            if (model == null || model.AuthenticatedClient == null || model.Exception != null)
            {
                // Fail. Hand back to the long page.
                Debug.WriteLine("Could not process OAuth callback");
                if (model != null && model.Exception != null)
                {
                    Debug.WriteLine(model.Exception.ToString());
                }

                return(new RedirectResponse("/login"));
            }

            // Get user with matching email address.
            var user = this.userQueries.GetByEmailAddress(model.AuthenticatedClient.UserInformation.Email);

            // If the user doesn't exist, we are registering them...
            if (user == null)
            {
                this.RegisterUser(model);
            }
            else
            {
                this.AddOrUpdateUserProvider(model, user);
            }

            user = this.userQueries.GetByEmailAddress(model.AuthenticatedClient.UserInformation.Email);

            return(nancyModule.LoginAndRedirect(user.Id, fallbackRedirectUrl: "/profile"));
        }
Ejemplo n.º 3
0
        public Response SignIn(UserNamePassword namePassword, NancyModule module)
        {
            var user = _repository.Find(namePassword.Name);
            if (user == null)
            {
                return HttpStatusCode.Unauthorized;
            }

            var hash = user.Hash(namePassword.Password);

            return user.Password.IsNotEqualTo(hash)
                ? HttpStatusCode.Unauthorized
                : module.LoginAndRedirect(user.Id);
        }
        public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model)
        {
            try
            {
                if (model.Exception != null)
                {
                    Logger.Fatal("3rd party provider returned exception: " + model.Exception);
                    return(ReturnLoginErrorResponse());
                }
                else
                {
                    //TODO handle exception from model.Exception
                    Repository repo = new Repository();

                    User user = repo.GetByMail(model.AuthenticatedClient.UserInformation.Email);

                    if (user == null)
                    {
                        user = new User
                        {
                            Id                 = Guid.NewGuid(),
                            CreateDate         = DateTime.UtcNow,
                            Email              = model.AuthenticatedClient.UserInformation.Email,
                            Name               = model.AuthenticatedClient.UserInformation.Name,
                            Picture            = model.AuthenticatedClient.UserInformation.Picture,
                            Provider           = model.AuthenticatedClient.ProviderName,
                            ProviderExternalId = model.AuthenticatedClient.UserInformation.Id
                        };

                        repo.Add(user);
                    }

                    return(nancyModule.LoginAndRedirect(user.Id));
                }
            }
            catch (Exception exc)
            {
                Logger.Fatal(exc);
                return(ReturnLoginErrorResponse());
            }
        }
Ejemplo n.º 5
0
        public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model)
        {
            var currentUserId = nancyModule.GetUserID();

            if (model.Exception != null)
            {
                _logger.Error("SOCIALAUTH", model.Exception);
                return(nancyModule.Response.AsRedirect("/"));
            }

            var userInfo     = model.AuthenticatedClient.UserInformation;
            var providerName = model.AuthenticatedClient.ProviderName;
            var userName     = userInfo.UserName ?? userInfo.Name ?? "Unknown";
            var email        = userInfo.Email ?? string.Empty;

            var user = _userService.GetByIdentity(providerName, userInfo.Id);

            if (user == null)
            {
                if (currentUserId == null)
                {
                    user = _userService.AddUser(userName, email, providerName, userInfo.Id);
                }
                else
                {
                    user = _userService.AttachIdentity(currentUserId, userName, email, providerName, userInfo.Id);
                }
            }
            else
            {
                if (currentUserId != null && user.ID != currentUserId)
                {
                    nancyModule.Session["MergeID"] = user.ID;

                    return(nancyModule.Response.AsRedirect("/#!/profile?tab=logins"));
                }
            }

            return(nancyModule.LoginAndRedirect(user.Identifier, DateTime.Now.AddMonths(3), model.ReturnUrl));
        }
Ejemplo n.º 6
0
        public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model)
        {
            if (model.Exception != null)
            {
                throw new NotImplementedException("Login failure", model.Exception);
            }

            CrossoutUser loggedInUser = null;

            if (nancyModule.Context.CurrentUser != null)
            {
                loggedInUser = userRepository.LoadUser(nancyModule.Context.CurrentUser.UserName);
            }

            if (loggedInUser == null)
            {
                UserInformation userInfo = model.AuthenticatedClient.UserInformation;

                var user = userRepository.LoadUser(userInfo.Id);

                if (user == CrossoutUser.EmptyUser)
                {
                    userRepository.SaveUser(new CrossoutUser
                    {
                        AuthEmail    = userInfo.Email,
                        AuthId       = userInfo.Id,
                        AuthName     = userInfo.Name,
                        AuthUsername = userInfo.UserName
                    });
                    user = userRepository.LoadUser(userInfo.Id);
                }
                return(nancyModule.LoginAndRedirect(user.Guid, null, "~/admin"));
            }

            return(nancyModule.AsRedirectQueryStringOrDefault("~/admin"));
        }