Skip to content

[PM-19883] Add untrust devices endpoint #5619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Api/Auth/Models/Request/UntrustDevicesModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
๏ปฟusing System.ComponentModel.DataAnnotations;

#nullable enable

namespace Bit.Api.Auth.Models.Request;

public class UntrustDevicesRequestModel
{
[Required]
public IEnumerable<Guid> Devices { get; set; } = null!;

Check warning on line 10 in src/Api/Auth/Models/Request/UntrustDevicesModel.cs

View check run for this annotation

Codecov / codecov/patch

src/Api/Auth/Models/Request/UntrustDevicesModel.cs#L10

Added line #L10 was not covered by tests
}
17 changes: 17 additions & 0 deletions src/Api/Controllers/DevicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Bit.Api.Models.Response;
using Bit.Core.Auth.Models.Api.Request;
using Bit.Core.Auth.Models.Api.Response;
using Bit.Core.Auth.UserFeatures.DeviceTrust;
using Bit.Core.Context;
using Bit.Core.Exceptions;
using Bit.Core.Repositories;
Expand All @@ -21,6 +22,7 @@
private readonly IDeviceRepository _deviceRepository;
private readonly IDeviceService _deviceService;
private readonly IUserService _userService;
private readonly IUntrustDevicesCommand _untrustDevicesCommand;
private readonly IUserRepository _userRepository;
private readonly ICurrentContext _currentContext;
private readonly ILogger<DevicesController> _logger;
Expand All @@ -29,13 +31,15 @@
IDeviceRepository deviceRepository,
IDeviceService deviceService,
IUserService userService,
IUntrustDevicesCommand untrustDevicesCommand,
IUserRepository userRepository,
ICurrentContext currentContext,
ILogger<DevicesController> logger)
{
_deviceRepository = deviceRepository;
_deviceService = deviceService;
_userService = userService;
_untrustDevicesCommand = untrustDevicesCommand;
_userRepository = userRepository;
_currentContext = currentContext;
_logger = logger;
Expand Down Expand Up @@ -165,6 +169,19 @@
model.OtherDevices ?? Enumerable.Empty<OtherDeviceKeysUpdateRequestModel>());
}

[HttpPost("untrust")]
public async Task PostUntrust([FromBody] UntrustDevicesRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);

Check warning on line 175 in src/Api/Controllers/DevicesController.cs

View check run for this annotation

Codecov / codecov/patch

src/Api/Controllers/DevicesController.cs#L174-L175

Added lines #L174 - L175 were not covered by tests

if (user == null)
{
throw new UnauthorizedAccessException();

Check warning on line 179 in src/Api/Controllers/DevicesController.cs

View check run for this annotation

Codecov / codecov/patch

src/Api/Controllers/DevicesController.cs#L178-L179

Added lines #L178 - L179 were not covered by tests
}

await _untrustDevicesCommand.UntrustDevices(user, model.Devices);
}

Check warning on line 183 in src/Api/Controllers/DevicesController.cs

View check run for this annotation

Codecov / codecov/patch

src/Api/Controllers/DevicesController.cs#L182-L183

Added lines #L182 - L183 were not covered by tests

[HttpPut("identifier/{identifier}/token")]
[HttpPost("identifier/{identifier}/token")]
public async Task PutToken(string identifier, [FromBody] DeviceTokenRequestModel model)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
๏ปฟusing Bit.Core.Entities;

namespace Bit.Core.Auth.UserFeatures.DeviceTrust;

public interface IUntrustDevicesCommand
{
public Task UntrustDevices(User user, IEnumerable<Guid> devicesToUntrust);
}
39 changes: 39 additions & 0 deletions src/Core/Auth/UserFeatures/DeviceTrust/UntrustDevicesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
๏ปฟusing Bit.Core.Entities;
using Bit.Core.Repositories;

namespace Bit.Core.Auth.UserFeatures.DeviceTrust;

