Exemple #1
0
            public void UpdatesThePasswordCredential()
            {
                // Arrange
                var service = Get <AuthenticationService>();
                var user    = Fakes.CreateUser(
                    "tempUser",
                    CredentialBuilder.CreatePbkdf2Password("oldpwd"));

                service.Entities
                .Set <User>()
                .Add(user);

                // Act
                var changed = service.ChangePassword(user.Username, "oldpwd", "newpwd");

                // Assert
                var cred = user.Credentials.Single();

                Assert.Equal(CredentialTypes.Password.Pbkdf2, cred.Type);
                Assert.True(VerifyPasswordHash(cred.Value, Constants.PBKDF2HashAlgorithmId, "newpwd"));
                service.Entities.VerifyCommitChanges();
            }
Exemple #2
0
        public async Task <ActionResult> CreateUser(NewUserModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("New", model));
            }

            var foundUser = UserService.GetUser(model.Username, model.EmailAddress);

            if (foundUser != null)
            {
                ModelState.AddModelError("Username", "A user with this username or email address is already registered.");
                return(View("New", model));
            }

            if (model.Password != model.PasswordConfirmation)
            {
                ModelState.AddModelError("Password", "Passwords did not match.");
                return(View("New", model));
            }

            AuthenticatedUser user;

            try
            {
                user = await AuthService.Register(model.Username, model.EmailAddress, model.FullName, CredentialBuilder.CreatePbkdf2Password(model.Password));
            }
            catch (EntityException ex)
            {
                ModelState.AddModelError("Register", ex.Message);
                return(View("New", model));
            }

            return(SafeRedirect(Url.AdminUsers()));
        }
Exemple #3
0
        public Fakes()
        {
            User = new User("testUser")
            {
                Key          = 42,
                EmailAddress = "*****@*****.**",
                Credentials  = new List <Credential>
                {
                    CredentialBuilder.CreatePbkdf2Password(Password),
                    CredentialBuilder.CreateV1ApiKey(Guid.Parse("519e180e-335c-491a-ac26-e83c4bd31d65"),
                                                     ExpirationForApiKeyV1)
                }
            };

            ShaUser = new User("testShaUser")
            {
                Key          = 42,
                EmailAddress = "*****@*****.**",
                Credentials  = new List <Credential>
                {
                    CredentialBuilder.CreateSha1Password(Password),
                    CredentialBuilder.CreateV1ApiKey(Guid.Parse("b9704a41-4107-4cd2-bcfa-70d84e021ab2"),
                                                     ExpirationForApiKeyV1)
                }
            };

            Admin = new User("testAdmin")
            {
                Key          = 43,
                EmailAddress = "*****@*****.**",
                Credentials  = new List <Credential> {
                    CredentialBuilder.CreatePbkdf2Password(Password)
                },
                Roles = new List <Role> {
                    new Role {
                        Name = Constants.AdminRoleName
                    }
                }
            };

            Owner = new User("testPackageOwner")
            {
                Key         = 44,
                Credentials = new List <Credential> {
                    CredentialBuilder.CreatePbkdf2Password(Password)
                },
                EmailAddress = "*****@*****.**" //package owners need confirmed email addresses, obviously.
            };

            Package = new PackageRegistration
            {
                Id     = "FakePackage",
                Owners = new List <User> {
                    Owner
                },
                Packages = new List <Package>
                {
                    new Package {
                        Version = "1.0"
                    },
                    new Package {
                        Version = "2.0"
                    }
                }
            };
        }
        public async virtual Task <ActionResult> RegisterUser(LogOnViewModel model, string returnUrl, bool linkingAccount)
        {
            // I think it should be obvious why we don't want the current URL to be the return URL here ;)
            ViewData[Constants.ReturnUrlViewDataKey] = returnUrl;

            if (Request.IsAuthenticated)
            {
                TempData["Message"] = Strings.AlreadyLoggedIn;
                return(SafeRedirect(returnUrl));
            }

            if (linkingAccount)
            {
                ModelState.Remove("Register.Password");
            }

            if (!ModelState.IsValid)
            {
                return(LogOnView(model));
            }

            AuthenticatedUser user;

            try
            {
                if (linkingAccount)
                {
                    var result = await AuthService.ReadExternalLoginCredential(OwinContext);

                    if (result.ExternalIdentity == null)
                    {
                        return(ExternalLinkExpired());
                    }

                    user = await AuthService.Register(
                        model.Register.Username,
                        model.Register.EmailAddress,
                        result.Credential);
                }
                else
                {
                    user = await AuthService.Register(
                        model.Register.Username,
                        model.Register.EmailAddress,
                        CredentialBuilder.CreatePbkdf2Password(model.Register.Password));
                }
            }
            catch (EntityException ex)
            {
                ModelState.AddModelError("Register", ex.Message);
                return(LogOnView(model));
            }

            // Send a new account email
            MessageService.SendNewAccountEmail(
                new MailAddress(user.User.UnconfirmedEmailAddress, user.User.Username),
                Url.ConfirmationUrl(
                    "Confirm",
                    "Users",
                    user.User.Username,
                    user.User.EmailConfirmationToken));

            // We're logging in!
            AuthService.CreateSession(OwinContext, user.User);

            return(RedirectFromRegister(returnUrl));
        }