public async Task <int> Handle(CreateUpdateUserCommand request, CancellationToken cancellationToken) { var uid = _userManager.GetUserUid(); var claims = request.Claims; var user = await _context.Users .SingleOrDefaultAsync(x => x.Uid.Equals(uid), cancellationToken); if (user == null) { // Create new user user = new User { Email = claims[ClaimTypes.Email], FirstName = claims[ClaimTypes.GivenName], LastName = claims[ClaimTypes.Surname], Uid = uid, Username = claims["preferred_username"] }; await _context.Users.AddAsync(user, cancellationToken); } else { // Try to update user entry user.Email = claims[ClaimTypes.Email]; user.FirstName = claims[ClaimTypes.GivenName]; user.LastName = claims[ClaimTypes.Surname]; user.Username = claims["preferred_username"]; } await _context.SaveChangesAsync(cancellationToken); return(user.Id); }
public async Task <Unit> Handle(UpdateServerCommand command, CancellationToken cancellationToken) { // Get server with specified id var server = await _context.Servers .SingleOrDefaultAsync(x => x.Id == command.Id, cancellationToken); // Throw an error if not found if (server == null) { throw new NullReferenceException($"A server with id #{command.Id} could not be found."); } // Ensure name is unique var exists = await _context.Servers.AnyAsync(x => x.Name == command.Server.Name && x.Id != command.Id, cancellationToken); if (exists) { throw new ArgumentException($"A server with name '{command.Server.Name}' already exists."); } // Update server properties server.Name = command.Server.Name; server.Description = command.Server.Description; server.AssignedRange = command.Server.AssignedRange.ToAddress(); server.Dns = IPAddress.Parse(command.Server.Dns); server.Endpoint = command.Server.Endpoint; server.ListenPort = command.Server.ListenPort; server.PublicKey = command.Server.PublicKey; // Save changes await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(UpdateClientCommand command, CancellationToken cancellationToken) { var client = await _context.Clients .SingleOrDefaultAsync(x => x.Id == command.Id, cancellationToken); if (client == null) { throw new NullReferenceException($"Client with id #{command.Id} could not be found."); } // Ensure client is unique await _clientValidator.EnsureUniqueClientOnUpdate( client.Id, command.Client.Name, command.Client.AssignedIp, client.ServerId, cancellationToken ); // Update properties client.Name = command.Client.Name; client.Description = command.Client.Description; client.AssignedIp = command.Client.AssignedIp.ToAddress(); client.IsRevoked = command.Client.IsRevoked ?? client.IsRevoked; client.PublicKey = command.Client.PublicKey; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <int> Handle(CreateServerCommand command, CancellationToken cancellationToken) { var exists = await _context.Servers.AnyAsync(x => x.Name == command.Server.Name, cancellationToken); if (exists) { throw new ArgumentException($"A server with name '{command.Server.Name}' already exists."); } var server = new WireguardServer { Name = command.Server.Name, Description = command.Server.Description, AssignedRange = command.Server.AssignedRange.ToAddress(), Dns = IPAddress.Parse(command.Server.Dns), Endpoint = command.Server.Endpoint, ListenPort = command.Server.ListenPort, PublicKey = command.Server.PublicKey }; await _context.Servers.AddAsync(server, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(server.Id); }
public async Task <int> Handle(CreateNetworkAddressCommand command, CancellationToken cancellationToken) { var address = new Domain.Entitites.NetworkAddress { Address = command.Address.ToAddress() }; await _context.NetworkAddresses.AddAsync(address, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(address.Id); }
public async Task <int> Handle(AddAddressToClientCommand command, CancellationToken cancellationToken) { var clientAddress = new ClientNetworkAddress { ClientId = command.ClientId, NetworkAddressId = command.NetworkAddressId, }; await _context.ClientNetworkAddresses.AddAsync(clientAddress, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(clientAddress.Id); }
public async Task <int> Handle(AddAddressToServerCommand command, CancellationToken cancellationToken) { var serverAddress = new ServerNetworkAddress { NetworkAddressId = command.NetworkAddressId, ServerId = command.ServerId }; await _context.ServerNetworkAddresses.AddAsync(serverAddress, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(serverAddress.Id); }
public async Task <Unit> Handle(DeleteServerCommand command, CancellationToken cancellationToken) { var server = await _context.Servers .SingleOrDefaultAsync(x => x.Id == command.Id, cancellationToken); if (server == null) { throw new NullReferenceException($"Server with id #{command.Id} could not be found."); } _context.Servers.Remove(server); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(RemoveAddressFromClientCommand command, CancellationToken cancellationToken) { var clientAddress = await _context.ClientNetworkAddresses .SingleOrDefaultAsync(x => x.ClientId == command.ClientId && x.NetworkAddressId == command.NetworkAddressId); if (clientAddress == null) { throw new NullReferenceException($"A network address #{command.NetworkAddressId} on client #{command.ClientId} could not be found."); } _context.ClientNetworkAddresses.Remove(clientAddress); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <int> Handle(CreateClientCommand command, CancellationToken cancellationToken) { var server = await _context.Servers.AsNoTracking().SingleOrDefaultAsync(x => x.Id == command.ServerId, cancellationToken); if (server == null) { throw new NullReferenceException($"A server with id #{command.ServerId} could not be found."); } // Ensure client is unique await _clientValidator.EnsureUniqueClientOnCreate( command.Client.Name, command.Client.AssignedIp, command.ServerId, command.Client.IsIpAutoGenerated == true, cancellationToken ); var network = IPNetwork.Parse($"{server.AssignedRange.addr}/{server.AssignedRange.cidr}"); // Handle IP address if (command.Client.IsIpAutoGenerated == true) { // Get a list of all usable addresses var available = network.ListIPAddress(FilterEnum.Usable).ToList(); available.Remove(network.FirstUsable); var addresses = await _context.Clients .AsNoTracking() .Where(x => x.ServerId == command.ServerId) .Select(x => x.AssignedIp) .ToListAsync(); // Remove the ones that are in use foreach (var addr in addresses) { available.Remove(addr.addr); } // Grab the first remaining one command.Client.AssignedIp = $"{available.First()}/32"; } else { // Check that assigned IP is in range var isInRange = network.Contains(command.Client.AssignedIp.ToAddress().addr); if (!isInRange) { throw new ArgumentException($"Assigned IP address was not in range {server.AssignedRange.addr}/{server.AssignedRange.cidr}"); } } var client = new WireguardClient { Name = command.Client.Name, Description = command.Client.Description, AssignedIp = command.Client.AssignedIp.ToAddress(), IsRevoked = command.Client.IsRevoked ?? false, PublicKey = command.Client.PublicKey, ServerId = command.ServerId }; await _context.Clients.AddAsync(client, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(client.Id); }