From d300c82ffd417823096319e95f68c73ee40820d1 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 23 Jul 2016 02:49:57 -0400 Subject: [PATCH] Added support for device registration during two factor login --- src/Api/Controllers/AuthController.cs | 2 +- src/Api/Models/Request/AuthTokenTwoFactorRequestModel.cs | 1 + src/Core/Identity/JwtBearerSignInManager.cs | 9 ++++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Api/Controllers/AuthController.cs b/src/Api/Controllers/AuthController.cs index 9b6dbeb596..5b8931d3bb 100644 --- a/src/Api/Controllers/AuthController.cs +++ b/src/Api/Controllers/AuthController.cs @@ -45,7 +45,7 @@ namespace Bit.Api.Controllers [Authorize("TwoFactor")] public async Task PostTokenTwoFactor([FromBody]AuthTokenTwoFactorRequestModel model) { - var result = await _signInManager.TwoFactorSignInAsync(_currentContext.User, model.Provider, model.Code); + var result = await _signInManager.TwoFactorSignInAsync(_currentContext.User, model.Provider, model.Code, model.Device?.ToDevice()); if(result == JwtBearerSignInResult.Success) { return new AuthTokenResponseModel(result.Token, result.User); diff --git a/src/Api/Models/Request/AuthTokenTwoFactorRequestModel.cs b/src/Api/Models/Request/AuthTokenTwoFactorRequestModel.cs index f059aa1967..27a8bca1bc 100644 --- a/src/Api/Models/Request/AuthTokenTwoFactorRequestModel.cs +++ b/src/Api/Models/Request/AuthTokenTwoFactorRequestModel.cs @@ -8,5 +8,6 @@ namespace Bit.Api.Models public string Code { get; set; } [Required] public string Provider { get; set; } + public DeviceRequestModel Device { get; set; } } } diff --git a/src/Core/Identity/JwtBearerSignInManager.cs b/src/Core/Identity/JwtBearerSignInManager.cs index 67140a8a1b..02081c63d7 100644 --- a/src/Core/Identity/JwtBearerSignInManager.cs +++ b/src/Core/Identity/JwtBearerSignInManager.cs @@ -96,7 +96,7 @@ namespace Bit.Core.Identity return await PasswordSignInAsync(user, password, device); } - public async Task TwoFactorSignInAsync(User user, string provider, string code) + public async Task TwoFactorSignInAsync(User user, string provider, string code, Device device = null) { if(user == null) { @@ -111,6 +111,13 @@ namespace Bit.Core.Identity success.Token = token; success.User = user; + var existingDevice = await _deviceRepository.GetByIdentifierAsync(device.Identifier, user.Id); + if(existingDevice == null) + { + device.UserId = user.Id; + await _deviceRepository.CreateAsync(device); + } + return success; }