/// <inheritdoc /> public async Task <AuthenticatedUser> AuthenticateAsync(string userName, string password) { var userDb = this.context.Users.SingleOrDefault(x => x.UserName == userName); if (userDb == null) { userDb = new User { Id = SecurityEngine.GenerateId(), UserName = userName, Password = SecurityEngine.HashPassword(password), CreationDate = DateTime.UtcNow, LastLoginDate = DateTime.UtcNow, }; this.context.Users.Add(userDb); await this.context.SaveChangesAsync(); return(this.mapper.Map <AuthenticatedUser>(userDb)); } if (SecurityEngine.VerifyPassword(password, userDb.Password)) { userDb.LastLoginDate = DateTime.UtcNow; await this.context.SaveChangesAsync(); return(this.mapper.Map <AuthenticatedUser>(userDb)); } return(null); }
public void GenerateId(int iterations) { var guids = new List <Guid>(); for (var i = 0; i < iterations; i++) { guids.Add(SecurityEngine.GenerateId()); } Assert.True(guids.Distinct().Count() == guids.Count); }
/// <inheritdoc /> public void Add(Guid userId, string projectName) { var project = new Model.Project(); project.Id = SecurityEngine.GenerateId(); project.UserId = userId; project.Name = projectName; this.SetCreated(project, userId); this.context.Projects.Add(project); this.context.SaveChanges(); }
private void Initialize(IStartupOptions startOptions) { LogInformation("Initializing core.."); if (File.Exists(startOptions.ConfigFile)) { LogInformation("Loading configuration."); //The IOManager is not initialized yet, so read the data directly. Settings = JsonConvert.DeserializeObject <MetaServerSettings>(File.ReadAllText(startOptions.ConfigFile)); } else { LogInformation("Initializing new configuration."); Settings = new MetaServerSettings(startOptions.RootPath) { //TODO: Add additional startup options. }; } #region Create Directory Structure. if (Directory.Exists(Settings.RootPath) == false) { Directory.CreateDirectory(Settings.RootPath); } if (Directory.Exists(Settings.TransactionPath) == false) { Directory.CreateDirectory(Settings.TransactionPath); } #endregion //Settings.LoginFile if (File.Exists(Settings.ConfigFile) == false) { //The IOManager is not initialized yet, so write the data directly. File.WriteAllText(Settings.ConfigFile, JsonConvert.SerializeObject(Settings)); } if (File.Exists(Settings.LoginFile) == false) { var loginCatalog = new MetaLoginCollection(); #if DEBUG var defaultLogin = new MetaLogin("root"); defaultLogin.SetPassword("p@ssWord!"); loginCatalog.Add(defaultLogin); #endif //The IOManager is not initialized yet, so write the data directly. File.WriteAllText(Settings.LoginFile, JsonConvert.SerializeObject(loginCatalog)); } LogInformation("Initializing security engine."); Security = new SecurityEngine(this); LogInformation("Initializing IO engine."); IO = new IOEngine(this); LogInformation("Initializing schema engine."); Schema = new SchemaEngine(this); LogInformation("Initializing session engine."); Session = new SessionEngine(this); LogInformation("Initializing latch engine."); Latch = new LatchEngine(this); LogInformation("Initializing transaction engine."); Transaction = new TransactionEngine(this); LogInformation("Initializing document engine."); Document = new DocumentEngine(this); LogInformation("Initializing query engine."); Query = new QueryEngine(this); LogInformation("Starting transaction recovery."); Transaction.Recover(); LogInformation("Transaction recovery complete."); }
public void HashPasswordOk(string password) { var hashed = SecurityEngine.HashPassword(password); Assert.True(SecurityEngine.VerifyPassword(password, hashed)); }
public void HashPasswordKo(string password) { var hashedWrong = SecurityEngine.HashPassword("42"); Assert.False(SecurityEngine.VerifyPassword(password, hashedWrong)); }