-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
5ae232e
commit cc96e35
Showing
21 changed files
with
620 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Core/Auth/Models/Api/Response/DeviceAuthRequestResponseModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Bit.Core.Auth.Models.Data; | ||
using Bit.Core.Auth.Utilities; | ||
using Bit.Core.Enums; | ||
using Bit.Core.Models.Api; | ||
|
||
namespace Bit.Core.Auth.Models.Api.Response; | ||
|
||
public class DeviceAuthRequestResponseModel : ResponseModel | ||
{ | ||
public DeviceAuthRequestResponseModel() | ||
: base("device") { } | ||
|
||
public static DeviceAuthRequestResponseModel From(DeviceAuthDetails deviceAuthDetails) | ||
{ | ||
var converted = new DeviceAuthRequestResponseModel | ||
{ | ||
Id = deviceAuthDetails.Id, | ||
Name = deviceAuthDetails.Name, | ||
Type = deviceAuthDetails.Type, | ||
Identifier = deviceAuthDetails.Identifier, | ||
CreationDate = deviceAuthDetails.CreationDate, | ||
IsTrusted = deviceAuthDetails.IsTrusted() | ||
}; | ||
|
||
if (deviceAuthDetails.AuthRequestId != null && deviceAuthDetails.AuthRequestCreatedAt != null) | ||
{ | ||
converted.DevicePendingAuthRequest = new PendingAuthRequest | ||
{ | ||
Id = (Guid)deviceAuthDetails.AuthRequestId, | ||
CreationDate = (DateTime)deviceAuthDetails.AuthRequestCreatedAt | ||
}; | ||
} | ||
|
||
return converted; | ||
} | ||
|
||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public DeviceType Type { get; set; } | ||
public string Identifier { get; set; } | ||
public DateTime CreationDate { get; set; } | ||
public bool IsTrusted { get; set; } | ||
|
||
public PendingAuthRequest DevicePendingAuthRequest { get; set; } | ||
|
||
public class PendingAuthRequest | ||
{ | ||
public Guid Id { get; set; } | ||
public DateTime CreationDate { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using Bit.Core.Auth.Utilities; | ||
using Bit.Core.Entities; | ||
using Bit.Core.Enums; | ||
|
||
namespace Bit.Core.Auth.Models.Data; | ||
|
||
public class DeviceAuthDetails : Device | ||
{ | ||
public bool IsTrusted { get; set; } | ||
public Guid? AuthRequestId { get; set; } | ||
public DateTime? AuthRequestCreatedAt { get; set; } | ||
|
||
/** | ||
* Constructor for EF response. | ||
*/ | ||
public DeviceAuthDetails( | ||
Device device, | ||
Guid? authRequestId, | ||
DateTime? authRequestCreationDate) | ||
{ | ||
if (device == null) | ||
{ | ||
throw new ArgumentNullException(nameof(device)); | ||
} | ||
|
||
Id = device.Id; | ||
Name = device.Name; | ||
Type = device.Type; | ||
Identifier = device.Identifier; | ||
CreationDate = device.CreationDate; | ||
IsTrusted = device.IsTrusted(); | ||
AuthRequestId = authRequestId; | ||
AuthRequestCreatedAt = authRequestCreationDate; | ||
} | ||
|
||
/** | ||
* Constructor for dapper response. | ||
* Note: if the authRequestId or authRequestCreationDate is null it comes back as | ||
* an empty guid and a min value for datetime. That could change if the stored | ||
* procedure runs on a different kind of db. | ||
*/ | ||
public DeviceAuthDetails( | ||
Guid id, | ||
Guid userId, | ||
string name, | ||
short type, | ||
string identifier, | ||
string pushToken, | ||
DateTime creationDate, | ||
DateTime revisionDate, | ||
string encryptedUserKey, | ||
string encryptedPublicKey, | ||
string encryptedPrivateKey, | ||
bool active, | ||
Guid authRequestId, | ||
DateTime authRequestCreationDate) | ||
{ | ||
Id = id; | ||
Name = name; | ||
Type = (DeviceType)type; | ||
Identifier = identifier; | ||
CreationDate = creationDate; | ||
IsTrusted = new Device | ||
{ | ||
Id = id, | ||
UserId = userId, | ||
Name = name, | ||
Type = (DeviceType)type, | ||
Identifier = identifier, | ||
PushToken = pushToken, | ||
RevisionDate = revisionDate, | ||
EncryptedUserKey = encryptedUserKey, | ||
EncryptedPublicKey = encryptedPublicKey, | ||
EncryptedPrivateKey = encryptedPrivateKey, | ||
Active = active | ||
}.IsTrusted(); | ||
AuthRequestId = authRequestId != Guid.Empty ? authRequestId : null; | ||
AuthRequestCreatedAt = | ||
authRequestCreationDate != DateTime.MinValue ? authRequestCreationDate : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
| ||
using Bit.Core.Auth.Entities; | ||
using Bit.Core.Auth.Entities; | ||
|
||
namespace Bit.Core.Auth.Models.Data; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...structure.EntityFramework/Auth/Repositories/Queries/DeviceWithPendingAuthByUserIdQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.