如何调试护照
how to debug passport
我似乎一辈子都想不出如何调试通行证策略。我只是在概念上做了一些非常错误的事情吗?我已经为此苦苦思索了大约 10 个小时,但一点进展都没有。
这是我第一次使用护照。在这种特殊情况下,我使用的是 passport-jwt。我知道对象键不正确,但我只是想使用 console.log() 来跟踪通过服务器的路径,所以我了解它是如何工作的。我什至没有达到 passport.use( new JwtStrategy(..))
.
我省略了不必要的代码。与我的 mongodb 的连接很好,我的猫鼬模式也很好。
我正在使用测试路线 server.get('/fakelogin', ...)
进行测试,该路线向 /api/login
执行请求承诺 POST。我还尝试使用 curl -X POST
并将 post 路由修改为 url 查询参数。我只是经常收到 "Unauthorized" 错误,而护照策略代码 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;
对上面的代码进行了一些编辑以完成流程。
完全理解这是超级 n00b,但希望它能帮助初学者尝试理解 auth。
所以完全错过了您需要在请求中发送 JWT 的事实。请求中使用的 JWT 需要使用与您在 passport-jwt 策略 opts.secretOrKey 中定义的相同的秘密。如果它们不匹配,您将获得未经授权并且永远无法到达 passport.use 策略块。
生成了一个 HMAC
http://www.freeformatter.com/hmac-generator.html
创建了一个测试 JWT
https://jwt.io/#debugger
很棒的向导,我终于找到了
http://jonathanmh.com/express-passport-json-web-token-jwt-authentication-beginners/
我似乎一辈子都想不出如何调试通行证策略。我只是在概念上做了一些非常错误的事情吗?我已经为此苦苦思索了大约 10 个小时,但一点进展都没有。
这是我第一次使用护照。在这种特殊情况下,我使用的是 passport-jwt。我知道对象键不正确,但我只是想使用 console.log() 来跟踪通过服务器的路径,所以我了解它是如何工作的。我什至没有达到 passport.use( new JwtStrategy(..))
.
我省略了不必要的代码。与我的 mongodb 的连接很好,我的猫鼬模式也很好。
我正在使用测试路线 server.get('/fakelogin', ...)
进行测试,该路线向 /api/login
执行请求承诺 POST。我还尝试使用 curl -X POST
并将 post 路由修改为 url 查询参数。我只是经常收到 "Unauthorized" 错误,而护照策略代码 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;
对上面的代码进行了一些编辑以完成流程。
完全理解这是超级 n00b,但希望它能帮助初学者尝试理解 auth。
所以完全错过了您需要在请求中发送 JWT 的事实。请求中使用的 JWT 需要使用与您在 passport-jwt 策略 opts.secretOrKey 中定义的相同的秘密。如果它们不匹配,您将获得未经授权并且永远无法到达 passport.use 策略块。
生成了一个 HMAC http://www.freeformatter.com/hmac-generator.html
创建了一个测试 JWT https://jwt.io/#debugger
很棒的向导,我终于找到了 http://jonathanmh.com/express-passport-json-web-token-jwt-authentication-beginners/