85 def authenticate(self, configurationAttributes, requestParameters, step):
86 identity = CdiUtil.bean(Identity)
87 userService = CdiUtil.bean(UserService)
88 authenticationService = CdiUtil.bean(AuthenticationService)
90 mapUserDeployment =
False 91 enrollUserDeployment =
False 92 if (configurationAttributes.containsKey(
"gplus_deployment_type")):
93 deploymentType = StringHelper.toLowerCase(configurationAttributes.get(
"gplus_deployment_type").getValue2())
95 if (StringHelper.equalsIgnoreCase(deploymentType,
"map")):
96 mapUserDeployment =
True 97 if (StringHelper.equalsIgnoreCase(deploymentType,
"enroll")):
98 enrollUserDeployment =
True 101 print "Google+ Authenticate for step 1" 103 gplusAuthCodeArray = requestParameters.get(
"gplus_auth_code")
104 gplusAuthCode = gplusAuthCodeArray[0]
108 if (StringHelper.isEmptyString(gplusAuthCode)):
113 print "Google+ Authenticate for step 1. Basic authentication" 115 identity.setWorkingParameter(
"gplus_count_login_steps", 1)
117 credentials = identity.getCredentials()
119 userName = credentials.getUsername()
120 userPassword = credentials.getPassword()
123 if (StringHelper.isNotEmptyString(userName)
and StringHelper.isNotEmptyString(userPassword)):
124 userService = CdiUtil.bean(UserService)
125 loggedIn = authenticationService.authenticate(userName, userPassword)
133 print "Google+ Authenticate for step 1. gplusAuthCode:", gplusAuthCode
135 currentClientSecrets = self.getCurrentClientSecrets(self.clientSecrets, configurationAttributes, requestParameters)
136 if (currentClientSecrets ==
None):
137 print "Google+ Authenticate for step 1. Client secrets configuration is invalid" 140 print "Google+ Authenticate for step 1. Attempting to gets tokens" 141 tokenResponse = self.getTokensByCode(self.clientSecrets, configurationAttributes, gplusAuthCode)
142 if ((tokenResponse ==
None)
or (tokenResponse.getIdToken() ==
None)
or (tokenResponse.getAccessToken() ==
None)):
143 print "Google+ Authenticate for step 1. Failed to get tokens" 146 print "Google+ Authenticate for step 1. Successfully gets tokens" 148 jwt = Jwt.parse(tokenResponse.getIdToken())
151 gplusUserUid = jwt.getClaims().getClaimAsString(JwtClaimName.SUBJECT_IDENTIFIER)
152 print "Google+ Authenticate for step 1. Found Google user ID in the ID token: '%s'" % gplusUserUid
154 if (mapUserDeployment):
156 print "Google+ Authenticate for step 1. Attempting to find user by oxExternalUid: 'gplus:%s'" % gplusUserUid
159 foundUser = userService.getUserByAttribute(
"oxExternalUid",
"gplus:" + gplusUserUid)
161 if (foundUser ==
None):
162 print "Google+ Authenticate for step 1. Failed to find user" 163 print "Google+ Authenticate for step 1. Setting count steps to 2" 164 identity.setWorkingParameter(
"gplus_count_login_steps", 2)
165 identity.setWorkingParameter(
"gplus_user_uid", gplusUserUid)
168 foundUserName = foundUser.getUserId()
169 print "Google+ Authenticate for step 1. foundUserName: '%s'" % foundUserName
171 userAuthenticated = authenticationService.authenticate(foundUserName)
172 if (userAuthenticated ==
False):
173 print "Google+ Authenticate for step 1. Failed to authenticate user" 176 print "Google+ Authenticate for step 1. Setting count steps to 1" 177 identity.setWorkingParameter(
"gplus_count_login_steps", 1)
179 postLoginResult = self.extensionPostLogin(configurationAttributes, foundUser)
180 print "Google+ Authenticate for step 1. postLoginResult: '%s'" % postLoginResult
182 return postLoginResult
183 elif (enrollUserDeployment):
185 print "Google+ Authenticate for step 1. Attempting to find user by oxExternalUid: 'gplus:%s'" % gplusUserUid
188 foundUser = userService.getUserByAttribute(
"oxExternalUid",
"gplus:" + gplusUserUid)
190 if (foundUser ==
None):
192 print "Google+ Authenticate for step 1. There is no user in LDAP. Adding user to local LDAP" 194 print "Google+ Authenticate for step 1. Attempting to gets user info" 195 userInfoResponse = self.getUserInfo(currentClientSecrets, configurationAttributes, tokenResponse.getAccessToken())
196 if ((userInfoResponse ==
None)
or (userInfoResponse.getClaims().size() == 0)):
197 print "Google+ Authenticate for step 1. Failed to get user info" 200 print "Google+ Authenticate for step 1. Successfully gets user info" 202 gplusResponseAttributes = userInfoResponse.getClaims()
205 gplusResponseNormalizedAttributes = HashMap()
206 for gplusResponseAttributeEntry
in gplusResponseAttributes.entrySet():
207 gplusResponseNormalizedAttributes.put(
208 StringHelper.toLowerCase(gplusResponseAttributeEntry.getKey()), gplusResponseAttributeEntry.getValue())
210 currentAttributesMapping = self.getCurrentAttributesMapping(self.attributesMapping, configurationAttributes, requestParameters)
211 print "Google+ Authenticate for step 1. Using next attributes mapping '%s'" % currentAttributesMapping
214 for attributesMappingEntry
in currentAttributesMapping.entrySet():
215 remoteAttribute = attributesMappingEntry.getKey()
216 localAttribute = attributesMappingEntry.getValue()
218 localAttributeValue = gplusResponseNormalizedAttributes.get(remoteAttribute)
219 if (localAttribute !=
None):
220 newUser.setAttribute(localAttribute, localAttributeValue)
222 if (newUser.getAttribute(
"sn") ==
None):
223 newUser.setAttribute(
"sn", gplusUserUid)
225 if (newUser.getAttribute(
"cn") ==
None):
226 newUser.setAttribute(
"cn", gplusUserUid)
231 if (newUser.getAttribute(
"oxTrustEmail")
is None and 232 newUser.getAttribute(
"mail")
is not None):
234 "value": newUser.getAttribute(
"mail"),
235 "display": newUser.getAttribute(
"mail"),
241 newUser.setAttribute(
"oxTrustEmail", json.dumps(oxTrustEmail))
243 newUser.setAttribute(
"oxExternalUid",
"gplus:" + gplusUserUid)
244 print "Google+ Authenticate for step 1. Attempting to add user '%s' with next attributes '%s'" % (gplusUserUid, newUser.getCustomAttributes())
246 foundUser = userService.addUser(newUser,
True)
247 print "Google+ Authenticate for step 1. Added new user with UID: '%s'" % foundUser.getUserId()
249 foundUserName = foundUser.getUserId()
250 print "Google+ Authenticate for step 1. foundUserName: '%s'" % foundUserName
252 userAuthenticated = authenticationService.authenticate(foundUserName)
253 if (userAuthenticated ==
False):
254 print "Google+ Authenticate for step 1. Failed to authenticate user" 257 print "Google+ Authenticate for step 1. Setting count steps to 1" 258 identity.setWorkingParameter(
"gplus_count_login_steps", 1)
260 print "Google+ Authenticate for step 1. Attempting to run extension postLogin" 261 postLoginResult = self.extensionPostLogin(configurationAttributes, foundUser)
262 print "Google+ Authenticate for step 1. postLoginResult: '%s'" % postLoginResult
264 return postLoginResult
267 print "Google+ Authenticate for step 1. Attempting to find user by uid: '%s'" % gplusUserUid
269 foundUser = userService.getUser(gplusUserUid)
270 if (foundUser ==
None):
271 print "Google+ Authenticate for step 1. Failed to find user" 274 foundUserName = foundUser.getUserId()
275 print "Google+ Authenticate for step 1. foundUserName: '%s'" % foundUserName
277 userAuthenticated = authenticationService.authenticate(foundUserName)
278 if (userAuthenticated ==
False):
279 print "Google+ Authenticate for step 1. Failed to authenticate user" 282 print "Google+ Authenticate for step 1. Setting count steps to 1" 283 identity.setWorkingParameter(
"gplus_count_login_steps", 1)
285 postLoginResult = self.extensionPostLogin(configurationAttributes, foundUser)
286 print "Google+ Authenticate for step 1. postLoginResult: '%s'" % postLoginResult
288 return postLoginResult
290 print "Google+ Authenticate for step 2" 292 sessionAttributes = identity.getSessionId().getSessionAttributes()
293 if (sessionAttributes ==
None)
or not sessionAttributes.containsKey(
"gplus_user_uid"):
294 print "Google+ Authenticate for step 2. gplus_user_uid is empty" 297 gplusUserUid = sessionAttributes.get(
"gplus_user_uid")
298 passed_step1 = StringHelper.isNotEmptyString(gplusUserUid)
299 if (
not passed_step1):
302 identity = CdiUtil.bean(Identity)
303 credentials = identity.getCredentials()
305 userName = credentials.getUsername()
306 userPassword = credentials.getPassword()
309 if (StringHelper.isNotEmptyString(userName)
and StringHelper.isNotEmptyString(userPassword)):
310 loggedIn = authenticationService.authenticate(userName, userPassword)
317 foundUser = userService.getUserByAttribute(
"oxExternalUid",
"gplus:" + gplusUserUid)
319 if (foundUser ==
None):
321 foundUser = userService.addUserAttribute(userName,
"oxExternalUid",
"gplus:" + gplusUserUid)
322 if (foundUser ==
None):
323 print "Google+ Authenticate for step 2. Failed to update current user" 326 postLoginResult = self.extensionPostLogin(configurationAttributes, foundUser)
327 print "Google+ Authenticate for step 2. postLoginResult: '%s'" % postLoginResult
329 return postLoginResult
331 foundUserName = foundUser.getUserId()
332 print "Google+ Authenticate for step 2. foundUserName: '%s'" % foundUserName
334 if StringHelper.equals(userName, foundUserName):
335 postLoginResult = self.extensionPostLogin(configurationAttributes, foundUser)
336 print "Google+ Authenticate for step 2. postLoginResult: '%s'" % postLoginResult
338 return postLoginResult