使用护照-jwt 对节点 API 进行身份验证
Authenticating node API with passport-jwt
我正在尝试使用护照-jwt设置JWT身份验证。 我想我已经采取了正确的步骤,但是测试 GET 不会成功,我不知道如何调试它。
这是我所做的:
-
尽可能直接从文档中设置 Passport-JWT
var jwtOptions = { secretOrKey: 'secret', issuer: "accounts.examplesoft.com", // wasn't sure what this was, so i left as defaulted in the doc audience: "yoursite.net" // wasn't sure what this was, so i left as defaulted in the doc }; jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeader(); passport.use(new JwtStrategy(jwtOptions, function(jwt_payload, done) { User.findOne({id: jwt_payload.sub}, function(err, user) { if (err) { return done(err, false); } if (user) { done(null, user); } else { done(null, false); // or you could create a new account } }); }));
-
将令牌结果添加到我的用户/登录端点
var jwt = require('jsonwebtoken'); // ... exports.postLogin = function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err) throw err; if (!user) { return res.send({ msg: 'Login incorrect' }); } req.logIn(user, function(err) { if (err) throw err; var secretOrKey = jwtOptions.secretOrKey; var token = jwt.sign(user, secretOrKey, { expiresIn: 631139040 // 20 years in seconds }); res.send({ user: user, jwtToken: "JWT " + token }); }); })(req, res, next); };
到目前为止,情况看起来不错。 我可以登录一个用户(使用护照本地身份验证),并且响应是我希望...
{ "用户":{ "_id": "56c8b5bd80d16ef41ec705dd", "电子邮件": "peachy@keen.com", "密码": "$2A$10$ZD ...等等", "__v": 0, }, "jwtToken": "JWT eyJ0eXAiOiJ ....等等。
我像这样创建了一个不受保护的测试路由...
// in my routes file
app.get('/user/tokenTest', user.tokenTest);
在我的控制器中,一个简单的端点...
exports.tokenTest = function(req, res) {
console.log(req.headers);
res.send("token test!!");
};
GET-ing也工作正常。
但是后来我尝试像这样保护该路线:
app.get('/user/tokenTest', passport.authenticate('jwt', { session: false }), user.tokenTest);
在我这样做之后,只有悲伤。 我发送这样的请求:
curl -k 'https://localhost:3443/user/tokenTest' -H 'Authorization: JWT eyJ0eXAiOiJ... etc.'
并且总是,总是得到一个 401:
未经 授权
控制器中的控制台日志似乎不会执行,passport.use
策略方法中的日志记录也不会执行。 我已经调整了又调整,但我有点迷茫。 passport-jwt 文档只提供了示例,几乎没有其他帮助。
请问,关于我上面犯的错误的任何想法,或者至少如何进行调试?
对于任何跟随我的可怜灵魂:passport-jwt 文档暗示身份验证标头应该看起来像这样......
授权:智威汤逊JSON_WEB_TOKEN_STRING.....
事实证明,这是误导性的(无论如何,对我来说)。
幸运的是,多亏了这篇文章,我才能够了解代币是如何构建的。 (令牌的前缀直到第一个"."是方案的 base64 编码。 前面的"JWT"是阻止验证工作的噪音。
因此,修复方法是将用户控制器返回的令牌从:
res.send({ user: user, jwtToken: "JWT " + token });
更简单:
res.send({ user: user, jwtToken: token });
唷。 是我,还是真的令人沮丧,这些事情在这么多节点包文档中解释得多么不充分??
我可能迟到了,但我遇到了类似的问题,我有另一个解决方案。您可以使用此options.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('JWT')
使用以下格式从身份验证标头中提取 JWT 令牌:
授权:智威汤逊JSON_WEB_TOKEN_STRING.....
这是我使用的文档: https://github.com/themikenicholson/passport-jwt
从请求中提取 JWT
可以通过多种方式将 JWT 包含在请求中。在 为了保持尽可能的灵活性,JWT 是从 由作为 jwtFromRequest 传入的用户提供的回调发出的请求 参数。此回调,从现在开始称为提取器, 接受请求对象作为参数并返回编码的 JWT 字符串或空值。随附的提取器
中提供了许多提取器工厂函数 护照-智威汤逊。摘录。这些工厂函数返回一个新的 使用给定参数配置的提取器。
fromHeader(header_name) creates a new extractor that looks for the JWT in the given http header fromBodyField(field_name) creates a new extractor that looks for the JWT in the given body field. You must have a body parser configured in order to use this method. fromUrlQueryParameter(param_name) creates a new extractor that looks for the JWT in the given URL query parameter. fromAuthHeaderWithScheme(auth_scheme) creates a new extractor that looks for the JWT in the authorization header, expecting the scheme to match auth_scheme. fromAuthHeaderAsBearerToken() creates a new extractor that looks for the JWT in the authorization header with the scheme 'bearer' fromExtractors([array of extractor functions]) creates a new extractor using an array of extractors provided. Each extractor is attempted in order until one returns a token.
- Twitter API 1.1无法对您2进行身份验证
- javascript api,用于在第三方域上存储身份验证令牌
- 实施Facebook's无用户身份验证的Graph API
- 显示LinkedIn Api的个人资料数据,无需身份验证
- postmates api的AJAX中的HTTP身份验证失败
- Wordpress REST API身份验证,无需登录页面
- 如何在带有JQuery的Neo4j 2.2.x中使用事务Cypher HTTP端点和新的REST API身份验证和授权
- 如何使用不同的API's在同一个Angular JS应用程序中具有不同的身份验证标头
- Twitter搜索API身份验证错误
- 在Javascript中对API进行身份验证
- 使用基于令牌的身份验证保护Sails.js中的API路由
- AngularJS身份验证,带有一个restful api,成功时返回一个令牌
- 访问 Twitter API - 身份验证和骨干模型
- JS API 身份验证
- 网页 API 身份验证和 SSL
- Facebook api身份验证PHP
- 在浏览器中使用Javascript进行REST API身份验证
- 外部专用API身份验证(使用主干)
- Rest api 身份验证在 javascript 中
- 使用 javascript 进行 HTTP 登录 API 身份验证