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材料。