在MobileFirst v.6.3中使用ChallengeHandler的submitAdapterAuthentication()方法出现问题

Mag*_*nus 0 callback promise ibm-mobilefirst

我们在IBM MobileFirst v.6.3中遇到了ChallengeHandler的submitAdapterAuthentication()方法的问题.

我们将回调函数分配给options对象中的属性'onSuccess'和'onFailure' .

然后,我们提供options对象以submitAdapterAuthentication(invocationData,options)并执行它.

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

//////////////////

function login (user, pass) {

  tempUser = {username: user, password: pass};
  userObj.user = user;
  var auth = "Basic " + window.btoa(user + ":" + pass);

    var invocationData = {
     parameters: [auth, user],
     adapter: "SingleStepAuthAdapter",
     procedure: "submitLogin"
    };

    var options = {
      onSuccess: iWon,
      onFailure: iLost,
      invocationContext: {invocationData: invocationData},
      timeout: 10000
    };

    ch.submitAdapterAuthentication(invocationData, options);

});

function iWon(response) {
    WL.Logger.debug('Login success! Response: ' + JSON.stringify(response));

   //update user info, as somehow isUserAuthenticated return false without it
    WL.Client.updateUserInfo(function(response) {
        WL.Logger.debug('Updated User Info success! Response: ' + JSON.stringify(response));
    });
}

function iLost(response) {
    WL.Logger.debug('ERROR. Login failed! Response: ' + JSON.stringify(response));
}
Run Code Online (Sandbox Code Playgroud)

执行submitAdapterAuthentication(invocationData,options)后,不会调用onSuccess(iWon)或onFailure(iLost).

我们如何知道身份验证是否成功?

我们应该寻找和使用哪些选项,事件,回调或承诺?

我们也在这里发布了这个问题: submitAdapterAuthentication无效

Yoe*_*nez 5

您缺少函数的定义

ch.isCustomResponse = function(response){...}
ch.handleChallenge = function(response){...}
Run Code Online (Sandbox Code Playgroud)

你的代码看起来应该更像这样

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

ch.isCustomResponse = function(response) {
  if (!response||!response.responseJSON||response.responseText === null) {
       return false;
  }
     if (typeof(response.responseJSON.authRequired) !== 'undefined'){
         return true;
     } else {
         return false;
     }
};


ch.handleChallenge = function(response){
  var authRequired = response.responseJSON.authRequired;

    if (authRequired == true){
        // handle the case when authentication is needed, i.e., show login form etc.


        if (response.responseJSON.errorMessage) {
      // authentication failed, show a message to the user indicating what went wrong

      // call the login failed function or move it's contents here
      iLost(response);
    }

    } else if (authRequired == false){
          // no authentication is needed 
          ch.submitSuccess();

      // call the login success function or move it's contents here
      iWon(response);
    }
};



//////////////////

function login (user, pass) {

  tempUser = {username: user, password: pass};
  userObj.user = user;

  // is the first parameter expected by submitLogin the username or the
  // Basic Authentication encoded string ???
  var auth = "Basic " + window.btoa(user + ":" + pass);

  var invocationData = {
     parameters: [auth, user],
     adapter: "SingleStepAuthAdapter",
     procedure: "submitLogin"
  };

  ch.submitAdapterAuthentication(invocationData, {});

});


function iWon(response) {
    WL.Logger.debug('Login success! Response: ' + JSON.stringify(response));

   //update user info, as somehow isUserAuthenticated return false without it
    WL.Client.updateUserInfo(function(response) {
        WL.Logger.debug('Updated User Info success! Response: ' + JSON.stringify(response));
    });
}

function iLost(response) {
    WL.Logger.debug('ERROR. Login failed! Response: ' + JSON.stringify(response));
}
Run Code Online (Sandbox Code Playgroud)

有关基于适配器的身份验证的详细信息,请访问http://www-01.ibm.com/support/knowledgecenter/SSHS8R_6.3.0/com.ibm.worklight.dev.doc/devref/t_adapter_based_authenticator.html?lang=en

您还应该检查基于适配器的混合应用程序身份验证的入门模块https://developer.ibm.com/mobilefirstplatform/documentation/getting-started-6-3/authentication-security/adapter-based-authentication/adapter-基于认证杂交的应用程序/