/// <summary> /// Executes the specified command. /// </summary> /// <param name="command">The command.</param> /// <param name="scope">The scope.</param> public async Task <Result <User> > Execute(CreateUserCommand command, ICommandScope scope) { if (await this.IsEmailAddressInUse(command.EmailAddress).WithCurrentCulture()) { return(Result.AsError(CommandMessages.EmailIsNotAvailable)); } var user = new User { EmailAddress = command.EmailAddress, IsDeactivated = true, PreferredLanguage = command.Language, }; await this._documentSession.StoreAsync(user).WithCurrentCulture(); var authenticationData = new AuthenticationData { Id = AuthenticationData.CreateId(user.Id), UserId = user.Id, Salt = this._secretGenerator.Generate() }; authenticationData.Hash = this._saltCombiner.Combine(authenticationData.Salt, command.PasswordMD5Hash); await this._documentSession.StoreAsync(authenticationData).WithCurrentCulture(); return(Result.AsSuccess(user)); }
/// <summary> /// Executes the specified command. /// </summary> /// <param name="command">The command.</param> /// <param name="scope">The scope.</param> public async Task <Result <AuthenticationToken> > Execute(ValidateLoginAndCreateTokenCommand command, ICommandScope scope) { Result <User> userResult = await this.GetUserWithEmailAddress(command.EmailAddress, scope).WithCurrentCulture(); if (userResult.IsError) { return(Result.AsError(userResult.Message)); } if (userResult.Data.IsDeactivated) { return(Result.AsError(CommandMessages.UserIsDeactivated)); } AuthenticationData authenticationData = await this._documentSession .LoadAsync <AuthenticationData>(AuthenticationData.CreateId(userResult.Data.Id)) .WithCurrentCulture(); byte[] passedHash = this._saltCombiner.Combine(authenticationData.Salt, command.PasswordMD5Hash); if (authenticationData.Hash.SequenceEqual(passedHash) == false) { return(Result.AsError(CommandMessages.PasswordIncorrect)); } var token = new AuthenticationToken { Token = this._secretGenerator.GenerateString(), CreatedDate = DateTimeOffset.Now, UserId = userResult.Data.Id, ValidUntil = DateTimeOffset.Now.AddHours(ValidTokenDurationInHours), Client = new Client { ClientId = command.ClientId, Version = command.ClientVersion, IP = command.ClientIP } }; await this._documentSession.StoreAsync(token).WithCurrentCulture(); return(Result.AsSuccess(token)); }
/// <summary> /// Customizes the raven database. /// </summary> /// <param name="documentStore">The document store.</param> private void CustomizeRavenDocumentStore(DocumentStore documentStore) { documentStore.Conventions.RegisterAsyncIdConvention <AuthenticationData>((databaseName, commands, entity) => { return(Task.FromResult(AuthenticationData.CreateId(entity.UserId))); }); documentStore.Conventions.RegisterAsyncIdConvention <AuthenticationToken>((databaseName, commands, entity) => { return(Task.FromResult(AuthenticationToken.CreateId(entity.Token))); }); documentStore.Conventions.RegisterAsyncIdConvention <MileageInternalSettings>((databaseName, commands, entity) => { return(Task.FromResult(MileageInternalSettings.CreateId())); }); documentStore.Conventions.RegisterAsyncIdConvention <MileageSettings>((databaseName, commands, entity) => { return(Task.FromResult(MileageSettings.CreateId())); }); documentStore.Conventions.RegisterAsyncIdConvention <StoredLayout>((databaseName, commands, entity) => { return(Task.FromResult(StoredLayout.CreateId(entity.UserId, entity.LayoutName))); }); }