【问题标题】:How to get the value of decoded JSON Web Token (JWT)如何获取解码的 JSON Web Token (JWT) 的值
【发布时间】:2016-01-07 18:19:57
【问题描述】:

我正在使用 Mailgun 节点 machine-pack 发送一封电子邮件,其中包含一个使用 machinepack-jwt 创建的 JWT 的 url。当用户在生成的电子邮件中单击“确认”时,它会点击我想要解码 JWT 的 Sails 控制器方法。

我不断得到一个具有空字符串作为其值的属性的对象。

供参考: 我正在使用node-machine machinepack-jwt 进行编码和解码。我试图这样标记问题,但此标记不在可用标记中,而且我没有所需的 1500 个代表点

我的帆控制器:

 module.exports = {
   //Encode method
   signup: function (req, res){
     if (!req.param('serviceManager')) {
       res.badRequest('Missing required parameter!');
     } else {
       var Mailgun = require('machinepack-mailgun');
       var JWT = require('machinepack-jwt');
       var newUser = req.param('serviceManager');
       JWT.encode({
         secret: 'my_secret',
         algorithm: 'my_algo',
         expires: 2880, //in minutes(two days)
         payload: newUser.email + ':' + newUser.password
       }).exec({
         error: function (err){
           console.log(err);
         },
         success : function (authToken){
           Mailgun.sendHtmlEmail({//my Mailgun send with template that has authToken in it});

    //Decode method
    confirm_email: function (req, res){
      if (!req.params[0]) {
        res.badRequest('Missing required parameter!');
     } else {
       var JWT = require('machinepack-jwt');
       var authToken = req.params[0];
       console.log(authToken);
       JWT.decode({
         secret: 'my_secret',
         token: authToken,
         algorithm: 'my_algo'
       }).exec({
         error: function (err) {
           res.send(err);
         },
         success: function (decodedToken) {
           res.view('emailconfirmed');
           console.log(decodedToken);// returns { id: '', email: '', role: '', sessionId: '' }
         }
       });
     }
   }

我期待的是我在发送的 JWT 中编码的用户电子邮件和密码。

【问题讨论】:

标签: node.js sails.js jwt sails-postgresql


【解决方案1】:

您是否使用虚假数据显示代码?我问是因为 JWT 中的算法参数的值“my_algo”不正​​确。您只能使用几种算法 - S256、HS384、HS512 和 RS256

接下来你应该像这样使用这段代码:

JWT.encode({
         secret: 'my_secret',
         algorithm: 'HS256',
         expires: 2880, //in minutes(two days)
         payload: newUser
       }).exec({

 JWT.decode({
         secret: 'my_secret',
         token: authToken,
         algorithm: 'HS256'
         schema:'email, password' // set here properties from payload object
       }).exec({

在这个库中,您应该使用架构参数来为您的数据设置架构 - http://node-machine.org/machinepack-jwt/decode

//-----

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function (req, res) {
  var JWT = require('machinepack-jwt');
       var newUser ={
         username:'username',
         password:'pass'
       }

       JWT.encode({
         secret: 'my_secret',
         algorithm: 'HS256',
         expires: 2880, //in minutes(two days)
         payload: newUser
       }).exec({
         error: function (err){
           console.log(err);
         },
         success : function (authToken){
           JWT.decode({
                   secret: 'my_secret',
                   token: authToken,
                   algorithm: 'HS256',
                   schema:'username,password'
                 }).exec({
                   error: function (err) {
                     res.send(err);
                   },
                   success: function (decodedToken) {
                     res.send(decodedToken);
                   }
                 });
         }
       });
});
app.listen(3000);

module.exports = app;

【讨论】:

  • 我认为我上面链接到的库的decode() 方法存在问题。我能够使用 machinepack-jwt 所依赖的 [this] (github.com/auth0/node-jsonwebtoken) 节点模块的 jwt.verify() 方法进行解码。是的,我在示例中使用了虚假数据。我的算法是有效的。我认为加载参数的顺序可能有问题。当我有时间时,我会弄清楚并提出拉取请求并在此处发布解决方案。
  • 我为此编写了一些测试,当我使用模式参数并从有效负载对象设置属性时,然后工作。
  • 测试和你上面贴的一样吗?因为我也尝试过使用模式参数。它的值也以空字符串的形式返回。
  • 所以我看到的主要区别是您在 encode 方法的成功回调中调用 decode 方法。我在单独的命名函数中使用它们。否则,这就是我使用的确切代码。
  • 那和我将一个字符串传递给编码中的有效负载。一个电子邮件地址。以及 Schema 的一个参数。
猜你喜欢
  • 2019-11-24
  • 2014-07-11
  • 2019-03-10
  • 2018-11-28
  • 1970-01-01
  • 2015-09-23
  • 2016-04-07
  • 2016-03-20
  • 1970-01-01
相关资源
最近更新 更多