コード例 #1
0
        public static string ToProvider(this TwoFactorAuthenticationType value)
        {
            switch (value)
            {
            case TwoFactorAuthenticationType.Authenticator:
                return(TokenOptions.DefaultAuthenticatorProvider);

            case TwoFactorAuthenticationType.Email:
                return(TokenOptions.DefaultEmailProvider);

            case TwoFactorAuthenticationType.Phone:
                return(TokenOptions.DefaultPhoneProvider);

            default:
                return(string.Empty);
            }
        }
コード例 #2
0
 public static string ForType(TwoFactorAuthenticationType twoFactorAuthenticationType)
 {
     return(Map[twoFactorAuthenticationType]);
 }
コード例 #3
0
        public virtual async Task <TwoFactorCodeSetup> GenerateCodeSetup(string secretKey, Customer customer, Language language, TwoFactorAuthenticationType twoFactorAuthenticationType)
        {
            var model = new TwoFactorCodeSetup();

            switch (twoFactorAuthenticationType)
            {
            case TwoFactorAuthenticationType.AppVerification:
                var setupInfo = _twoFactorAuthentication.GenerateSetupCode(_storeContext.CurrentStore.CompanyName, customer.Email, secretKey, false, 3);
                model.CustomValues.Add("QrCodeImageUrl", setupInfo.QrCodeSetupImageUrl);
                model.CustomValues.Add("ManualEntryQrCode", setupInfo.ManualEntryKey);
                break;

            case TwoFactorAuthenticationType.EmailVerification:
                var token = PrepareRandomCode();
                await _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.TwoFactorValidCode, token);

                await _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.TwoFactorCodeValidUntil, DateTime.UtcNow.AddMinutes(30));

                model.CustomValues.Add("Token", token);
                await _workflowMessageService.SendCustomerEmailTokenValidationMessage(customer, _storeContext.CurrentStore, language.Id);

                break;

            case TwoFactorAuthenticationType.SMSVerification:
                var smsVerificationService = _serviceProvider.GetRequiredService <ISMSVerificationService>();
                model = await smsVerificationService.GenerateCode(secretKey, customer, language);

                break;

            default:
                break;
            }

            return(model);
        }
コード例 #4
0
        public virtual async Task <bool> AuthenticateTwoFactor(string secretKey, string token, Customer customer, TwoFactorAuthenticationType twoFactorAuthenticationType)
        {
            switch (twoFactorAuthenticationType)
            {
            case TwoFactorAuthenticationType.AppVerification:
                return(_twoFactorAuthentication.ValidateTwoFactorPIN(secretKey, token.Trim()));

            case TwoFactorAuthenticationType.EmailVerification:
                var customertoken = customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.TwoFactorValidCode);
                if (customertoken != token.Trim())
                {
                    return(false);
                }
                var validuntil = customer.GetAttributeFromEntity <DateTime>(SystemCustomerAttributeNames.TwoFactorCodeValidUntil);
                if (validuntil < DateTime.UtcNow)
                {
                    return(false);
                }

                return(true);

            case TwoFactorAuthenticationType.SMSVerification:
                var smsVerificationService = _serviceProvider.GetRequiredService <ISMSVerificationService>();
                return(await smsVerificationService.Authenticate(secretKey, token.Trim(), customer));

            default:
                return(false);
            }
        }
コード例 #5
0
        private async Task <Result <IEnumerable <string> > > AddTwoFactorAuthentication(string userId, TwoFactorAuthenticationType twoFactorAuthenticationType, string token)
        {
            AppUserEntity appUser = await _userManager.FindByIdAsync(userId);

            if (appUser == null)
            {
                _logger.LogError($"No user. UserId {userId}");
                return(Result.Fail <IEnumerable <string> >("no_user", "No User"));
            }

            bool isCodeValid = await _userManager.VerifyTwoFactorTokenAsync(appUser, twoFactorAuthenticationType.ToProvider(), token);

            if (!isCodeValid)
            {
                _logger.LogError($"Invalid TwoFactor Verification code. User {userId}");
                return(Result.Fail <IEnumerable <string> >("invlid_code", "Invalid Code"));
            }

            await _userManager.SetTwoFactorEnabledAsync(appUser, true);

            appUser.TwoFactor = twoFactorAuthenticationType;
            if (twoFactorAuthenticationType == TwoFactorAuthenticationType.Phone)
            {
                appUser.PhoneNumberConfirmed = true; //HACK: try to do this before you add 2fa
            }

            await _userManager.UpdateAsync(appUser);

            _logger.LogInformation($"TwoFactorAuthentication enabled. User {appUser.Id}");

            Result loginResult = await _loginService.Login(userId);

            if (loginResult.Failure)
            {
                _logger.LogError($"Failed to login user after enabling 2fa. UserId {userId}");
            }

            IEnumerable <string> recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(appUser, _identityUIEndpoints.NumberOfRecoveryCodes);

            return(Result.Ok(recoveryCodes));
        }