1

我一直在试图做一个特定的操作,一旦我从challenge处理器收到submitAdapterAuthentication,并且我无法做任何操作,因为我的代码甚至没有通过它编译。我在我的角度服务的一种方法中使用submitAdapterAuthentication。该方法是这样的:submitAdapterAuthentication不能正常工作

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; 
    } 

其中CHVAR CH = WL.Client.createChallengeHandler(securityTest);

checkOnline是此功能检查用户是否在线与否:

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; 
    } 

最后,这是“submitLogin”的过程,我在我的SingleStepAuthAdapter.js。 SingleStepAuthAdapter是适配器的名称。

//-- 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"); 
} 

所以我想一个承诺发到我的控制器,以便将用户重定向到另一个页面,但作为挑战处理机甚至没有工作没有被返回的承诺。

顺便说一下,我按照这个教程https://medium.com/@papasimons/worklight-authentication-done-right-with-angularjs-768aa933329c

有谁知道这是怎么回事?

+0

“我的代码甚至没有通过它编译”,你究竟看到了什么症状? – djna 2015-02-24 10:21:34

+0

我的意思是,ch.submitAdapterAuthentication(invocationData,{})方法本身并不通过可选参数。 例如,在我的ch.submitAdapterAuthentication(invocationData,{})方法中,我传递了几个函数作为可选项以查看方法是否成功。所以我在每个可选函数中都有console.log,并且在控制台中没有看到任何东西。 您可以在顶部看到我的登录方法。 – 2015-02-24 11:52:02

回答

1

您对挑战处理程序和我的理解有很大不同。

虽然

ch.submitAdapterAuthentication() 

是在结构上与标准适配器的方法调用我从来没有使用任何回调与之类似。

我从IBM AdapteBasedAuthentication tutorial materials

的基本思想是,你的挑战处理机应该有两个回调方法工作:

isCustomResponse() 
handleChallenge() 

你会看到这些功能,以响应您提交调用。

我建议先看看这些方法。我无法评论你引用的离子型例子,但是我自己使用了角度/离子与认证框架和挑战处理程序。我的出发点是我上面提到的IBM材料。