2013-12-10 5 views
2

여권과 함께 여러 가지 전략을 사용하고 있습니다 (지역 및 무기명 전략). 지역 전략으로 로그인하십시오. 로그인 후 토큰을 생성하고 토큰은 redis에 저장됩니다. 초기 로그인 후에 토큰이 redis에서 발견되는 한 세션없이 bearer auth를 사용하고자합니다. 올바른 토큰을 보내면 redis를 쿼리하고 사용자 데이터를 얻을 수 있지만 예상 한 200 개의 상태 코드 대신 403 개의 응답을 보냅니다. 토큰은 레디 스에서 발견되지 않는 경우, 다음과 같은 오류와 충돌을 항해 : 여기 Passport-http-bearer 인증이 작동하지 않는 Sails.js

/workspace/rs-api-sails/node_modules/redis/index.js:587 
      throw err; 
       ^
Error: Can't set headers after they are sent. 
    at ServerResponse.OutgoingMessage.setHeader (http.js:691:11) 
    at ServerResponse.res.setHeader (/workspace/rs-api-sails/node_modules/sails/node_modules/express/node_modules/connect/lib/patch.js:59:22) 
    at allFailed (/workspace/rs-api-sails/node_modules/passport/lib/passport/middleware/authenticate.js:153:13) 
    at attempt (/workspace/rs-api-sails/node_modules/passport/lib/passport/middleware/authenticate.js:232:28) 
    at Context.delegate.fail (/workspace/rs-api-sails/node_modules/passport/lib/passport/middleware/authenticate.js:227:9) 
    at Context.actions.fail (/workspace/rs-api-sails/node_modules/passport/lib/passport/context/http/actions.js:35:22) 
    at verified (/workspace/rs-api-sails/node_modules/passport-http-bearer/lib/strategy.js:125:19) 
    at /workspace/rs-api-sails/config/bootstrap.js:40:18 
    at try_callback (/workspace/rs-api-sails/node_modules/redis/index.js:580:9) 
    at RedisClient.return_reply (/workspace/rs-api-sails/node_modules/redis/index.js:670:13) 
10 Dec 13:25:15 - [nodemon] app crashed - waiting for file changes before starting... 

가 bootstrap.js에서 베어러 인증을위한 코드입니다 :

passport.use(new BearerStrategy(
    function(token, done) { 
    var redis = require("redis"), 
    client = redis.createClient(null, null, {detect_buffers: true}); 

    client.get(token, function (err, reply) { 
     if (reply === null) { 
     // if token is not a key in redis, node throws the headers already sent error 
     return done(null, false); 
     } else { 
     User.findOne({ id: reply.toString() }).done(function(err, user) { 
      sails.log(user); 

      // here we get the user data from waterline but node still sends a 403 
      return done(null, user); 
     }); 
     } 
    }); 
    } 
)); 

이 코드는 정책/isAuthenticated.js에 :

module.exports = function(req, res, next) { 
    var passport = require('passport');  

    passport.authenticate('bearer', { session: false })(req, res, next); 

    // User is allowed, proceed to the next policy, 
    // or if this is the last policy, the controller 
    if (req.isAuthenticated()) { 
    return next(); 
    } 

    // User is not allowed 
    // (default res.forbidden() behavior can be overridden in `config/403.js`) 
    return res.forbidden('You are not permitted to perform this action.'); 
}; 

저는 새로운 노드입니다.

답변

1

업데이트 :

var passport = require('passport'); 

module.exports = function(req, res, next) { 

    passport.authenticate('bearer', { session: false }, function(err, user, info) { 

    if (req.isAuthenticated()) { 
     // user is allowed through local strategy 
     return next(); 
    } 

    if (err) { 
     return res.send(403, { error: 'Error: ' + info });   
    } 

    if (!user) { 
     return res.send(403, { error: 'Invalid token' }); 
    } 

    if (user) { 
     sails.log(user); 
     return next(); 
    } 

    // (default res.forbidden() behavior can be overridden in `config/403.js`) 
    return res.forbidden('You are not permitted to perform this action.');  

    })(req, res, next); 

}; 
: 정책/isAuthenticated.js에 몇 가지 변경 후 지금 작동하는 것 같군