submitAdapterAuthentication not working

2019-03-02 04:55发布

问题:

I have been trying to do a specific operation once I receive the submitAdapterAuthentication from the challenge handler and I could not do any operation because my code it does not even compile through it. I am using the submitAdapterAuthentication in one method of my angular service. The method looks like this:

login: function (user, pass) {
      //promise
      var deferred = $q.defer();
      //tempuser
      tempUser = {username: user, password: pass};
      userObj.user = user;
      checkOnline().then(function (onl) {
        if (onl) { //online
          console.log("attempting online login");
          var auth = "Basic " + window.btoa(user + ":" + pass);
          var invocationData = {
            parameters: [auth, user],
            adapter: "SingleStepAuthAdapter",
            procedure: "submitLogin"
          };
          ch.submitAdapterAuthentication(invocationData, {
            onFailure: function (error) {
              console.log("ERROR ON FAIL: ", error);
            },
            onConnectionFailure: function (error) {
              console.log("BAD CONNECTION - OMAR", error);
            },
            timeout: 10000,
            fromChallengeRequest: true,
            onSuccess: function () {
              console.log("-> submitAdapterAuthentication onSuccess!");
              //update user info, as somehow isUserAuthenticated return false without it
              WL.Client.updateUserInfo({
                onSuccess: function () {
                  //return promise
                  deferred.resolve(true);
                }
              });
            }
          });
        } else { //offline
          console.log("attempting offline login");
          deferred.resolve(offlineLogin());
        }
        uiService.hideBusyIndicator();
      });
      uiService.hideBusyIndicator();
      return deferred.promise;
    }

where ch is var ch = WL.Client.createChallengeHandler(securityTest);

and checkOnline is this function that checks whether the user is online or not:

function checkOnline() {
    var deferred = $q.defer();
    WL.Client.connect({
      onSuccess: function () {
        console.log("** User is online!");
        deferred.resolve(true);
      },
      onFailure: function () {
        console.log("** User is offline!");
        deferred.resolve(false);
      },
      timeout: 1000
    });
    return deferred.promise;
  }

Finally this is the "submitLogin" procedure that I have in my SingleStepAuthAdapter.js. SingleStepAuthAdapter is the name of the adapter.

//-- exposed methods --//
function submitLogin(auth, username){

  WL.Server.setActiveUser("SingleStepAuthAdapter", null);

  var input = {
    method  : 'get',
    headers: {Authorization: auth},
    path : "/",
    returnedContentType : 'plain'
  };

  var response = "No response";
  response = WL.Server.invokeHttp(input);

  WL.Logger.info('Response: ' + response.isSuccessful);
  WL.Logger.info('response.responseHeader: ' + response.responseHeader);
  WL.Logger.info('response.statusCode: ' + response.statusCode);

  if (response.isSuccessful === true && (response.statusCode === 200)){

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

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

    return {
      authRequired: false
    };

  }

  WL.Logger.error('Auth unsuccessful');

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

So I am trying to send a promise to my controller in order to redirect the user to another page but the promise is not being returned as the challenge handler is not even working.

And by the way, I have followed this tutorial: https://medium.com/@papasimons/worklight-authentication-done-right-with-angularjs-768aa933329c

Does anyone know what this is happening?

回答1:

Your understanding of the Challenge Handler and mine are considerably different.

Although the

 ch.submitAdapterAuthentication()

is similar in structure to the standard adapter invocation methods I have never used any callbacks with it.

I work from the IBM AdapteBasedAuthentication tutorial materials

The basic idea is that your challenge handler should have two callback methods:

isCustomResponse()
handleChallenge()

You will see these functions invoked in response to your submission.

I suggest that start by looking at those methods. I can't comment on the ionic example you reference, but I have myself used angular/ionic with the authentication framework and challenge handlers. My starting point was the IBM material I reference above.