public class UntrustDevicesCommand : IUntrustDevicesCommand
{
private readonly IDeviceRepository _deviceRepository;

public UntrustDevicesCommand(
IDeviceRepository deviceRepository)
{
_deviceRepository = deviceRepository;
}

public async Task UntrustDevices(User user, IEnumerable<Guid> devicesToUntrust)
{
var userDevices = await _deviceRepository.GetManyByUserIdAsync(user.Id);
var deviceIdDict = userDevices.ToDictionary(device => device.Id);

// Validate that the user owns all devices that they passed in
foreach (var deviceId in devicesToUntrust)
{
if (!deviceIdDict.ContainsKey(deviceId))
{
throw new UnauthorizedAccessException($"User {user.Id} does not have access to device {deviceId}");
}
}

foreach (var deviceId in devicesToUntrust)
{
var device = deviceIdDict[deviceId];
device.EncryptedPrivateKey = null;
device.EncryptedPublicKey = null;
device.EncryptedUserKey = null;
await _deviceRepository.UpsertAsync(device);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
๏ปฟ

using Bit.Core.Auth.UserFeatures.DeviceTrust;
using Bit.Core.Auth.UserFeatures.Registration;
using Bit.Core.Auth.UserFeatures.Registration.Implementations;
using Bit.Core.Auth.UserFeatures.TdeOffboardingPassword.Interfaces;
Expand All @@ -22,13 +23,19 @@ public static class UserServiceCollectionExtensions
public static void AddUserServices(this IServiceCollection services, IGlobalSettings globalSettings)
{
services.AddScoped<IUserService, UserService>();
services.AddDeviceTrustCommands();
services.AddUserPasswordCommands();
services.AddUserRegistrationCommands();
services.AddWebAuthnLoginCommands();
services.AddTdeOffboardingPasswordCommands();
services.AddTwoFactorQueries();
}

public static void AddDeviceTrustCommands(this IServiceCollection services)
{
services.AddScoped<IUntrustDevicesCommand, UntrustDevicesCommand>();
}

public static void AddUserKeyCommands(this IServiceCollection services, IGlobalSettings globalSettings)
{
services.AddScoped<IRotateUserKeyCommand, RotateUserKeyCommand>();
Expand Down
4 changes: 4 additions & 0 deletions test/Api.Test/Auth/Controllers/DevicesControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bit.Api.Models.Response;
using Bit.Core.Auth.Models.Api.Response;
using Bit.Core.Auth.Models.Data;
using Bit.Core.Auth.UserFeatures.DeviceTrust;
using Bit.Core.Context;
using Bit.Core.Entities;
using Bit.Core.Enums;
Expand All @@ -19,6 +20,7 @@ public class DevicesControllerTest
private readonly IDeviceRepository _deviceRepositoryMock;
private readonly IDeviceService _deviceServiceMock;
private readonly IUserService _userServiceMock;
private readonly IUntrustDevicesCommand _untrustDevicesCommand;
private readonly IUserRepository _userRepositoryMock;
private readonly ICurrentContext _currentContextMock;
private readonly IGlobalSettings _globalSettingsMock;
Expand All @@ -30,6 +32,7 @@ public DevicesControllerTest()
_deviceRepositoryMock = Substitute.For<IDeviceRepository>();
_deviceServiceMock = Substitute.For<IDeviceService>();
_userServiceMock = Substitute.For<IUserService>();
_untrustDevicesCommand = Substitute.For<IUntrustDevicesCommand>();
_userRepositoryMock = Substitute.For<IUserRepository>();
_currentContextMock = Substitute.For<ICurrentContext>();
_loggerMock = Substitute.For<ILogger<DevicesController>>();
Expand All @@ -38,6 +41,7 @@ public DevicesControllerTest()
_deviceRepositoryMock,
_deviceServiceMock,
_userServiceMock,
_untrustDevicesCommand,
_userRepositoryMock,
_currentContextMock,
_loggerMock);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
๏ปฟusing Bit.Core.Auth.UserFeatures.DeviceTrust;
using Bit.Core.Entities;
using Bit.Core.Repositories;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using NSubstitute;
using Xunit;

namespace Bit.Core.Test.Auth.UserFeatures.WebAuthnLogin;

[SutProviderCustomize]
public class UntrustDevicesCommandTests
{
[Theory, BitAutoData]
public async Task SetsKeysToNull(SutProvider<UntrustDevicesCommand> sutProvider, User user)
{
var deviceId = Guid.NewGuid();
// Arrange
sutProvider.GetDependency<IDeviceRepository>()
.GetManyByUserIdAsync(user.Id)
.Returns([new Device
{
Id = deviceId,
EncryptedPrivateKey = "encryptedPrivateKey",
EncryptedPublicKey = "encryptedPublicKey",
EncryptedUserKey = "encryptedUserKey"
}]);

// Act
await sutProvider.Sut.UntrustDevices(user, new List<Guid> { deviceId });

// Assert
await sutProvider.GetDependency<IDeviceRepository>()
.Received()
.UpsertAsync(Arg.Is<Device>(d =>
d.Id == deviceId &&
d.EncryptedPrivateKey == null &&
d.EncryptedPublicKey == null &&
d.EncryptedUserKey == null));
}

[Theory, BitAutoData]
public async Task RejectsWrongUser(SutProvider<UntrustDevicesCommand> sutProvider, User user)
{
var deviceId = Guid.NewGuid();
// Arrange
sutProvider.GetDependency<IDeviceRepository>()
.GetManyByUserIdAsync(user.Id)
.Returns([]);

// Act
await Assert.ThrowsAsync<UnauthorizedAccessException>(async () =>
await sutProvider.Sut.UntrustDevices(user, new List<Guid> { deviceId }));
}
}
Loading