1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 16:42:50 -05:00

Added device identifier, APIs for updating token by identifier, Device creation/update upon signin.

This commit is contained in:
Kyle Spearrin
2016-06-21 00:08:22 -04:00
parent 8a34692e7c
commit 37ec1de7a3
12 changed files with 109 additions and 6 deletions

View File

@ -9,6 +9,7 @@ namespace Bit.Core.Domains
public Guid UserId { get; set; }
public string Name { get; set; }
public Enums.DeviceType Type { get; set; }
public string Identifier { get; set; }
public string PushToken { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;

View File

@ -9,11 +9,14 @@ using Microsoft.Extensions.Options;
using Bit.Core.Domains;
using Microsoft.AspNetCore.Builder;
using Microsoft.IdentityModel.Tokens;
using Bit.Core.Repositories;
namespace Bit.Core.Identity
{
public class JwtBearerSignInManager
{
private readonly IDeviceRepository _deviceRepository;
public JwtBearerSignInManager(
UserManager<User> userManager,
IHttpContextAccessor contextAccessor,
@ -21,7 +24,8 @@ namespace Bit.Core.Identity
IOptions<IdentityOptions> optionsAccessor,
IOptions<JwtBearerIdentityOptions> jwtIdentityOptionsAccessor,
IOptions<JwtBearerOptions> jwtOptionsAccessor,
ILogger<JwtBearerSignInManager> logger)
ILogger<JwtBearerSignInManager> logger,
IDeviceRepository deviceRepository)
{
UserManager = userManager;
Context = contextAccessor.HttpContext;
@ -29,6 +33,7 @@ namespace Bit.Core.Identity
IdentityOptions = optionsAccessor?.Value ?? new IdentityOptions();
JwtIdentityOptions = jwtIdentityOptionsAccessor?.Value ?? new JwtBearerIdentityOptions();
JwtBearerOptions = jwtOptionsAccessor?.Value ?? new JwtBearerOptions();
_deviceRepository = deviceRepository;
}
internal UserManager<User> UserManager { get; set; }
@ -54,7 +59,7 @@ namespace Bit.Core.Identity
return Task.FromResult(false);
}
public async Task<JwtBearerSignInResult> PasswordSignInAsync(User user, string password)
public async Task<JwtBearerSignInResult> PasswordSignInAsync(User user, string password, Device device = null)
{
if(user == null)
{
@ -63,13 +68,23 @@ namespace Bit.Core.Identity
if(await UserManager.CheckPasswordAsync(user, password))
{
return await SignInOrTwoFactorAsync(user);
var result = await SignInOrTwoFactorAsync(user);
if(result.Succeeded && device != null)
{
var existingDevice = await _deviceRepository.GetByIdentifierAsync(device.Identifier, user.Id);
if(existingDevice == null)
{
await _deviceRepository.CreateAsync(device);
}
}
return result;
}
return JwtBearerSignInResult.Failed;
}
public async Task<JwtBearerSignInResult> PasswordSignInAsync(string userName, string password)
public async Task<JwtBearerSignInResult> PasswordSignInAsync(string userName, string password, Device device = null)
{
var user = await UserManager.FindByNameAsync(userName);
if(user == null)
@ -77,7 +92,7 @@ namespace Bit.Core.Identity
return JwtBearerSignInResult.Failed;
}
return await PasswordSignInAsync(user, password);
return await PasswordSignInAsync(user, password, device);
}
public async Task<JwtBearerSignInResult> TwoFactorSignInAsync(User user, string provider, string code)

View File

@ -8,6 +8,7 @@ namespace Bit.Core.Repositories
public interface IDeviceRepository : IRepository<Device, Guid>
{
Task<Device> GetByIdAsync(Guid id, Guid userId);
Task<Device> GetByIdentifierAsync(string identifier, Guid userId);
Task<ICollection<Device>> GetManyByUserIdAsync(Guid userId);
}
}

View File

@ -30,6 +30,23 @@ namespace Bit.Core.Repositories.SqlServer
return device;
}
public async Task<Device> GetByIdentifierAsync(string identifier, Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Device>(
$"[{Schema}].[{Table}_ReadByIdentifierUserId]",
new
{
UserId = userId,
Identifier = identifier
},
commandType: CommandType.StoredProcedure);
return results.FirstOrDefault();
}
}
public async Task<ICollection<Device>> GetManyByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))