Question

I'm using the WorkLightAuthenticator to validate User login. But I need to return some parameters like userName, organozationID, among others. What would be the correct way to return this data? I believe that would be the method createIdentity as exemple down. And to retrieve the data on the client?

/*Worklight Adapter*/    
public class LoginModule implements WorkLightAuthLoginModule {

private String USERNAME;
private String PASSWORD;

public void init(Map<String, String> options) throws MissingConfigurationOptionException {
}

@SuppressWarnings("unchecked")
public boolean login(Map<String, Object> authenticationData) {
    USERNAME = (String) authenticationData.get("username");
    PASSWORD = (String) authenticationData.get("password");

    InvocationResult invocationResult = callLoginAdapter(USERNAME, PASSWORD);
    JSONObject jsonObject = invocationResult.toJSON();

    HashMap<String, String> result = (HashMap<String, String>) jsonObject.get("result");

    if(result != null){
        return true;
    } else {
        HashMap<String, String> fault = (HashMap<String, String>) jsonObject.get("Fault");
        String detail = fault.get("Detail");                        
        throw new RuntimeException(detail);
    }
}



public UserIdentity createIdentity(String loginModule) {
    HashMap<String, Object> customAttributes = new HashMap<String, Object>();
    customAttributes.put("AuthenticationDate", new Date());
    customAttributes.put("userName", userExample);
    customAttributes.put("organizationID", 1);

    UserIdentity identity = new UserIdentity(loginModule, USERNAME, null, null, customAttributes, PASSWORD);
    return identity;
}

public void logout() {
    USERNAME = null;
    PASSWORD = null;
}

public void abort() {
    USERNAME = null;
    PASSWORD = null;
}

@Override
public LoginModule clone() throws CloneNotSupportedException {
    return (LoginModule) super.clone();
}

public InvocationResult callLoginAdapter(String user, String password){
    DataAccessService service = WorklightBundles.getInstance().getDataAccessService();
    String parameters = "['"+user+"','"+password+"']";
    ProcedureQName procedureQName = new ProcedureQName("LoginAdapter", "getUserByLogin");
    return service.invokeProcedure(procedureQName, parameters);     
}
}

And AuthenticatorRealmChallengeHandler in client-side

/*AuthenticatorRealmChallengeHandler.js*/
var customAuthenticatorRealmChallengeHandler = WL.Client.createChallengeHandler("AuthenticatorRealm");

customAuthenticatorRealmChallengeHandler.isCustomResponse = function(response) {
console.log("**********************");
console.log(response.responseJSON);

if (!response || !response.responseJSON) {
    return false;
}

if (response.responseJSON.authStatus) 
    return true;
else 
    return false;
};

customAuthenticatorRealmChallengeHandler.handleChallenge = function(response){
var authStatus = response.responseJSON.authStatus;

if (authStatus == "required"){      
    if (response.responseJSON.errorMessage){
        currentPage.loginFail(response.responseJSON.errorMessage);
    }
} else if (authStatus == "complete"){       
    currentPage.loadMaster();
    customAuthenticatorRealmChallengeHandler.submitSuccess();

}
};

customAuthenticatorRealmChallengeHandler.submitLoginFormCallback = function(response) {
var isLoginFormResponse = customAuthenticatorRealmChallengeHandler.isCustomResponse(response);
if (isLoginFormResponse){
    customAuthenticatorRealmChallengeHandler.handleChallenge(response);
} 
};

$('#loginButton').bind('click', function () {
var reqURL = '/auth_request_url';
var options = {};
options.parameters = {
    username : $('#userTextField').val(),
    password : $('#passwordTextField').val()
};
options.headers = {};
customAuthenticatorRealmChallengeHandler.submitLoginForm(reqURL, options, customAuthenticatorRealmChallengeHandler.submitLoginFormCallback);
});
Was it helpful?

Solution

var attributes = WL.Client.getUserInfo("AuthenticatorRealm", "attributes");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top