/// <inheritdoc /> /// <summary> /// Explicitly implement this interface method - which overrides the base class's implementation /// </summary> /// <param name="purpose"></param> /// <param name="token"></param> /// <param name="manager"></param> /// <param name="user"></param> /// <returns></returns> Task <bool> IUserTokenProvider <BackOfficeIdentityUser, int> .ValidateAsync(string purpose, string token, UserManager <BackOfficeIdentityUser, int> manager, BackOfficeIdentityUser user) { if (purpose == Constants.GoogleAuthenticatorProviderName) { var twoFactorAuthenticator = new TwoFactorAuthenticator(); var database = ApplicationContext.Current.DatabaseContext.Database; var result = database.Fetch <TwoFactor>(string.Format("WHERE [userId] = {0} AND [key] = '{1}' AND [confirmed] = 1", user.Id, Constants.GoogleAuthenticatorProviderName)); if (result.Any() == false) { return(Task.FromResult(false)); } var key = result.First().Value; var validToken = twoFactorAuthenticator.ValidateTwoFactorPIN(key, token); return(Task.FromResult(validToken)); } if (purpose == Constants.YubiKeyProviderName) { var yubiKeyService = new YubiKeyService(); var response = yubiKeyService.Validate(token, user.Id); return(Task.FromResult(response != null && response.Status == YubicoResponseStatus.Ok)); } return(Task.FromResult(false)); }
public bool ValidateAndSave(string code) { var database = DatabaseContext.Database; var user = Security.CurrentUser; var insertSucces = false; try { var yubiKeyService = new YubiKeyService(); var response = yubiKeyService.Validate(code); if (response != null && response.Status == YubicoResponseStatus.Ok) { var result = database.Insert(new TwoFactor { UserId = user.Id, Key = Constants.YubiKeyProviderName, Value = response.PublicId, Confirmed = true }); if (result is bool) { insertSucces = (bool)result; } } } catch (Exception ex) { LogHelper.Error <TwoFactorAuthController>("Could not log in with the provided one-time-password", ex); } return(insertSucces); }