From db3316dd6064fc980d7c12f6c7bebbc4b73b4eab Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 28 Aug 2018 22:21:13 -0400 Subject: [PATCH] token providers cant inject userservice --- .../Identity/AuthenticatorTokenProvider.cs | 10 ++++++---- src/Core/Identity/DuoWebTokenProvider.cs | 19 ++++++++++++------- src/Core/Identity/U2fTokenProvider.cs | 18 +++++++++++------- src/Core/Identity/YubicoOtpTokenProvider.cs | 16 ++++++++++------ 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/Core/Identity/AuthenticatorTokenProvider.cs b/src/Core/Identity/AuthenticatorTokenProvider.cs index ddde7c23f..8f8e3ae26 100644 --- a/src/Core/Identity/AuthenticatorTokenProvider.cs +++ b/src/Core/Identity/AuthenticatorTokenProvider.cs @@ -5,16 +5,17 @@ using Bit.Core.Models.Table; using Bit.Core.Enums; using OtpNet; using Bit.Core.Services; +using Microsoft.Extensions.DependencyInjection; namespace Bit.Core.Identity { public class AuthenticatorTokenProvider : IUserTwoFactorTokenProvider { - private readonly IUserService _userService; + private readonly IServiceProvider _serviceProvider; - public AuthenticatorTokenProvider(IUserService userService) + public AuthenticatorTokenProvider(IServiceProvider serviceProvider) { - _userService = userService; + _serviceProvider = serviceProvider; } public async Task CanGenerateTwoFactorTokenAsync(UserManager manager, User user) @@ -24,7 +25,8 @@ namespace Bit.Core.Identity { return false; } - return await user.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.Authenticator, _userService); + return await user.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.Authenticator, + _serviceProvider.GetRequiredService()); } public Task GenerateAsync(string purpose, UserManager manager, User user) diff --git a/src/Core/Identity/DuoWebTokenProvider.cs b/src/Core/Identity/DuoWebTokenProvider.cs index 66abe896d..a651bed5e 100644 --- a/src/Core/Identity/DuoWebTokenProvider.cs +++ b/src/Core/Identity/DuoWebTokenProvider.cs @@ -4,26 +4,29 @@ using Bit.Core.Models.Table; using Bit.Core.Enums; using Bit.Core.Utilities.Duo; using Bit.Core.Models; +using System; +using Microsoft.Extensions.DependencyInjection; using Bit.Core.Services; namespace Bit.Core.Identity { public class DuoWebTokenProvider : IUserTwoFactorTokenProvider { - private readonly IUserService _userService; + private readonly IServiceProvider _serviceProvider; private readonly GlobalSettings _globalSettings; public DuoWebTokenProvider( - IUserService userService, + IServiceProvider serviceProvider, GlobalSettings globalSettings) { - _userService = userService; + _serviceProvider = serviceProvider; _globalSettings = globalSettings; } public async Task CanGenerateTwoFactorTokenAsync(UserManager manager, User user) { - if(!(await _userService.CanAccessPremium(user))) + var userService = _serviceProvider.GetRequiredService(); + if(!(await userService.CanAccessPremium(user))) { return false; } @@ -34,12 +37,13 @@ namespace Bit.Core.Identity return false; } - return await user.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.Duo, _userService); + return await user.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.Duo, userService); } public async Task GenerateAsync(string purpose, UserManager manager, User user) { - if(!(await _userService.CanAccessPremium(user))) + var userService = _serviceProvider.GetRequiredService(); + if(!(await userService.CanAccessPremium(user))) { return null; } @@ -57,7 +61,8 @@ namespace Bit.Core.Identity public async Task ValidateAsync(string purpose, string token, UserManager manager, User user) { - if(!(await _userService.CanAccessPremium(user))) + var userService = _serviceProvider.GetRequiredService(); + if(!(await userService.CanAccessPremium(user))) { return false; } diff --git a/src/Core/Identity/U2fTokenProvider.cs b/src/Core/Identity/U2fTokenProvider.cs index 5f9a8f504..632f8af08 100644 --- a/src/Core/Identity/U2fTokenProvider.cs +++ b/src/Core/Identity/U2fTokenProvider.cs @@ -12,28 +12,30 @@ using U2F.Core.Models; using U2F.Core.Exceptions; using System; using Bit.Core.Services; +using Microsoft.Extensions.DependencyInjection; namespace Bit.Core.Identity { public class U2fTokenProvider : IUserTwoFactorTokenProvider { - private readonly IUserService _userService; + private readonly IServiceProvider _serviceProvider; private readonly IU2fRepository _u2fRepository; private readonly GlobalSettings _globalSettings; public U2fTokenProvider( - IUserService userService, + IServiceProvider serviceProvider, IU2fRepository u2fRepository, GlobalSettings globalSettings) { - _userService = userService; + _serviceProvider = serviceProvider; _u2fRepository = u2fRepository; _globalSettings = globalSettings; } public async Task CanGenerateTwoFactorTokenAsync(UserManager manager, User user) { - if(!(await _userService.CanAccessPremium(user))) + var userService = _serviceProvider.GetRequiredService(); + if(!(await userService.CanAccessPremium(user))) { return false; } @@ -44,12 +46,13 @@ namespace Bit.Core.Identity return false; } - return await user.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.U2f, _userService); + return await user.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.U2f, userService); } public async Task GenerateAsync(string purpose, UserManager manager, User user) { - if(!(await _userService.CanAccessPremium(user))) + var userService = _serviceProvider.GetRequiredService(); + if(!(await userService.CanAccessPremium(user))) { return null; } @@ -108,7 +111,8 @@ namespace Bit.Core.Identity public async Task ValidateAsync(string purpose, string token, UserManager manager, User user) { - if(!(await _userService.CanAccessPremium(user)) || string.IsNullOrWhiteSpace(token)) + var userService = _serviceProvider.GetRequiredService(); + if(!(await userService.CanAccessPremium(user)) || string.IsNullOrWhiteSpace(token)) { return false; } diff --git a/src/Core/Identity/YubicoOtpTokenProvider.cs b/src/Core/Identity/YubicoOtpTokenProvider.cs index 63a229461..1bd19487e 100644 --- a/src/Core/Identity/YubicoOtpTokenProvider.cs +++ b/src/Core/Identity/YubicoOtpTokenProvider.cs @@ -5,25 +5,28 @@ using Bit.Core.Enums; using YubicoDotNetClient; using System.Linq; using Bit.Core.Services; +using System; +using Microsoft.Extensions.DependencyInjection; namespace Bit.Core.Identity { public class YubicoOtpTokenProvider : IUserTwoFactorTokenProvider { - private readonly IUserService _userService; + private readonly IServiceProvider _serviceProvider; private readonly GlobalSettings _globalSettings; public YubicoOtpTokenProvider( - IUserService userService, + IServiceProvider serviceProvider, GlobalSettings globalSettings) { - _userService = userService; + _serviceProvider = serviceProvider; _globalSettings = globalSettings; } public async Task CanGenerateTwoFactorTokenAsync(UserManager manager, User user) { - if(!(await _userService.CanAccessPremium(user))) + var userService = _serviceProvider.GetRequiredService(); + if(!(await userService.CanAccessPremium(user))) { return false; } @@ -34,7 +37,7 @@ namespace Bit.Core.Identity return false; } - return await user.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.YubiKey, _userService); + return await user.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.YubiKey, userService); } public Task GenerateAsync(string purpose, UserManager manager, User user) @@ -44,7 +47,8 @@ namespace Bit.Core.Identity public async Task ValidateAsync(string purpose, string token, UserManager manager, User user) { - if(!(await _userService.CanAccessPremium(user))) + var userService = _serviceProvider.GetRequiredService(); + if(!(await userService.CanAccessPremium(user))) { return false; }