I have a login page in Worklight that sets the active user on the worklight server. This sets a userIdentity. In this userIdentity I save an encrypted string of 'username:password'.

Every time an adapterprocedure is called it tests if a user is logged in. When he is logged in, the procedure is called.

In the back-end I use basic authentication for every REST call. So I need in every call's header ->

    Authorization: Basic  encrypted(username:password)

When I mock this, everything works. Example ->

    var input = {
    method : 'get',
    returnedContentType : 'json',
    path : path,
     headers:{
            Authorization: "Basic "+"dGVzdDp0ZXN0"
        }

I need to set, that everytime a procedure is called, this 'username:password' is the username:password from the userIdentity of the user that is logged in in the worklightserver.

How can I do this?


Authorizing HTTP Adapter in IBM Worklight

Here they use $( username ), but I don't know where the $ sign is referring to.

2

There are 2 best solutions below

0
On BEST ANSWER

I do not think it is the best way, but it works for me...

wheb setting a userIdentity i added a loginString in the user object. this object contains a var credentials that i initiated in the login-adapter.

loginstring = encode.encode_base64(user:pass);

userIdentity = {

    userID:"01",
    displayName: "name",
    credentials:loginstring, 
    teacherId:"212",

}

WL.Server.setActiveUser("AuthRealm", userIdentity);

Then in the adapter you can reach the userIdentity-object:

function get() {

var user = WL.Server.getActiveUser("AuthRealm");
var input = {
    method : 'get',
    returnedContentType : 'json',
    headers:{
        Authorization: "Basic " + user.credentials
    },
    path : 'path',
};

return WL.Server.invokeHttp(input); }

6
On

In your connection policy, all you need is:

        <authentication>
            <basic/>
        </authentication>  

Then for each of your procedures, use:

<procedure name="getFeed" connectAs="endUser"/>

You only need the serverIdentity if you are using connectAs="server"