128 def authenticate(self, configurationAttributes, requestParameters, step):
129 identity = CdiUtil.bean(Identity)
130 credentials = identity.getCredentials()
132 userService = CdiUtil.bean(UserService)
133 authenticationService = CdiUtil.bean(AuthenticationService)
135 saml_map_user =
False 136 saml_enroll_user =
False 137 saml_enroll_all_user_attr =
False 139 if configurationAttributes.containsKey(
"saml_deployment_type"):
140 saml_deployment_type = StringHelper.toLowerCase(configurationAttributes.get(
"saml_deployment_type").getValue2())
142 if StringHelper.equalsIgnoreCase(saml_deployment_type,
"map"):
145 if StringHelper.equalsIgnoreCase(saml_deployment_type,
"enroll"):
146 saml_enroll_user =
True 148 if StringHelper.equalsIgnoreCase(saml_deployment_type,
"enroll_all_attr"):
149 saml_enroll_all_user_attr =
True 151 saml_allow_basic_login =
False 152 if configurationAttributes.containsKey(
"saml_allow_basic_login"):
153 saml_allow_basic_login = StringHelper.toBoolean(configurationAttributes.get(
"saml_allow_basic_login").getValue2(),
False)
155 use_basic_auth =
False 156 if saml_allow_basic_login:
159 user_name = credentials.getUsername()
160 user_password = credentials.getPassword()
161 if StringHelper.isNotEmpty(user_name)
and StringHelper.isNotEmpty(user_password):
162 use_basic_auth =
True 164 if (step == 1)
and saml_allow_basic_login
and use_basic_auth:
165 print "Asimba. Authenticate for step 1. Basic authentication" 167 identity.setWorkingParameter(
"saml_count_login_steps", 1)
169 user_name = credentials.getUsername()
170 user_password = credentials.getPassword()
173 if StringHelper.isNotEmptyString(user_name)
and StringHelper.isNotEmptyString(user_password):
174 logged_in = authenticationService.authenticate(user_name, user_password)
182 print "Asimba. Authenticate for step 1" 184 currentSamlConfiguration = self.getCurrentSamlConfiguration(self.samlConfiguration, configurationAttributes, requestParameters)
185 if (currentSamlConfiguration ==
None):
186 print "Asimba. Prepare for step 1. Client saml configuration is invalid" 189 saml_response_array = requestParameters.get(
"SAMLResponse")
190 if ArrayHelper.isEmpty(saml_response_array):
191 print "Asimba. Authenticate for step 1. saml_response is empty" 194 saml_response = saml_response_array[0]
196 print "Asimba. Authenticate for step 1. saml_response: '%s'" % saml_response
198 samlResponse = Response(currentSamlConfiguration)
199 samlResponse.loadXmlFromBase64(saml_response)
201 saml_validate_response =
True 202 if configurationAttributes.containsKey(
"saml_validate_response"):
203 saml_validate_response = StringHelper.toBoolean(configurationAttributes.get(
"saml_validate_response").getValue2(),
False)
205 if saml_validate_response:
206 if not samlResponse.isValid():
207 print "Asimba. Authenticate for step 1. saml_response isn't valid" 210 if samlResponse.isAuthnFailed():
211 print "Asimba. Authenticate for step 1. saml_response AuthnFailed" 214 saml_response_attributes = samlResponse.getAttributes()
215 print "Asimba. Authenticate for step 1. attributes: '%s'" % saml_response_attributes
218 saml_user_uid = self.getSamlNameId(samlResponse)
219 if saml_user_uid ==
None:
223 print "Asimba. Authenticate for step 1. Attempting to find user by oxExternalUid: saml: '%s'" % saml_user_uid
226 find_user_by_uid = userService.getUserByAttribute(
"oxExternalUid",
"saml:%s" % saml_user_uid)
228 if find_user_by_uid ==
None:
229 print "Asimba. Authenticate for step 1. Failed to find user" 230 print "Asimba. Authenticate for step 1. Setting count steps to 2" 231 identity.setWorkingParameter(
"saml_count_login_steps", 2)
232 identity.setWorkingParameter(
"saml_user_uid", saml_user_uid)
235 found_user_name = find_user_by_uid.getUserId()
236 print "Asimba. Authenticate for step 1. found_user_name: '%s'" % found_user_name
238 user_authenticated = authenticationService.authenticate(found_user_name)
239 if user_authenticated ==
False:
240 print "Asimba. Authenticate for step 1. Failed to authenticate user" 243 print "Asimba. Authenticate for step 1. Setting count steps to 1" 244 identity.setWorkingParameter(
"saml_count_login_steps", 1)
246 post_login_result = self.samlExtensionPostLogin(configurationAttributes, find_user_by_uid)
247 print "Asimba. Authenticate for step 1. post_login_result: '%s'" % post_login_result
249 return post_login_result
250 elif saml_enroll_user:
252 newUser = self.getMappedUser(configurationAttributes, requestParameters, saml_response_attributes)
254 saml_user_uid = self.getNameId(samlResponse, newUser)
255 if saml_user_uid ==
None:
258 self.setDefaultUid(newUser, saml_user_uid)
259 newUser.setAttribute(
"oxExternalUid",
"saml:%s" % saml_user_uid)
262 print "Asimba. Authenticate for step 1. Attempting to find user by oxExternalUid: saml: '%s'" % saml_user_uid
265 find_user_by_uid = userService.getUserByAttribute(
"oxExternalUid",
"saml:%s" % saml_user_uid)
266 if find_user_by_uid ==
None:
268 print "Asimba. Authenticate for step 1. There is no user in LDAP. Adding user to local LDAP" 270 print "Asimba. Authenticate for step 1. Attempting to add user '%s' with next attributes: '%s'" % (saml_user_uid, newUser.getCustomAttributes())
271 user_unique = self.checkUserUniqueness(newUser)
273 print "Asimba. Authenticate for step 1. Failed to add user: '%s'. User not unique" % newUser.getUserId()
274 facesMessages = CdiUtil.bean(FacesMessages)
275 facesMessages.add(FacesMessage.SEVERITY_ERROR,
"Failed to enroll. User with same key attributes exist already")
276 facesMessages.setKeepMessages()
279 find_user_by_uid = userService.addUser(newUser,
True)
280 print "Asimba. Authenticate for step 1. Added new user with UID: '%s'" % find_user_by_uid.getUserId()
283 print "Asimba. Authenticate for step 1. Attempting to update user '%s' with next attributes: '%s'" % (saml_user_uid, newUser.getCustomAttributes())
284 find_user_by_uid.setCustomAttributes(newUser.getCustomAttributes())
285 userService.updateUser(find_user_by_uid)
286 print "Asimba. Authenticate for step 1. Updated user with UID: '%s'" % saml_user_uid
288 found_user_name = find_user_by_uid.getUserId()
289 print "Asimba. Authenticate for step 1. found_user_name: '%s'" % found_user_name
291 user_authenticated = authenticationService.authenticate(found_user_name)
292 if user_authenticated ==
False:
293 print "Asimba. Authenticate for step 1. Failed to authenticate user: '%s'" % found_user_name
296 print "Asimba. Authenticate for step 1. Setting count steps to 1" 297 identity.setWorkingParameter(
"saml_count_login_steps", 1)
299 post_login_result = self.samlExtensionPostLogin(configurationAttributes, find_user_by_uid)
300 print "Asimba. Authenticate for step 1. post_login_result: '%s'" % post_login_result
302 return post_login_result
303 elif saml_enroll_all_user_attr:
305 newUser = self.getMappedAllAttributesUser(saml_response_attributes)
307 saml_user_uid = self.getNameId(samlResponse, newUser)
308 if saml_user_uid ==
None:
311 self.setDefaultUid(newUser, saml_user_uid)
312 newUser.setAttribute(
"oxExternalUid",
"saml:%s" % saml_user_uid)
314 print "Asimba. Authenticate for step 1. Attempting to find user by oxExternalUid: saml:%s" % saml_user_uid
317 find_user_by_uid = userService.getUserByAttribute(
"oxExternalUid",
"saml:%s" % saml_user_uid)
318 if find_user_by_uid ==
None:
320 print "Asimba. Authenticate for step 1. There is no user in LDAP. Adding user to local LDAP" 322 print "Asimba. Authenticate for step 1. Attempting to add user '%s' with next attributes: '%s'" % (saml_user_uid, newUser.getCustomAttributes())
323 user_unique = self.checkUserUniqueness(newUser)
325 print "Asimba. Authenticate for step 1. Failed to add user: '%s'. User not unique" % newUser.getUserId()
326 facesMessages = CdiUtil.bean(FacesMessages)
327 facesMessages.add(FacesMessage.SEVERITY_ERROR,
"Failed to enroll. User with same key attributes exist already")
328 facesMessages.setKeepMessages()
331 find_user_by_uid = userService.addUser(newUser,
True)
332 print "Asimba. Authenticate for step 1. Added new user with UID: '%s'" % find_user_by_uid.getUserId()
335 print "Asimba. Authenticate for step 1. Attempting to update user '%s' with next attributes: '%s'" % (saml_user_uid, newUser.getCustomAttributes())
336 find_user_by_uid.setCustomAttributes(newUser.getCustomAttributes())
337 userService.updateUser(find_user_by_uid)
338 print "Asimba. Authenticate for step 1. Updated user with UID: '%s'" % saml_user_uid
340 found_user_name = find_user_by_uid.getUserId()
341 print "Asimba. Authenticate for step 1. found_user_name: '%s'" % found_user_name
343 user_authenticated = authenticationService.authenticate(found_user_name)
344 if user_authenticated ==
False:
345 print "Asimba. Authenticate for step 1. Failed to authenticate user" 348 print "Asimba. Authenticate for step 1. Setting count steps to 1" 349 identity.setWorkingParameter(
"saml_count_login_steps", 1)
351 post_login_result = self.samlExtensionPostLogin(configurationAttributes, find_user_by_uid)
352 print "Asimba. Authenticate for step 1. post_login_result: '%s'" % post_login_result
354 return post_login_result
356 if saml_user_uid ==
None:
360 print "Asimba. Authenticate for step 1. Attempting to find user by uid: '%s'" % saml_user_uid
362 find_user_by_uid = userService.getUser(saml_user_uid)
363 if find_user_by_uid ==
None:
364 print "Asimba. Authenticate for step 1. Failed to find user" 367 found_user_name = find_user_by_uid.getUserId()
368 print "Asimba. Authenticate for step 1. found_user_name: '%s'" % found_user_name
370 user_authenticated = authenticationService.authenticate(found_user_name)
371 if user_authenticated ==
False:
372 print "Asimba. Authenticate for step 1. Failed to authenticate user" 375 print "Asimba. Authenticate for step 1. Setting count steps to 1" 376 identity.setWorkingParameter(
"saml_count_login_steps", 1)
378 post_login_result = self.samlExtensionPostLogin(configurationAttributes, find_user_by_uid)
379 print "Asimba. Authenticate for step 1. post_login_result: '%s'" % post_login_result
381 return post_login_result
383 print "Asimba. Authenticate for step 2" 385 sessionAttributes = identity.getSessionId().getSessionAttributes()
386 if (sessionAttributes ==
None)
or not sessionAttributes.containsKey(
"saml_user_uid"):
387 print "Asimba. Authenticate for step 2. saml_user_uid is empty" 390 saml_user_uid = sessionAttributes.get(
"saml_user_uid")
391 passed_step1 = StringHelper.isNotEmptyString(saml_user_uid)
395 user_name = credentials.getUsername()
396 user_password = credentials.getPassword()
399 if StringHelper.isNotEmptyString(user_name)
and StringHelper.isNotEmptyString(user_password):
400 logged_in = authenticationService.authenticate(user_name, user_password)
407 find_user_by_uid = userService.getUserByAttribute(
"oxExternalUid",
"saml:%s" % saml_user_uid)
409 if find_user_by_uid ==
None:
411 find_user_by_uid = userService.addUserAttribute(user_name,
"oxExternalUid",
"saml:%s" % saml_user_uid)
412 if find_user_by_uid ==
None:
413 print "Asimba. Authenticate for step 2. Failed to update current user" 416 post_login_result = self.samlExtensionPostLogin(configurationAttributes, find_user_by_uid)
417 print "Asimba. Authenticate for step 2. post_login_result: '%s'" % post_login_result
419 return post_login_result
421 found_user_name = find_user_by_uid.getUserId()
422 print "Asimba. Authenticate for step 2. found_user_name: '%s'" % found_user_name
424 if StringHelper.equals(user_name, found_user_name):
425 post_login_result = self.samlExtensionPostLogin(configurationAttributes, find_user_by_uid)
426 print "Asimba. Authenticate for step 2. post_login_result: '%s'" % post_login_result
428 return post_login_result