Search code examples
ibm-mobilefirstmobilefirst-adapters

MobileFirst 7.0 combining Authentication and Java SQL Adapter fails


Im trying to combine Java UserAdapter and Adapter-based authentication

I want to get the users list after authenticated, but it fails

On server-side, Adapter-based authentication, AuthenticationAdapter-impl.js

function onAuthRequired(headers, errorMessage) {
  errorMessage = errorMessage ? errorMessage : null;

  return {
    authRequired: true,
    errorMessage: errorMessage
  };
}

function submitAuthentication(username, password){

  if (username==="admin" && password === "admin"){

    var userIdentity = {
      userId: username,
      displayName: username, 
      attributes: {
        foo: "bar"
      }
    };

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

    return { 
      authRequired: false 
    };
  }

  return onAuthRequired(null, "Invalid login credentials");
}


function listUsers() {
  var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/", WLResourceRequest.GET);
  return resourceRequest.send();
}

function onLogout() {
  WL.Server.setActiveUser("AdapterAuthRealm", null);
  WL.Logger.debug("Logged out");
}

And on client-side,

function listUsers(){
  busyIndicator.show();
  var invocationData = {
    adapter : "AuthenticationAdapter",
    procedure: "listUsers",
    parameters: []
  };

  WL.Client.invokeProcedure(invocationData, {
    onSuccess: listUsersSuccess, 
    onFailure: listUsersFailure
  });
}

function listUsersSuccess(result){
  WL.Logger.debug("Feed retrieve success");
  busyIndicator.hide();
  WL.Logger.debug(JSON.stringify(result));
  if (result.responseJSON.length > 0){ 
    displayUsers(result.responseJSON);
  } else {
    listUsersFailure();
  }
}

function listUsersFailure(result){
  WL.Logger.error("Feed retrieve failure");
  busyIndicator.hide();
  WL.SimpleDialog.show("Banking Application", "Service not available. Try again later.", [
    {
      text : 'Reload',
      handler : WL.Client.reloadApp 
    },
    {
      text: 'Close',
      handler : function() {}
    }
  ]);
}

It returns onFailure response


Solution

  • WLResourceRequest is a client side API so you CAN NOT use it on an adapter since the adapter runs on the server.

    You should update your listUsers function (client side) as follows:

    function listUsers(){
      busyIndicator.show();
    
      var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/", WLResourceRequest.GET);
    
      resourceRequest.send().then(listUsersSuccess).fail(listUsersFailure);
    }
    

    Update

    You can protect your Java Adapter methods by using the @OAuthSecurity annotation.

    UserAdapter.java

    @GET
    @Path("/protectePath")
    @OAuthSecurity(scope="YourRealm")
    public String myProtectedMethod() {
        // your code here
        return "your-response";
    }