By: Burcin Gunay named 20 Apr 2018 at 5:56 a.m. CDT

3 Responses
Burcin Gunay gravatar
I have developed a custom authentication script and use it in my test service provider to login using gluu based server with 2.4.4 In def prepareForStep(self, configurationAttributes, requestParameters, step), I generate a capcha image and value and put it in the session with the following code: ``` def prepareForStep(self, configurationAttributes, requestParameters, step): context = Contexts.getEventContext() sessionStateService = SessionStateService.instance() sessionState = sessionStateService.getSessionState() print "SessionState id: %s" % sessionState.getId() print "AkisIDExternalAuthenticator. Prepare for Step initialization" if (step == 1): try: nonceB64Encoded = self.produceNonce() context.set("akisNonce", nonceB64Encoded) captchaBase64Encoded, captchaImageB64Encoded = self.produceCaptchaImage() context.set("akisCaptchaImage", captchaImageB64Encoded) context.set("captcha", captchaBase64Encoded) sessionAttributes = sessionState.getSessionAttributes() sessionAttributes.put("captcha", captchaBase64Encoded) sessionAttributes.put("akisNonce", nonceB64Encoded) sessionState.setSessionAttributes(sessionAttributes) sessionStateService.updateSessionState(sessionState) print "AkisIDExternalAuthenticator. Nonce is set %s " %sessionAttributes.get("akisNonce") print "AkisIDExternalAuthenticator. Capcha is set %s " %sessionAttributes.get("captcha") except Exception, ex: print "Debugging inlet #2" print "Unexpected error:", ex print "AkisIDExternalAuthenticator. Prepare for Step 1" return True else: return False ``` When I print on the log file, I can see that the parameters can be retrieved. But in the authenticate method, I can not retrieve the same parameters from the session attributes, it gets null!! ``` def authenticate(self, configurationAttributes, requestParameters, step): context = Contexts.getEventContext() sessionStateService = SessionStateService.instance() sessionState = sessionStateService.getSessionState() sessionAttributes = sessionState.getSessionAttributes() print "AkisIDExternalAuthenticator. Authenticate. Session State ID: %s" % sessionState.getId() print "AkisIDExternalAuthenticator. Session Attributes %s" % sessionAttributes print "AkisIDExternalAuthenticator. capcha %s" % sessionAttributes.get("captcha") print "AkisIDExternalAuthenticator. Nonce %s" % sessionAttributes.get("akisNonce") context.set("keyOpErrorMsg", "") if (step == 1): print "AkisIDExternalAuthenticator. Authenticate for step 1" credentials = Identity.instance().getCredentials() user_name = credentials.getUsername() user_password = credentials.getPassword() print "AkisIDExternalAuthenticator Authenticate username:%s password:%s" % (user_name, user_password) ''' Getting captcha from request ''' captchaAnswerArray = requestParameters.get("captchaAnswer") if ArrayHelper.isEmpty(captchaAnswerArray): print "AkisIDExternalAuthenticator. captchaAnswer is empty" context.set("keyOpErrorMsg", "Captcha degeri bos olmamalidir !!!") return False captchaAnswer = captchaAnswerArray[0] print "AkisIDExternalAuthenticator Authenticate Captcha Answer: %s" % captchaAnswer _''' Getting captcha from the session ''' captchaB64Encoded = sessionAttributes.get("captcha")_ if captchaB64Encoded is None: print "captchaB64Encoded is not available" context.set("keyOpErrorMsg", "Oturumda bir captcha bilgisi bulunamadi !!!") return False else: captchaSerialized = Base64.decode(captchaB64Encoded) captcha = SerializationUtils.deserialize(captchaSerialized) print "AkisIDExternalAuthenticator Authenticate Captcha Answer Result: %r" % captcha.isCorrect(captchaAnswer) if not captcha.isCorrect(captchaAnswer): context.set("keyOpErrorMsg", "Girilen captcha degeri yanlis !!!") return False ```

By Yuriy Movchan staff 20 Apr 2018 at 3:43 p.m. CDT

Yuriy Movchan gravatar
Can you try to add method `getExtraParametersForStep` and use next patern? ``` def authenticate(self, configurationAttributes, requestParameters, step): context = Contexts.getEventContext() ... context.set("akisCaptchaImage", captchaImageB64Encoded) context.set("captcha", captchaBase64Encoded) ... def getExtraParametersForStep(self, configurationAttributes, step): if (step == 2): return Arrays.asList("akisCaptchaImage", "captcha") return None ```

By Yuriy Movchan staff 20 Apr 2018 at 3:45 p.m. CDT

Yuriy Movchan gravatar
After that you should be able to get parameters from authentication session: ``` sessionAttributes = context.get("sessionAttributes") if (sessionAttributes == None) or not sessionAttributes.containsKey("akisCaptchaImage"): print "Authenticate for step 2. akisCaptchaImageis empty" return False ```

By Burcin Gunay named 24 Apr 2018 at 6:07 a.m. CDT

Burcin Gunay gravatar
Hi, Before the answer, I have tried to get session attributes with sessionAttributes = context.get("sessionAttributes") and it all worked well! Your answer also is well appreciated!