mirror of
https://github.com/bitwarden/server.git
synced 2025-07-13 13:47:30 -05:00
Auth/pm 2996/add auth request data to devices response model (#5152)
fix(auth): [PM-2996] Add Pending Auth Request Data to Devices Response - New stored procedure to fetch the appropriate data. - Updated devices controller to respond with the new data. - Tests written at the controller and repository level. Resolves PM-2996
This commit is contained in:

committed by
GitHub

parent
5ae232e336
commit
cc96e35072
@ -0,0 +1,38 @@
|
||||
using Bit.Core.Auth.Enums;
|
||||
using Bit.Core.Auth.Models.Data;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Auth.Repositories.Queries;
|
||||
|
||||
public class DeviceWithPendingAuthByUserIdQuery
|
||||
{
|
||||
public IQueryable<DeviceAuthDetails> GetQuery(
|
||||
DatabaseContext dbContext,
|
||||
Guid userId,
|
||||
int expirationMinutes)
|
||||
{
|
||||
var devicesWithAuthQuery = (
|
||||
from device in dbContext.Devices
|
||||
where device.UserId == userId && device.Active
|
||||
select new
|
||||
{
|
||||
device,
|
||||
authRequest =
|
||||
(
|
||||
from authRequest in dbContext.AuthRequests
|
||||
where authRequest.RequestDeviceIdentifier == device.Identifier
|
||||
where authRequest.Type == AuthRequestType.AuthenticateAndUnlock || authRequest.Type == AuthRequestType.Unlock
|
||||
where authRequest.Approved == null
|
||||
where authRequest.UserId == userId
|
||||
where authRequest.CreationDate.AddMinutes(expirationMinutes) > DateTime.UtcNow
|
||||
orderby authRequest.CreationDate descending
|
||||
select authRequest
|
||||
).First()
|
||||
}).Select(deviceWithAuthRequest => new DeviceAuthDetails(
|
||||
deviceWithAuthRequest.device,
|
||||
deviceWithAuthRequest.authRequest.Id,
|
||||
deviceWithAuthRequest.authRequest.CreationDate));
|
||||
|
||||
return devicesWithAuthQuery;
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
using AutoMapper;
|
||||
using Bit.Core.Auth.Models.Data;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Infrastructure.EntityFramework.Auth.Repositories.Queries;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@ -10,9 +13,17 @@ namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class DeviceRepository : Repository<Core.Entities.Device, Device, Guid>, IDeviceRepository
|
||||
{
|
||||
public DeviceRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
private readonly IGlobalSettings _globalSettings;
|
||||
|
||||
public DeviceRepository(
|
||||
IServiceScopeFactory serviceScopeFactory,
|
||||
IMapper mapper,
|
||||
IGlobalSettings globalSettings
|
||||
)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Devices)
|
||||
{ }
|
||||
{
|
||||
_globalSettings = globalSettings;
|
||||
}
|
||||
|
||||
public async Task ClearPushTokenAsync(Guid id)
|
||||
{
|
||||
@ -69,4 +80,15 @@ public class DeviceRepository : Repository<Core.Entities.Device, Device, Guid>,
|
||||
return Mapper.Map<List<Core.Entities.Device>>(devices);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<DeviceAuthDetails>> GetManyByUserIdWithDeviceAuth(Guid userId)
|
||||
{
|
||||
var expirationMinutes = (int)_globalSettings.PasswordlessAuth.UserRequestExpiration.TotalMinutes;
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new DeviceWithPendingAuthByUserIdQuery();
|
||||
return await query.GetQuery(dbContext, userId, expirationMinutes).ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user