2014-02-13 186 views
0

我創建通過在Azure移動服務API自定義驗證,撿起基於這樣的回答:自定義身份驗證Azure的移動服務採用API

Registering and login users in Azure Mobile Services

然後用一個鏈接的代碼來創建連接身份驗證令牌。

但我在調用API時出現「內部服務器錯誤」。在發生錯誤的位置:「...... results.length ......」

var crypto = require('crypto'); 
    var iterations = 1000; 
    var bytes = 32; 
    var aud = "Custom"; 
    var masterKey = "wkeHEoWUaPJSHsSOcWgmVLOZbIpeeg92"; 
    var _request; 
    var _response; 

    exports.post = function(request, response) { 
     var user = request.body.userName; 
     var pass = request.body.password; 
     _request = request; 
     _response = response 
     validateUserNamePassword(user, pass, function(error, userId, token) { 
      if (error) { 
       response.send(401, { error: "Unauthorized" }); 
      } else { 
       response.send(200, { user: userId, token: token }); 
      } 
     }); 
    } 

    function validateUserNamePassword(user, pass, funcao){ 

     var accounts = _request.service.tables.getTable('account'); 

     accounts 
      .where({ userid : user }) 
      .read({ 
       success: function(results) 
       { 
        if (results.length === 0) 
        { 
         _response.send(401, { error: "Unauthorized1" }); 
         console.log("Incorrect username or password"); 
         _request.respond(401, "Incorrect username or password"); 
        } 
        else 

         _response.send(401, { error: "Unauthorized2" }); 
         var account = results[0]; 
         hash(item.password, account.salt, function(err, h) { 
          var incoming = h; 
          if (slowEquals(incoming, account.password)) { 
           var expiry = new Date().setUTCDate(new Date().getUTCDate() + 30); 
           var userId = aud + ":" + account.id; 
           _request.respond(200, { 
            userId: userId, 
            token: zumoJwt(expiry, aud, userId, masterKey) 
           }); 
          } 
          else { 
           _request.respond(401, "Incorrect username or password"); 
          } 
         }); 
        } 
       } 
      }); 
    } 



    function hash(text, salt, callback) { 
     crypto.pbkdf2(text, salt, iterations, bytes, function(err, derivedKey){ 
      if (err) { callback(err); } 
      else { 
       var h = new Buffer(derivedKey).toString('base64'); 
       callback(null, h); 
      } 
     }); 
    } 

    function slowEquals(a, b) { 
     var diff = a.length^b.length; 
     for (var i = 0; i < a.length && i < b.length; i++) { 
      diff |= (a[i]^b[i]); 
     } 
     return diff === 0; 
    } 

    function zumoJwt(expiryDate, aud, userId, masterKey) { 

     var crypto = require('crypto'); 

     function base64(input) { 
      return new Buffer(input, 'utf8').toString('base64'); 
     } 

     function urlFriendly(b64) { 
      return b64.replace(/\+/g, '-').replace(/\//g, '_').replace(new RegExp("=", "g"), ''); 
     } 

     function signature(input) { 
      var key = crypto.createHash('sha256').update(masterKey + "JWTSig").digest('binary'); 
      var str = crypto.createHmac('sha256', key).update(input).digest('base64'); 
      return urlFriendly(str); 
     } 

     var s1 = '{"alg":"HS256","typ":"JWT","kid":0}'; 
     var j2 = { 
      "exp":expiryDate.valueOf()/1000, 
      "iss":"urn:microsoft:windows-azure:zumo", 
      "ver":1, 
      "aud":aud, 
      "uid":userId 
     }; 
     var s2 = JSON.stringify(j2); 
     var b1 = urlFriendly(base64(s1)); 
     var b2 = urlFriendly(base64(s2)); 
     var b3 = signature(b1 + "." + b2); 
     return [b1,b2,b3].join("."); 
    } 

我調用這樣的:

 try 
     { 
      var loginInput = new JObject(); 
      loginInput.Add("userName", "breno"); 
      loginInput.Add("password", "test"); 
      var loginResult = await LoginAuthenticationService.InvokeApiAsync("login", loginInput); 
      LoginAuthenticationService.CurrentUser = new MobileServiceUser((string)loginResult["user"]); 
      LoginAuthenticationService.CurrentUser.MobileServiceAuthenticationToken = (string)loginResult["token"]; 
     } 
     catch (MobileServiceInvalidOperationException e) 
     { 
      var exception = e; 
     } 
+0

如果您在「console.log(results);」中放入日誌,您會在日誌中看到什麼?在檢查長度之前打電話嗎?你確定request.body.userName包含用戶名嗎? – Chris

+0

@Chris我更新帖子與我如何調用,我做錯了什麼?感謝您的幫助! –

+0

聽起來好像這個請求是將它傳遞給你的自定義API,因爲你得到了500.如果你在post方法的開頭做了一個console.log(request.body),你看到了什麼? – Chris

回答

0

如果您收到此錯誤「無法加載腳本文件'login.js':SyntaxError:Unexpected token}「,如註釋中所示,您的腳本存在語法問題。你需要仔細閱讀你的腳本並找出問題出在哪裏。

相關問題