Beispiel #1
0
        public TUser CreateUserAndActivationRequest(TCreateUserForm form, Uri activateUserUrl)
        {
            var login = form.Login.Trim();

            if (!EmailValidator.Validate(login))
            {
                throw new IncorrectEmailException();
            }

            var password = form.Password;

            if (!passwordValidator.Validate(password))
            {
                throw new WeakPasswordException();
            }

            var existingUser = userStorage.FindUserByLogin(login);

            if (existingUser != null)
            {
                throw new LoginAlreadyExistsException(login);
            }

            var userId = Guid.NewGuid();
            var user   = userProcessor.MakeUser(form, userId);

            SetUserPassword(user, password);
            userStorage.CreateUser(user);
            SendUserActivationRequest(user, activateUserUrl);
            return(userStorage.FindUser(userId));
        }
        public async Task <IActionResult> CreateAccount([FromBody] string login, CancellationToken token)
        {
            if (await userStorage.FindUserByLogin(login, token) != null)
            {
                return(Forbid());
            }

            await userStorage.AddUser(new User(login), token);

            return(NoContent());
        }
        public async Task <IActionResult> CreateAccount([FromBody] UserDto user, CancellationToken token)
        {
            if (await userStorage.FindUserByLogin(user.Login, token) != null)
            {
                return(Forbid());
            }

            await userStorage.AddUser(new User(user.Login, user.Password), token);

            return(NoContent());
        }
        public void SendChangePasswordRequest(string login, Uri restorePasswordUrl, Uri activateUserUrl)
        {
            var user = userStorage.FindUserByLogin(login);

            if (user == null)
            {
                throw new LoginNotFoundException(login);
            }

            //if (!user.IsActive)
            //{
            //	throw new UserIsNotActiveException(user.UserId);
            //}

            var requestId          = Guid.NewGuid();
            var expirationDateTime = DateTimeOffset.UtcNow.Add(changePasswordRequestValidityPeriod);
            var request            = new TChangePasswordRequest
            {
                RequestId          = requestId,
                UserId             = user.UserId,
                ExpirationDateTime = expirationDateTime
            };

            changePasswordRequestDataStorage.CreateChangePasswordRequest(request);

            var builder = new UriBuilder(restorePasswordUrl);

            builder.Query = builder.Query.Length > 1
                                ? builder.Query.Substring(1) + "&" + "requestId=" + requestId
                                : "requestId=" + requestId;

            authEmailSender.SendPasswordResetEmail(user.Login, builder.Uri.ToString());
        }
        public void SendUserInvitation(string email, Guid organizationId, Guid invitingUserId, string organizationTitle,
                                       Uri acceptInvitationUrl)
        {
            EmailValidator.Validate(email);

            var existingUser = userStorage.FindUserByLogin(email);

            var userInvitationId = Guid.NewGuid();
            var invitedUserId    = existingUser != null ? existingUser.UserId : (Guid?)null;
            var utcNow           = DateTimeOffset.UtcNow;

            var userInvitation = new TUserInvitation
            {
                UserInvitationId = userInvitationId,
                OrganizationId   = organizationId,
                InivitingUserId  = invitingUserId,
                InvitedUserId    = invitedUserId,
                Email            = email,
                DateTimeCreated  = utcNow,
                Status           = UserInvitationStatus.Created
            };

            userInvitationStorage.CreateUserInvitation(userInvitation);

            var builder = new UriBuilder(acceptInvitationUrl);

            if (builder.Query.Length > 1)
            {
                builder.Query = builder.Query.Substring(1) + "&" + "invitationId=" + userInvitationId;
            }
            else
            {
                builder.Query = "invitationId=" + userInvitationId;
            }
            var invitationUrl = builder.Uri.ToString();

            authEmailSender.SendUserInvitation(email, invitationUrl, organizationTitle);
        }
Beispiel #6
0
        public AuthenticationResult <TUser> Authenticate(string login, string password)
        {
            if (string.IsNullOrWhiteSpace(login))
            {
                throw new WrongLoginPasswordException();
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new WrongLoginPasswordException();
            }

            login = login.Trim();

            var user = userStorage.FindUserByLogin(login);

            if (user == null)
            {
                throw new WrongLoginPasswordException();
            }

            if (!user.IsActive)
            {
                throw new InactiveUserException();
            }

            if (string.IsNullOrEmpty(user.PasswordHash) || string.IsNullOrEmpty(user.PasswordSalt))
            {
                throw new UserPasswordNotSetException(user.UserId);
            }

            var passwordHash = passwordHasher.HashPassword(password, user.PasswordSalt);

            if (user.PasswordHash != passwordHash)
            {
                throw new WrongLoginPasswordException();
            }

            var authenticationToken         = new AuthenticationToken(user.UserId);
            var encryptedBase64EncodedToken = authenticationTokenCryptography.EncryptTokenToBase64(authenticationToken);

            return(new AuthenticationResult <TUser>(encryptedBase64EncodedToken, user));
        }