use of password.pwm.util.operations.otp.OTPUserRecord in project pwm by pwm-project.
the class SetupOtpServlet method handleRestValidateCode.
@ActionHandler(action = "restValidateCode")
private ProcessStatus handleRestValidateCode(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException, ServletException, ChaiUnavailableException {
final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
final PwmSession pwmSession = pwmRequest.getPwmSession();
final OTPUserRecord otpUserRecord = pwmSession.getUserInfo().getOtpUserRecord();
final OtpService otpService = pwmApplication.getOtpService();
final String bodyString = pwmRequest.readRequestBodyAsString();
final Map<String, String> clientValues = JsonUtil.deserializeStringMap(bodyString);
final String code = Validator.sanitizeInputValue(pwmApplication.getConfig(), clientValues.get("code"), 1024);
try {
final boolean passed = otpService.validateToken(pwmRequest.getSessionLabel(), pwmSession.getUserInfo().getUserIdentity(), otpUserRecord, code, false);
final RestResultBean restResultBean = RestResultBean.withData(passed);
LOGGER.trace(pwmSession, "returning result for restValidateCode: " + JsonUtil.serialize(restResultBean));
pwmRequest.outputJsonResult(restResultBean);
} catch (PwmOperationalException e) {
final String errorMsg = "error during otp code validation: " + e.getMessage();
LOGGER.error(pwmSession, errorMsg);
pwmRequest.outputJsonResult(RestResultBean.fromError(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg), pwmRequest));
}
return ProcessStatus.Continue;
}
use of password.pwm.util.operations.otp.OTPUserRecord in project pwm by pwm-project.
the class SetupOtpServlet method initializeBean.
private void initializeBean(final PwmRequest pwmRequest, final SetupOtpBean otpBean) throws PwmUnrecoverableException {
final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
final PwmSession pwmSession = pwmRequest.getPwmSession();
// has pre-existing, nothing to do.
if (otpBean.isHasPreExistingOtp()) {
return;
}
final OtpService service = pwmApplication.getOtpService();
final UserIdentity theUser = pwmSession.getUserInfo().getUserIdentity();
// first time here
if (otpBean.getOtpUserRecord() == null) {
final OTPUserRecord existingUserRecord;
try {
existingUserRecord = service.readOTPUserConfiguration(pwmRequest.getSessionLabel(), theUser);
} catch (ChaiUnavailableException e) {
throw PwmUnrecoverableException.fromChaiException(e);
}
if (existingUserRecord != null) {
otpBean.setHasPreExistingOtp(true);
LOGGER.trace(pwmSession, "user has existing otp record");
return;
}
}
// make a new user record.
if (otpBean.getOtpUserRecord() == null) {
try {
final Configuration config = pwmApplication.getConfig();
final SetupOtpProfile setupOtpProfile = getSetupOtpProfile(pwmRequest);
final String identifierConfigValue = setupOtpProfile.readSettingAsString(PwmSetting.OTP_SECRET_IDENTIFIER);
final String identifier = pwmSession.getSessionManager().getMacroMachine(pwmApplication).expandMacros(identifierConfigValue);
final OTPUserRecord otpUserRecord = new OTPUserRecord();
final List<String> rawRecoveryCodes = pwmApplication.getOtpService().initializeUserRecord(setupOtpProfile, otpUserRecord, pwmRequest.getSessionLabel(), identifier);
otpBean.setOtpUserRecord(otpUserRecord);
otpBean.setRecoveryCodes(rawRecoveryCodes);
LOGGER.trace(pwmSession, "generated new otp record");
if (config.isDevDebugMode()) {
LOGGER.trace(pwmRequest, "newly generated otp record: " + JsonUtil.serialize(otpUserRecord));
}
} catch (Exception e) {
final String errorMsg = "error setting up new OTP secret: " + e.getMessage();
LOGGER.error(pwmSession, errorMsg);
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg));
}
}
}
use of password.pwm.util.operations.otp.OTPUserRecord in project pwm by pwm-project.
the class ForgottenPasswordServlet method processEnterOtpToken.
@ActionHandler(action = "enterOtp")
private ProcessStatus processEnterOtpToken(final PwmRequest pwmRequest) throws IOException, ServletException, PwmUnrecoverableException, ChaiUnavailableException {
final ForgottenPasswordBean forgottenPasswordBean = forgottenPasswordBean(pwmRequest);
final String userEnteredCode = pwmRequest.readParameterAsString(PwmConstants.PARAM_TOKEN);
LOGGER.debug(pwmRequest, String.format("entered OTP: %s", userEnteredCode));
final UserInfo userInfo = ForgottenPasswordUtil.readUserInfo(pwmRequest, forgottenPasswordBean);
final OTPUserRecord otpUserRecord = userInfo.getOtpUserRecord();
final boolean otpPassed;
if (otpUserRecord != null) {
LOGGER.info(pwmRequest, "checking entered OTP");
try {
// forces service to use proxy account to update (write) updated otp record if necessary.
otpPassed = pwmRequest.getPwmApplication().getOtpService().validateToken(null, forgottenPasswordBean.getUserIdentity(), otpUserRecord, userEnteredCode, true);
if (otpPassed) {
StatisticsManager.incrementStat(pwmRequest, Statistic.RECOVERY_OTP_PASSED);
LOGGER.debug(pwmRequest, "one time password validation has been passed");
forgottenPasswordBean.getProgress().getSatisfiedMethods().add(IdentityVerificationMethod.OTP);
} else {
StatisticsManager.incrementStat(pwmRequest, Statistic.RECOVERY_OTP_FAILED);
handleUserVerificationBadAttempt(pwmRequest, forgottenPasswordBean, new ErrorInformation(PwmError.ERROR_INCORRECT_OTP_TOKEN));
}
} catch (PwmOperationalException e) {
handleUserVerificationBadAttempt(pwmRequest, forgottenPasswordBean, new ErrorInformation(PwmError.ERROR_INCORRECT_OTP_TOKEN, e.getErrorInformation().toDebugStr()));
}
}
return ProcessStatus.Continue;
}
Aggregations