2016-10-19 49 views
4

我試圖通過護照http包的幫助來保護我的風帆js rest api,但此刻我無法弄清楚錯誤在我的代碼中。 我用這個repo和這個tutorial來了解這應該如何工作。我的問題是,我的代碼總是返回一個401. 我真的不知道在哪裏尋找錯誤。如果您需要更多關於我的代碼的信息,請點評。 布魯諾Sails JS護照http 401

編輯: 我找到了問題的根源(隨着@Viktor的幫助)。我只是不太瞭解HTTP-Basic身份驗證如何工作。現在的問題是我如何發送我的認證和我的數據?如果我只使用auth(...)測試路線,它們就可以工作......但是如何添加數據?或者我必須先認證我並在第二次請求中發送數據?

passport.js

var passport = require('passport'); 
var BasicStrategy = require('passport-http').BasicStrategy; 
var bcrypt = require('bcrypt'); 

passport.serializeUser(function(user, done) { 
    done(null, user.id); 
}); 

passport.deserializeUser(function(id, done) { 
    User.findOne({ 
    id: id 
    }, function(err, user) { 
    done(err, user); 
    }); 
}); 

passport.use('user-authentication', new BasicStrategy(
    function(mail, password, done) { 
    sails.log.error("hallo"); 
    User.findOne({ 
     mail: mail 
    }, function(err, user) { 
     if (err) { 
     return done(err); 
     } 

     if (!user) { 
     return done(null, false, { 
      message: 'Incorrect email.' 
     }); 
     } 

     // Make sure the password is correct 
     bcrypt.compare(password, user.password, function(err, isMatch) { 
     if (err) { 
      return done(err); 
     } 

     // Password did not match 
     if (!isMatch) { 
      return done(null, false, { 
      message: 'Invalid Password' 
      }); 
     } 

     // Success 
     return done(null, user); 
     }); 
    }); 
    } 
)); 

isAuthenticated.js

var passport = require("passport"); 


module.exports = function (req, res, ok) { 
    passport.authenticate("user-authentication", { 
     session: false 
    }, function (err, user, info) { 
     if (err || !user) { 
      res.set("WWW-Authenticate", "Basic realm=\"Restricted\""); 

      return res.send("You are not permitted to perform this action", 401); 
     } 

     req.session.user = user; 
     return ok(null, user); 

    })(req, res, ok); 
}; 

policies.js

module.exports.policies = { 
    '*': true, 

    'UserController': { 
    update: 'isAuthenticated' 
    } 
} 

個UserController.test.js

var request = require('supertest'); 
var async = require('async'); 

describe('UserController', function() { 

    describe('#new()', function() { 
    it('...', function (done) { 
     request(sails.hooks.http.app) 
     .post('/user/new') 
     .send({ own_number: '654122', password: 'test', mail: '[email protected]', device_id: '1234', numbers: [1234567] }) 
     .expect(200) 
     .end(done); 
    }); 
    }); 

    describe('#update()', function(){ 
    it('...', function (done) { 
     async.series([ 

     function(callback){ 
      request(sails.hooks.http.app) 
      .post('/contact/update') 
      .send({ number: 1234, mail: "[email protected]", password: "test" }) 
      .expect(200) 
      .end(callback); 
     }, 

     function(callback){ 
      request(sails.hooks.http.app) 
      .post('/user/update') 
      .send({ numbers: [1234], mail: "[email protected]", password: "test" }) 
      .expect(200) 
      .end(callback); 
     } 
     ], done); 
    }); 
    }); 

}); 
+0

嘗試使用「基本」,而不是用戶認證 –

+0

好,不過我想我可以說出不同的策略 – Bruno

+0

https://開頭github上。com/jaredhanson /護照-http我希望這會幫助你 –

回答

0

對我的作品 - 我能,以及認證的訪問和管理用戶數據,一旦有數據庫的有效用戶。對於空的用戶數據庫,您當然會一直得到401,因爲這些策略不允許您創建第一個用戶進行身份驗證。暫時禁用config/policies.js中的UserController政策可讓您有機會創建第一個用戶。

假設您在數據庫中至少有一個有效用戶,讓我們縮小這個問題。你在isAuthenticated.js的日誌記錄erruser中得到了什麼輸出?如果獲得nullfalsepassport.js的不同步驟會發生什麼 - 您是否能夠通過數據庫中的電子郵件地址找到用戶,並且密碼是否匹配?

您是否有自定義路線和控制器操作,或者您是否使用藍圖?

編輯:您更新的測試看起來像這樣用HTTP基本身份驗證:

describe('#update()', function(){ 
    it('...', function (done) { 
    async.series([ 

     function(callback){ 
     request(sails.hooks.http.app) 
     .post('/contact/update') 
     .auth('[email protected]', 'test') 
     .send({ number: 1234, mail: "[email protected]", password: "test" }) 
     .expect(200) 
     .end(callback); 
     }, 

     function(callback){ 
     request(sails.hooks.http.app) 
     .post('/user/update') 
     .auth('[email protected]', 'test') 
     .send({ numbers: [1234], mail: "[email protected]", password: "test" }) 
     .expect(200) 
     .end(callback); 
     } 
    ], done); 
    }); 
}); 
+0

err爲空,並且用戶爲false(如你所建議的)。信息說'基本領域=「用戶」'。最讓我困惑的是每當我嘗試在'passport.use('user-authentication',...'函數內部記錄一些東西時,什麼都不會發生,我使用自定義路由,並且會發布我的測試函數。 ..可能我在那裏有一個錯誤... – Bruno

+0

@Bruno你有一個用戶在數據庫中登錄爲? – Viktor

+0

是的,我確實有,我改變了政策,所以總是可以創建新的用戶 – Bruno

0

從它表明你需要用戶名和密碼的文檔here。所以看着你的代碼,你有郵件代替userid,這就是爲什麼你得到401或至少可以開始尋找的地方。如果你需要驗證這一點,你可以看看passport-http basic strategy here

passport.use(new BasicStrategy( function(userid, password, done) { User.findOne({ mail: userid, password: password }, function (err, user) { done(err, user); }); } ));

+0

不會改變任何東西... – Bruno