Overview

When trying to access a protected resource, the server (the security check) will send back to the client a list containing one or more challenges for the client to handle.
This list is received as a JSON object, listing the security check name with an optional JSON of additional data:

The client should then register a challenge handler for each security check.
The challenge handler defines the client-side behavior that is specific to the security check.

Creating the challenge handler

A challenge handler handles challenges sent by the MobileFirst Server, such as displaying a login screen, collecting credentials, and submitting them back to the security check.

In this example, the security check is PinCodeAttempts which was defined in Implementing the CredentialsValidationSecurityCheck. The challenge sent by this security check contains the number of remaining attempts to log in (remainingAttempts), and an optional errorMsg.

Use the WL.Client.createSecurityCheckChallengeHandler() API method to create and register a challenge Handler:

Handling the challenge

The minimum requirement from the createSecurityCheckChallengeHandler protocol is to implement the handleChallenge() method, which is responsible for asking the user to provide the credentials. The handleChallenge method receives the challenge as a JSON Object.

In this example, an alert prompts the user to enter the PIN code:

PinCodeChallengeHandler.handleChallenge=function(challenge){varmsg="";// Create the title string for the promptif(challenge.errorMsg!=null){msg=challenge.errorMsg+"\n";}else{msg="This data requires a PIN code.\n";}msg+="Remaining attempts: "+challenge.remainingAttempts;// Display a prompt for user to enter the pin code varpinCode=prompt(msg,"");if(pinCode){// calling submitChallengeAnswer with the entered valuePinCodeChallengeHandler.submitChallengeAnswer({"pin":pinCode});}else{// calling cancel in case user pressed the cancel buttonPinCodeChallengeHandler.cancel();}};

If the credentials are incorrect, you can expect the framework to call handleChallenge again.

Submitting the challenge’s answer

After the credentials have been collected from the UI, use createSecurityCheckChallengeHandler’s submitChallengeAnswer() to send an answer back to the security check. In this example, PinCodeAttempts expects a property called pin containing the submitted PIN code:

PinCodeChallengeHandler.submitChallengeAnswer({"pin":pinCode});

Cancelling the challenge

In some cases, such as clicking a Cancel button in the UI, you want to tell the framework to discard this challenge completely.
To achieve this, call:

PinCodeChallengeHandler.cancel();

Handling failures

Some scenarios might trigger a failure (such as maximum attempts reached). To handle these, implement createSecurityCheckChallengeHandler’s handleFailure().
The structure of the JSON object passed as a parameter greatly depends on the nature of the failure.

Handling successes

In general, successes are automatically processed by the framework to allow the rest of the application to continue.

Optionally, you can also choose to do something before the framework closes the challenge handler flow, by implementing createSecurityCheckChallengeHandler’s handleSuccess(). Here again, the content and structure of the success JSON object depends on what the security check sends.

In the PinCodeAttemptsCordova sample application, the success does not contain any additional data.

Registering the challenge handler

For the challenge handler to listen for the right challenges, you must tell the framework to associate the challenge handler with a specific security check name.
To do so, create the challenge handler with the security check as follows: