Tim
Tim

Reputation: 737

WL.Server.getActiveUser returns Null

I need the ability to get a users login userid/password in a java adapter. After reading lots of articles, the best way seems to be to call

WL.Server.getActiveUser from a javascript function that gets called from the java adapter. So, I added a getIdentity function to the http adapter that authenticates our app. I have verified that getActiveUser works in the authentication function that the login pages calls...

When I call the getIdentity function, getActiveUser returns null using The same authentication realm. I have set the realm in the application_descriptor file. Not sure what else I have to do. Any ideas?

function performAuthentication(username, password) {
       WL.Logger.info("In performAuthentication: username = " + username + " password = " + password + "Time = " + new Date(new Date().getTime()).toLocaleString());

        var invocationData = {
            adapter : 'BluePages',  
            procedure : 'authenticate',  
            parameters  : [username, password]
        };
        var invocationResult = WL.Server.invokeProcedure(invocationData);

        var fullName = invocationResult.result.fullName;
       if (invocationResult.result.success == false) {
            return {
                authRequired: true,
                loginPassed: false
            };
        } 
        else {
            userIdentity = {
                userId: username,
                credentials: password,
                displayName: username,
                attributes: {
                    foo: "bar"
                }
        };

        WL.Server.setActiveUser("AuthRealm", null);
        WL.Server.setActiveUser("AuthRealm", userIdentity);
        var activeUser = WL.Server.getActiveUser("AuthRealm");
        WL.Logger.info("activeUser = " + activeUser);

        if(activeUser && activeUser.userId == username){
            WL.Logger.info("Active userId = " + activeUser.userId + " password = "    + activeUser.credentials);
            WL.Logger.info("User has been logged in!");
            return {
                loginPassed: true,
                authRequired: false,
                fullName: fullName,
                result: invocationResult.result
            };
        }
        else {
            WL.Logger.info("Else Clause...");
            if(activeUser != null)
                WL.Server.setActiveUser("AuthRealm", null);
            WL.Server.setActiveUser("AuthRealm", userIdentity);
        }


         return {
            authRequired: false,
            loginPassed: true,
            fullName: fullName
        };
    }

 }


function getIdentity() {
        WL.Logger.info("AuthAdapter: In getIdentity: Time = " + new Date(new Date().getTime()).toLocaleString());

        WL.Logger.info("AuthAdapter: userIdentity = " + userIdentity);

        var activeUser = WL.Server.getActiveUser("AuthRealm");

        WL.Logger.info("AuthAdapter: getIdentity: getActiveUser returned = " + activeUser);

        if (activeUser) {

                WL.Logger.info("AuthAdapter: getIdentity userId = " + activeUser.userId);

                return {
                    userId: activeUser.userId,
                    credentials: activeUser.credentials,
        };
     }
 }

Upvotes: 1

Views: 393

Answers (1)

Yuval Zymerman
Yuval Zymerman

Reputation: 151

There could be 2 reasons to get null when using WL.Server.getActiveUser:

1)If no realm is defined on the adapter, the method returns null (active user is unknown)

2)If a realm is defined on the adapter:

If there is no strong identity associated with the user (the user was authenticated in this session or in a previous session), the method returns null.

In your case you said the realm is exist so I suggest to try #2

You can find more information here: https://www.ibm.com/support/knowledgecenter/SSZH4A_6.0.0/com.ibm.worklight.help.doc/apiref/r_method_wl_server_getactiveuser.html

Upvotes: 5

Related Questions