public async Task SetConnection(ConnectionPayload connection) { var client = new MongoClient(this.settings); var db = client.GetDatabase(database); var coll = db.GetCollection <ConnectionPayload>(collectionName); var foundConnection = await coll.Find(x => x.AccessToken == connection.AccessToken).SingleOrDefaultAsync(); if (foundConnection == null) { if (connection.Password != null) { connection.Password = VinzClortho.Encrypt(connection.Password, secKey + connection.Salt); } connection.InsertedOn = DateTime.UtcNow; await coll.InsertOneAsync(connection); return; } var currentPwd = connection.Password == null ? foundConnection.Password : VinzClortho.Encrypt(connection.Password, secKey + connection.Salt); var filter = Builders <ConnectionPayload> .Filter.Eq("Id", foundConnection.Id); var update = Builders <ConnectionPayload> .Update .Set("IsEnabled", connection.IsEnabled) .Set("Password", currentPwd) .Set("Port", connection.Port) .Set("Role", connection.Role) .Set("Server", connection.Server) .Set("UpdatedOn", DateTime.UtcNow); await coll.UpdateOneAsync(filter, update); }
public async Task <ConnectionPayload> GetConnection(RconPayload rconPayload) { var client = new MongoClient(this.settings); var db = client.GetDatabase(database); var coll = db.GetCollection <ConnectionPayload>(collectionName); var currentConnection = await coll.Find(x => x.AccessToken == rconPayload.AccessToken).SingleOrDefaultAsync(); if (currentConnection?.Password != null) { currentConnection.Password = VinzClortho.Decrypt(currentConnection.Password, secKey + rconPayload.Salt); } return(currentConnection); }