首页 文章

使用JWT进行环回认证

提问于
浏览
9

我试图了解如何使自定义JWT路由适应环回安全模型 . 我的应用程序有一个涉及SMS的身份验证"dance",它使用excellent description产生有效的JWT令牌 . 我正在使用jsonwebtoken,事情按预期工作 . 获得令牌后,我的angular.js客户端发送带有 Authorisation: JWT ..token.. 标头中每个请求的令牌(发现有冲突的文档,其中一个说JWT,一个承载,但我可以解决这个问题) .

现在我想在loopback应用程序中使用令牌 . 我想使用ACL系统环回提供 . 我确实阅读了以下资源:

我不清楚接下来的步骤是什么 . 我有工作:

  • 用户'login' - 生成JWT

  • 用户名/密码登录(待退休)

  • 在环回中实现ACL实现(当我访问受ACL保护的资源时,我得到,正如预期的那样4xx错误)

  • 我的JWT令牌在请求的标头中正确(?)

我需要:

  • 基于JWT令牌的有效用户,其角色与回送ACL兼容

非常感谢帮助

1 回答

  • 3

    事实证明,这个解决方案要简单得多 . 对于初学者来说,loopback确实使用自己的jwt webtokens来保持(无状态)用户会话 . 在 Build 身份后(在我的情况下从我的JWT令牌中提取移动号码)我只需要查找该成员并生成回送本机JWT令牌 . 我的 endpoints 定义如下:

    Member.remoteMethod(
        'provideSMSToken', {
          accepts: [{
            arg: 'mobilenumber',
            type: 'string',
            description: 'Phone number including +65 and no spaces'
          }, {
            arg: 'token',
            type: 'string',
            description: 'the token received through SMS'
          }],
          returns: {
            arg: 'token',
            type: 'string'
          },
          description: 'provide SMS token to confirm login',
          http: {
            path: '/smsauthenticate',
            verb: 'post'
          },
          isStatic: true
        }
    
      );
    

    和那样的 provideSMSToken 函数:

    // Exchange the SMS Token with a login token
      Member.provideSMSToken = function(mobilenumber, token, cb) {
        var app = Member.app;
        // CHeck if the token does exist for the given phone number
        // if yes, check for the respective memeber
    
        if (!app.smsVerificationToken || !app.smsVerificationToken[mobilenumber] || app.smsVerificationToken[mobilenumber] !== token) {
          var wrongToken = new Error("Wrong or missing token");
          cb(wrongToken, "Wrong or missing token");
        } else {
          var timetolive = 86400;
          Member.lookupByPhone(mobilenumber, function(err, theOne) {
            if (err) {
              cb(err, "Sorry, no such member here!");
            } else {
              // We can provide a token now for authentication
              // using the default createAccessToken method
              theOne.createAccessToken(timetolive, function(err, accesstoken) {
                cb(err, accesstoken);
              })
            }
          });
        }
      }
    

    奇迹般有效

相关问题