382 def processOtpAuthentication(self, requestParameters, user_name, identity, otp_auth_method):
383 facesMessages = CdiUtil.bean(FacesMessages)
384 facesMessages.setKeepMessages()
386 userService = CdiUtil.bean(UserService)
388 otpCode = ServerUtil.getFirstValue(requestParameters,
"loginForm:otpCode")
389 if StringHelper.isEmpty(otpCode):
390 facesMessages.add(FacesMessage.SEVERITY_ERROR,
"Failed to authenticate. OTP code is empty")
391 print "OTP. Process OTP authentication. otpCode is empty" 395 if otp_auth_method ==
"enroll":
397 otp_secret_key_encoded = identity.getWorkingParameter(
"otp_secret_key")
398 if otp_secret_key_encoded ==
None:
399 print "OTP. Process OTP authentication. OTP secret key is invalid" 402 otp_secret_key = self.fromBase64Url(otp_secret_key_encoded)
404 if self.otpType ==
"hotp":
405 validation_result = self.validateHotpKey(otp_secret_key, 1, otpCode)
407 if (validation_result !=
None)
and validation_result[
"result"]:
408 print "OTP. Process HOTP authentication during enrollment. otpCode is valid" 410 otp_user_external_uid =
"hotp:%s;%s" % ( otp_secret_key_encoded, validation_result[
"movingFactor"] )
413 find_user_by_external_uid = userService.addUserAttribute(user_name,
"oxExternalUid", otp_user_external_uid)
414 if find_user_by_external_uid !=
None:
417 print "OTP. Process HOTP authentication during enrollment. Failed to update user entry" 418 elif self.otpType ==
"totp":
419 validation_result = self.validateTotpKey(otp_secret_key, otpCode)
420 if (validation_result !=
None)
and validation_result[
"result"]:
421 print "OTP. Process TOTP authentication during enrollment. otpCode is valid" 423 otp_user_external_uid =
"totp:%s" % otp_secret_key_encoded
426 find_user_by_external_uid = userService.addUserAttribute(user_name,
"oxExternalUid", otp_user_external_uid)
427 if find_user_by_external_uid !=
None:
430 print "OTP. Process TOTP authentication during enrollment. Failed to update user entry" 431 elif otp_auth_method ==
"authenticate":
432 user_enrollments = self.findEnrollments(user_name)
434 if len(user_enrollments) == 0:
435 print "OTP. Process OTP authentication. There is no OTP enrollment for user '%s'" % user_name
436 facesMessages.add(FacesMessage.SEVERITY_ERROR,
"There is no valid OTP user enrollments")
439 if self.otpType ==
"hotp":
440 for user_enrollment
in user_enrollments:
441 user_enrollment_data = user_enrollment.split(
";")
442 otp_secret_key_encoded = user_enrollment_data[0]
445 moving_factor = StringHelper.toInteger(user_enrollment_data[1])
446 otp_secret_key = self.fromBase64Url(otp_secret_key_encoded)
449 validation_result = self.validateHotpKey(otp_secret_key, moving_factor, otpCode)
450 if (validation_result !=
None)
and validation_result[
"result"]:
451 print "OTP. Process HOTP authentication during authentication. otpCode is valid" 452 otp_user_external_uid =
"hotp:%s;%s" % ( otp_secret_key_encoded, moving_factor )
453 new_otp_user_external_uid =
"hotp:%s;%s" % ( otp_secret_key_encoded, validation_result[
"movingFactor"] )
456 find_user_by_external_uid = userService.replaceUserAttribute(user_name,
"oxExternalUid", otp_user_external_uid, new_otp_user_external_uid)
457 if find_user_by_external_uid !=
None:
460 print "OTP. Process HOTP authentication during authentication. Failed to update user entry" 461 elif self.otpType ==
"totp":
462 for user_enrollment
in user_enrollments:
463 otp_secret_key = self.fromBase64Url(user_enrollment)
466 validation_result = self.validateTotpKey(otp_secret_key, otpCode)
467 if (validation_result !=
None)
and validation_result[
"result"]:
468 print "OTP. Process TOTP authentication during authentication. otpCode is valid" 471 facesMessages.add(FacesMessage.SEVERITY_ERROR,
"Failed to authenticate. OTP code is invalid")
472 print "OTP. Process OTP authentication. OTP code is invalid"