1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-10 07:38:13 -05:00

Update push token on login to allow multiple users on mobile devices ()

* Changed query for device to include userId

(cherry picked from commit 5e3f6db64bda449a8647ac05e69a822e6c5d462a)

* Refactored push registration to allow notification on multiple clients

(cherry picked from commit 75d299ae269eeb8ac272c96458815a359ea6d085)

* Linting

(cherry picked from commit f1cf54ebef2019743834f667861f9b34c1661e11)

* Fixed compile error.

* Removed class that I created when refactoring.

* Removed references to PushNotification from DeviceService tests.

* Refactored to not pass back a result on Save

* Refactored to send requestDevice to push notifications.

* Fixed whitespace.

* Added missing Noop services.

(cherry picked from commit bdad6cfadaf2779c2e672027122c95ea64e3cf0b)

* Linting.

* Refactored to put the push token back in SaveAsync.

* Removed constructor parameter.

* Added back in ClearTokenAsync to reduce risk.

* Updated tab for linting.
This commit is contained in:
Todd Martin 2022-11-16 10:30:28 -05:00 committed by GitHub
parent aa952e11df
commit 24469e2267
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 12 deletions
src
Api/Controllers
Core/Services
Identity/IdentityServer
test/IntegrationTestCommon/Factories

@ -17,7 +17,6 @@ public class DevicesController : Controller
private readonly IDeviceService _deviceService; private readonly IDeviceService _deviceService;
private readonly IUserService _userService; private readonly IUserService _userService;
private readonly IUserRepository _userRepository; private readonly IUserRepository _userRepository;
public DevicesController( public DevicesController(
IDeviceRepository deviceRepository, IDeviceRepository deviceRepository,
IDeviceService deviceService, IDeviceService deviceService,
@ -101,6 +100,7 @@ public class DevicesController : Controller
} }
await _deviceService.SaveAsync(model.ToDevice(device)); await _deviceService.SaveAsync(model.ToDevice(device));
} }
[AllowAnonymous] [AllowAnonymous]

@ -7,4 +7,5 @@ public interface IDeviceService
Task SaveAsync(Device device); Task SaveAsync(Device device);
Task ClearTokenAsync(Device device); Task ClearTokenAsync(Device device);
Task DeleteAsync(Device device); Task DeleteAsync(Device device);
} }

@ -41,6 +41,7 @@ public class DeviceService : IDeviceService
public async Task DeleteAsync(Device device) public async Task DeleteAsync(Device device)
{ {
await _deviceRepository.DeleteAsync(device); await _deviceRepository.DeleteAsync(device);
await _pushRegistrationService.DeleteRegistrationAsync(device.Id.ToString()); await _pushRegistrationService.DeleteRegistrationAsync(device.Id.ToString());
} }
} }

@ -37,7 +37,6 @@ public abstract class BaseRequestValidator<T> where T : class
private readonly IPolicyRepository _policyRepository; private readonly IPolicyRepository _policyRepository;
private readonly IUserRepository _userRepository; private readonly IUserRepository _userRepository;
private readonly ICaptchaValidationService _captchaValidationService; private readonly ICaptchaValidationService _captchaValidationService;
public BaseRequestValidator( public BaseRequestValidator(
UserManager<User> userManager, UserManager<User> userManager,
IDeviceRepository deviceRepository, IDeviceRepository deviceRepository,
@ -545,19 +544,16 @@ public abstract class BaseRequestValidator<T> where T : class
private async Task<Device> SaveDeviceAsync(User user, ValidatedTokenRequest request) private async Task<Device> SaveDeviceAsync(User user, ValidatedTokenRequest request)
{ {
var device = GetDeviceFromRequest(request); var deviceFromRequest = GetDeviceFromRequest(request);
if (device != null) if (deviceFromRequest != null)
{ {
var existingDevice = await GetKnownDeviceAsync(user, request); var existingDevice = await GetKnownDeviceAsync(user, request);
if (existingDevice == null) if (existingDevice == null)
{ {
device.UserId = user.Id;
await _deviceService.SaveAsync(device);
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
if (now - user.CreationDate > TimeSpan.FromMinutes(10)) if (now - user.CreationDate > TimeSpan.FromMinutes(10))
{ {
var deviceType = device.Type.GetType().GetMember(device.Type.ToString()) var deviceType = deviceFromRequest.Type.GetType().GetMember(deviceFromRequest.Type.ToString())
.FirstOrDefault()?.GetCustomAttribute<DisplayAttribute>()?.GetName(); .FirstOrDefault()?.GetCustomAttribute<DisplayAttribute>()?.GetName();
if (!_globalSettings.DisableEmailNewDevice) if (!_globalSettings.DisableEmailNewDevice)
{ {
@ -565,14 +561,13 @@ public abstract class BaseRequestValidator<T> where T : class
_currentContext.IpAddress); _currentContext.IpAddress);
} }
} }
return device;
} }
return existingDevice; deviceFromRequest.UserId = user.Id;
await _deviceService.SaveAsync(deviceFromRequest);
} }
return null; return deviceFromRequest;
} }
private async Task ResetFailedAuthDetailsAsync(User user) private async Task ResetFailedAuthDetailsAsync(User user)

@ -86,6 +86,14 @@ public abstract class WebApplicationFactoryBase<T> : WebApplicationFactory<T>
services.Remove(eventRepositoryService); services.Remove(eventRepositoryService);
services.AddSingleton<IEventRepository, EventRepository>(); services.AddSingleton<IEventRepository, EventRepository>();
var mailDeliveryService = services.First(sd => sd.ServiceType == typeof(IMailDeliveryService));
services.Remove(mailDeliveryService);
services.AddSingleton<IMailDeliveryService, NoopMailDeliveryService>();
var captchaValidationService = services.First(sd => sd.ServiceType == typeof(ICaptchaValidationService));
services.Remove(captchaValidationService);
services.AddSingleton<ICaptchaValidationService, NoopCaptchaValidationService>();
// Our Rate limiter works so well that it begins to fail tests unless we carve out // Our Rate limiter works so well that it begins to fail tests unless we carve out
// one whitelisted ip. We should still test the rate limiter though and they should change the Ip // one whitelisted ip. We should still test the rate limiter though and they should change the Ip
// to something that is NOT whitelisted // to something that is NOT whitelisted