386 def processOtpAuthentication(self, requestParameters, user_name, identity, otp_auth_method):
387 facesMessages = CdiUtil.bean(FacesMessages)
388 facesMessages.setKeepMessages()
390 userService = CdiUtil.bean(UserService)
392 otpCode = ServerUtil.getFirstValue(requestParameters,
"loginForm:otpCode")
393 if StringHelper.isEmpty(otpCode):
394 facesMessages.add(FacesMessage.SEVERITY_ERROR,
"Failed to authenticate. OTP code is empty")
395 print "OTP. Process OTP authentication. otpCode is empty" 399 if otp_auth_method ==
"enroll":
401 otp_secret_key_encoded = identity.getWorkingParameter(
"otp_secret_key")
402 if otp_secret_key_encoded ==
None:
403 print "OTP. Process OTP authentication. OTP secret key is invalid" 406 otp_secret_key = self.fromBase64Url(otp_secret_key_encoded)
408 if self.otpType ==
"hotp":
409 validation_result = self.validateHotpKey(otp_secret_key, 1, otpCode)
411 if (validation_result !=
None)
and validation_result[
"result"]:
412 print "OTP. Process HOTP authentication during enrollment. otpCode is valid" 414 otp_user_external_uid =
"hotp:%s;%s" % ( otp_secret_key_encoded, validation_result[
"movingFactor"] )
417 find_user_by_external_uid = userService.addUserAttribute(user_name,
"oxExternalUid", otp_user_external_uid)
418 if find_user_by_external_uid !=
None:
421 print "OTP. Process HOTP authentication during enrollment. Failed to update user entry" 422 elif self.otpType ==
"totp":
423 validation_result = self.validateTotpKey(otp_secret_key, otpCode)
424 if (validation_result !=
None)
and validation_result[
"result"]:
425 print "OTP. Process TOTP authentication during enrollment. otpCode is valid" 427 otp_user_external_uid =
"totp:%s" % otp_secret_key_encoded
430 find_user_by_external_uid = userService.addUserAttribute(user_name,
"oxExternalUid", otp_user_external_uid)
431 if find_user_by_external_uid !=
None:
434 print "OTP. Process TOTP authentication during enrollment. Failed to update user entry" 435 elif otp_auth_method ==
"authenticate":
436 user_enrollments = self.findEnrollments(user_name)
438 if len(user_enrollments) == 0:
439 print "OTP. Process OTP authentication. There is no OTP enrollment for user '%s'" % user_name
440 facesMessages.add(FacesMessage.SEVERITY_ERROR,
"There is no valid OTP user enrollments")
443 if self.otpType ==
"hotp":
444 for user_enrollment
in user_enrollments:
445 user_enrollment_data = user_enrollment.split(
";")
446 otp_secret_key_encoded = user_enrollment_data[0]
449 moving_factor = StringHelper.toInteger(user_enrollment_data[1])
450 otp_secret_key = self.fromBase64Url(otp_secret_key_encoded)
453 validation_result = self.validateHotpKey(otp_secret_key, moving_factor, otpCode)
454 if (validation_result !=
None)
and validation_result[
"result"]:
455 print "OTP. Process HOTP authentication during authentication. otpCode is valid" 456 otp_user_external_uid =
"hotp:%s;%s" % ( otp_secret_key_encoded, moving_factor )
457 new_otp_user_external_uid =
"hotp:%s;%s" % ( otp_secret_key_encoded, validation_result[
"movingFactor"] )
460 find_user_by_external_uid = userService.replaceUserAttribute(user_name,
"oxExternalUid", otp_user_external_uid, new_otp_user_external_uid)
461 if find_user_by_external_uid !=
None:
464 print "OTP. Process HOTP authentication during authentication. Failed to update user entry" 465 elif self.otpType ==
"totp":
466 for user_enrollment
in user_enrollments:
467 otp_secret_key = self.fromBase64Url(user_enrollment)
470 validation_result = self.validateTotpKey(otp_secret_key, otpCode)
471 if (validation_result !=
None)
and validation_result[
"result"]:
472 print "OTP. Process TOTP authentication during authentication. otpCode is valid" 475 facesMessages.add(FacesMessage.SEVERITY_ERROR,
"Failed to authenticate. OTP code is invalid")
476 print "OTP. Process OTP authentication. OTP code is invalid"