如何debugging护照

我不能为了我的生活似乎弄清楚如何debugging护照战略。 我只是在概念上做了一些可怕的错误? 我一直在这里b了我大概10个小时,还没有接近一点。

这是我第一次使用护照。 在这个特定的场景中,我正在使用passport-jwt。 我知道对象键是不正确的,但我只是想通过使用console.log()跟踪通过服务器的path,所以我明白事情是如何工作的。 我甚至没有达到passport.use( new JwtStrategy(..))

我遗漏了不必要的代码。 连接到我的mongodb是好的,我的mongoose模式是好的。

我正在testing使用testing路线server.get('/fakelogin', ...)做一个请求 – 承诺POST到/api/login 。 我也尝试使用curl -X POST并修改发布path到url查询参数。 我只是不断得到一个“未经授权”的错误,没有护照战略代码console.log有史以来就开始。

服务器

 var server = express(); server.use(passport.initialize()); let opts = { jwtFromRequest: ExtractJwt.fromBodyField('token'), secretOrKey: config.apiKey, algorithms: [ 'HS256', 'HS384' ], ignoreExpiration: true }; passport.use(new JwtStrategy(opts, function( jwt_payload, done ) { // mongoose users query against JWT content return done(null, true); // EDIT: test to finish flow })); server.use('/api', routes); server.listen(8000); 

路线

 let routes = express.Router(); routes.post('/securedroute', passport.authenticate('jwt', { session: false }), ( req, res ) => { res.send('success'); } ); routes.get('/testsecure', ( req, res ) => { // EDIT: mock request with JWT let options = { method: 'POST', uri: 'http://localhost:8000/api/authentication/securedroute', body: { token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZW1haWwiOiJhQGEuY29tIiwiYWRtaW4iOnRydWV9.afjyUmC81heavGBk7l9g7gAF5E6_eZeYSeE7FNmksp8' }, json: true }; rp(options) .then( ( info ) => res.json({ info }) ) .catch( ( err ) => res.json({ err }) ); }); export default routes; 

对上面的代码做了一些编辑来完成stream程。

完全理解,这是超级n00b,但希望它会帮助初学者试图了解身份validation。

所以完全错过了你需要在请求中发送一个JWT的事实。 请求中使用的JWT需要使用与您在passport-jwt策略opts.secretOrKey中定义的相同的秘密。 如果他们不匹配,你会得到一个未经授权的,永远不会达到passport.use战略块。

生成一个HMAC http://www.freeformatter.com/hmac-generator.html

创build了一个testingJWT https://jwt.io/#debugger

Greate指南,我最终发现 http://jonathanmh.com/express-passport-json-web-token-jwt-authentication-beginners/