mirror of
https://github.com/bitwarden/server.git
synced 2025-04-04 20:50:21 -05:00

* [PM-1203] feat: allow verification for all passwordless accounts (#3038) * [PM-1033] Org invite user creation flow 1 (#3028) * [PM-1033] feat: remove user verification from password enrollment * [PM-1033] feat: auto accept invitation when enrolling into password reset * [PM-1033] fix: controller tests * [PM-1033] refactor: `UpdateUserResetPasswordEnrollmentCommand` * [PM-1033] refactor(wip): make `AcceptUserCommand` * Revert "[PM-1033] refactor(wip): make `AcceptUserCommand`" This reverts commit dc1319e7fa70c4844bbc70e0b01089b682ac2843. * Revert "[PM-1033] refactor: `UpdateUserResetPasswordEnrollmentCommand`" This reverts commit 43df689c7f244af4f7ffec1f9768a72081a624c3. * [PM-1033] refactor: move invite accept to controller This avoids creating yet another method that depends on having `IUserService` passed in as a parameter * [PM-1033] fix: add missing changes * [PM-1381] Add Trusted Device Keys to Auth Response (#3066) * Return Keys for Trusted Device - Check whether the current logging in device is trusted - Return their keys on successful login * Formatting * Address PR Feedback * Add Remarks Comment * [PM-1338] `AuthRequest` Event Logs (#3046) * Update AuthRequestController - Only allow AdminApproval Requests to be created from authed endpoint - Add endpoint that has authentication to be able to create admin approval * Add PasswordlessAuthSettings - Add settings for customizing expiration times * Add new EventTypes * Add Logic for AdminApproval Type - Add logic for validating AdminApproval expiration - Add event logging for Approval/Disapproval of AdminApproval - Add logic for creating AdminApproval types * Add Test Helpers - Change BitAutoData to allow you to use string representations of common types. * Add/Update AuthRequestService Tests * Run Formatting * Switch to 7 Days * Add Test Covering ResponseDate Being Set * Address PR Feedback - Create helper for checking if date is expired - Move validation logic into smaller methods * Switch to User Event Type - Make RequestDeviceApproval user type - User types will log for each org user is in * [PM-2998] Move Approving Device Check (#3101) * Move Check for Approving Devices - Exclude currently logging in device - Remove old way of checking - Add tests asserting behavior * Update DeviceType list * Update Naming & Address PR Feedback * Fix Tests * Address PR Feedback * Formatting * Now Fully Update Naming? * Feature/auth/pm 2759/add can reset password to user decryption options (#3113) * PM-2759 - BaseRequestValidator.cs - CreateUserDecryptionOptionsAsync - Add new hasManageResetPasswordPermission for post SSO redirect logic required on client. * PM-2759 - Update IdentityServerSsoTests.cs to all pass based on the addition of HasManageResetPasswordPermission to TrustedDeviceUserDecryptionOption * IdentityServerSsoTests.cs - fix typo in test name: LoggingApproval --> LoginApproval * PM1259 - Add test case for verifying that TrustedDeviceOption.hasManageResetPasswordPermission is set properly based on user permission * dotnet format run * Feature/auth/pm 2759/add can reset password to user decryption options fix jit users (#3120) * PM-2759 - IdentityServer - CreateUserDecryptionOptionsAsync - hasManageResetPasswordPermission set logic was broken for JIT provisioned users as I assumed we would always have a list of at least 1 org during the SSO process. Added TODO for future test addition but getting this out there now as QA is blocked by being unable to create JIT provisioned users. * dotnet format * Tiny tweak * [PM-1339] Allow Rotating Device Keys (#3096) * Allow Rotation of Trusted Device Keys - Add endpoint for getting keys relating to rotation - Add endpoint for rotating your current device - In the same endpoint allow a list of other devices to rotate * Formatting * Use Extension Method * Add Tests from PR Co-authored-by: Jared Snider <jsnider@bitwarden.com> --------- Co-authored-by: Jared Snider <jsnider@bitwarden.com> * Check the user directly if they have the ResetPasswordKey (#3153) * PM-3327 - UpdateKeyAsync must exempt the currently calling device from the logout notification in order to prevent prematurely logging the user out before the client side key rotation process can complete. The calling device will log itself out once it is done. (#3170) * Allow OTP Requests When Users Are On TDE (#3184) * [PM-3356][PM-3292] Allow OTP For All (#3188) * Allow OTP For All - On a trusted device isn't a good check because a user might be using a trusted device locally but not trusted it long term - The logic wasn't working for KC users anyways * Remove Old Comment * [AC-1601] Added RequireSso policy as a dependency of TDE (#3209) * Added RequireSso policy as a dependency of TDE. * Added test for RequireSso for TDE. * Added save. * Fixed policy name. --------- Co-authored-by: Andreas Coroiu <acoroiu@bitwarden.com> Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: Vincent Salucci <vincesalucci21@gmail.com> Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com> Co-authored-by: Jared Snider <jsnider@bitwarden.com>
234 lines
7.5 KiB
C#
234 lines
7.5 KiB
C#
using Bit.Api.Auth.Models.Request;
|
|
using Bit.Api.Auth.Models.Request.Accounts;
|
|
using Bit.Api.Models.Request;
|
|
using Bit.Api.Models.Response;
|
|
using Bit.Core.Auth.Models.Api.Request;
|
|
using Bit.Core.Auth.Models.Api.Response;
|
|
using Bit.Core.Context;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Utilities;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Bit.Api.Controllers;
|
|
|
|
[Route("devices")]
|
|
[Authorize("Application")]
|
|
public class DevicesController : Controller
|
|
{
|
|
private readonly IDeviceRepository _deviceRepository;
|
|
private readonly IDeviceService _deviceService;
|
|
private readonly IUserService _userService;
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly ICurrentContext _currentContext;
|
|
|
|
public DevicesController(
|
|
IDeviceRepository deviceRepository,
|
|
IDeviceService deviceService,
|
|
IUserService userService,
|
|
IUserRepository userRepository,
|
|
ICurrentContext currentContext)
|
|
{
|
|
_deviceRepository = deviceRepository;
|
|
_deviceService = deviceService;
|
|
_userService = userService;
|
|
_userRepository = userRepository;
|
|
_currentContext = currentContext;
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<DeviceResponseModel> Get(string id)
|
|
{
|
|
var device = await _deviceRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
|
|
if (device == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
var response = new DeviceResponseModel(device);
|
|
return response;
|
|
}
|
|
|
|
[HttpGet("identifier/{identifier}")]
|
|
public async Task<DeviceResponseModel> GetByIdentifier(string identifier)
|
|
{
|
|
var device = await _deviceRepository.GetByIdentifierAsync(identifier, _userService.GetProperUserId(User).Value);
|
|
if (device == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
var response = new DeviceResponseModel(device);
|
|
return response;
|
|
}
|
|
|
|
[HttpGet("")]
|
|
public async Task<ListResponseModel<DeviceResponseModel>> Get()
|
|
{
|
|
ICollection<Device> devices = await _deviceRepository.GetManyByUserIdAsync(_userService.GetProperUserId(User).Value);
|
|
var responses = devices.Select(d => new DeviceResponseModel(d));
|
|
return new ListResponseModel<DeviceResponseModel>(responses);
|
|
}
|
|
|
|
[HttpPost("")]
|
|
public async Task<DeviceResponseModel> Post([FromBody] DeviceRequestModel model)
|
|
{
|
|
var device = model.ToDevice(_userService.GetProperUserId(User));
|
|
await _deviceService.SaveAsync(device);
|
|
|
|
var response = new DeviceResponseModel(device);
|
|
return response;
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
[HttpPost("{id}")]
|
|
public async Task<DeviceResponseModel> Put(string id, [FromBody] DeviceRequestModel model)
|
|
{
|
|
var device = await _deviceRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
|
|
if (device == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await _deviceService.SaveAsync(model.ToDevice(device));
|
|
|
|
var response = new DeviceResponseModel(device);
|
|
return response;
|
|
}
|
|
|
|
[HttpPut("{identifier}/keys")]
|
|
[HttpPost("{identifier}/keys")]
|
|
public async Task<DeviceResponseModel> PutKeys(string identifier, [FromBody] DeviceKeysRequestModel model)
|
|
{
|
|
var device = await _deviceRepository.GetByIdentifierAsync(identifier, _userService.GetProperUserId(User).Value);
|
|
if (device == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await _deviceService.SaveAsync(model.ToDevice(device));
|
|
|
|
var response = new DeviceResponseModel(device);
|
|
return response;
|
|
}
|
|
|
|
[HttpPost("{identifier}/retrieve-keys")]
|
|
public async Task<ProtectedDeviceResponseModel> GetDeviceKeys(string identifier, [FromBody] SecretVerificationRequestModel model)
|
|
{
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
|
|
if (user == null)
|
|
{
|
|
throw new UnauthorizedAccessException();
|
|
}
|
|
|
|
if (!await _userService.VerifySecretAsync(user, model.Secret))
|
|
{
|
|
await Task.Delay(2000);
|
|
throw new BadRequestException(string.Empty, "User verification failed.");
|
|
}
|
|
|
|
var device = await _deviceRepository.GetByIdentifierAsync(identifier, user.Id);
|
|
|
|
if (device == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
return new ProtectedDeviceResponseModel(device);
|
|
}
|
|
|
|
[HttpPost("update-trust")]
|
|
public async Task PostUpdateTrust([FromBody] UpdateDevicesTrustRequestModel model)
|
|
{
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
|
|
if (user == null)
|
|
{
|
|
throw new UnauthorizedAccessException();
|
|
}
|
|
|
|
if (!await _userService.VerifySecretAsync(user, model.Secret))
|
|
{
|
|
await Task.Delay(2000);
|
|
throw new BadRequestException(string.Empty, "User verification failed.");
|
|
}
|
|
|
|
await _deviceService.UpdateDevicesTrustAsync(
|
|
_currentContext.DeviceIdentifier,
|
|
user.Id,
|
|
model.CurrentDevice,
|
|
model.OtherDevices ?? Enumerable.Empty<OtherDeviceKeysUpdateRequestModel>());
|
|
}
|
|
|
|
[HttpPut("identifier/{identifier}/token")]
|
|
[HttpPost("identifier/{identifier}/token")]
|
|
public async Task PutToken(string identifier, [FromBody] DeviceTokenRequestModel model)
|
|
{
|
|
var device = await _deviceRepository.GetByIdentifierAsync(identifier, _userService.GetProperUserId(User).Value);
|
|
if (device == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await _deviceService.SaveAsync(model.ToDevice(device));
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPut("identifier/{identifier}/clear-token")]
|
|
[HttpPost("identifier/{identifier}/clear-token")]
|
|
public async Task PutClearToken(string identifier)
|
|
{
|
|
var device = await _deviceRepository.GetByIdentifierAsync(identifier);
|
|
if (device == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await _deviceService.ClearTokenAsync(device);
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
[HttpPost("{id}/delete")]
|
|
public async Task Delete(string id)
|
|
{
|
|
var device = await _deviceRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
|
|
if (device == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await _deviceService.DeleteAsync(device);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet("knowndevice")]
|
|
public async Task<bool> GetByIdentifierQuery(
|
|
[FromHeader(Name = "X-Request-Email")] string email,
|
|
[FromHeader(Name = "X-Device-Identifier")] string deviceIdentifier)
|
|
=> await GetByIdentifier(CoreHelpers.Base64UrlDecodeString(email), deviceIdentifier);
|
|
|
|
[Obsolete("Path is deprecated due to encoding issues, use /knowndevice instead.")]
|
|
[AllowAnonymous]
|
|
[HttpGet("knowndevice/{email}/{identifier}")]
|
|
public async Task<bool> GetByIdentifier(string email, string identifier)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(identifier))
|
|
{
|
|
throw new BadRequestException("Please provide an email and device identifier");
|
|
}
|
|
|
|
var user = await _userRepository.GetByEmailAsync(email);
|
|
if (user == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var device = await _deviceRepository.GetByIdentifierAsync(identifier, user.Id);
|
|
return device != null;
|
|
}
|
|
}
|