using System; using System.Threading; using System.Threading.Tasks; using Bit.Core.Repositories; using Bit.Core.Services; using Microsoft.AspNetCore.Identity; namespace Bit.Core.Identity { public class ReadOnlyDatabaseIdentityUserStore : ReadOnlyIdentityUserStore { private readonly IUserService _userService; private readonly IUserRepository _userRepository; public ReadOnlyDatabaseIdentityUserStore( IUserService userService, IUserRepository userRepository) { _userService = userService; _userRepository = userRepository; } public override async Task FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken = default(CancellationToken)) { var user = await _userRepository.GetByEmailAsync(normalizedEmail); return user?.ToIdentityUser(await _userService.TwoFactorIsEnabledAsync(user)); } public override async Task FindByIdAsync(string userId, CancellationToken cancellationToken = default(CancellationToken)) { if (!Guid.TryParse(userId, out var userIdGuid)) { return null; } var user = await _userRepository.GetByIdAsync(userIdGuid); return user?.ToIdentityUser(await _userService.TwoFactorIsEnabledAsync(user)); } } }