保持密钥和访问令牌在快速和NodeJS与Facebook在Rest API中的JWT [英] Maintaining Secret key and Access Token for JWT in Express and NodeJS with Facebook in Rest API

查看:149
本文介绍了保持密钥和访问令牌在快速和NodeJS与Facebook在Rest API中的JWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序:


  • 服务器(REST API服务器)

    • 节点js

    • Express

    • jsonwebtokens

    • express-jwt

    • mongoose

    • server ( REST API Server)
      • node js
      • Express
      • jsonwebtokens
      • express-jwt
      • mongoose

      • bootstrap

      • 角度JS

      • 本地存储

      • angular-facebook

      • -jwt

      • bootstrap
      • Angular JS
      • local-storage
      • angular-facebook
      • angular-jwt

      客户端应用程序将被移植到android,iphone和其他平台使用phonegap。对于OAuth,我正在使用Facebook作为提供者。现在,我刚才发现JSON Web Token是这种设置的方式。我的问题是一个架构性的而不是语法的 - 如何在nodejs中使用JWT签署Facebook访问令牌和用户ID时如何管理密钥?

      Lateron, the client app will be ported for android, iphone and other platforms using phonegap. For OAuth, I am using Facebook as the provider. Now, I just realized JSON Web Tokens are the way to go for this kind of set up. My question is an architectural one rather than syntactical one - how to manage a secret key when signing the facebook access token and user id with JWT in nodejs?

      所以这就是流程在我的应用程序中工作:

      So this is how the flow works in my app:


      1. 角色客户端有一个登录按钮

      2. 用户点击按钮> Facebook Auth开始

      3. 客户端接收user_id和FB访问令牌

      4. 客户端 POST json正文]将和访问令牌发送到 Node + Express Server http://server.com/auth/login '

      5. 节点服务器 / auth / login 使用

      1. Angular client has a Login button
      2. User Clicks the button > Facebook Auth starts
      3. Client receives user_id and FB Access Token
      4. Client sends[POST json body] both user_id and Access Token to Node+Express Server at 'http://server.com/auth/login'
      5. Node Server has applied express-jwt to all routes except /auth/login with a

      将所有路径应用了express-jwt var expressJwt = 'express-jwt');

      var expressJwt = require('express-jwt');

      var jwt = require('jsonwebtoken');

      var jwt = require('jsonwebtoken');

      app.use (expressjwt({secret:''})。unless({path:['/ a uth / login']});

      app.use(expressjwt({ secret: ''}).unless({path: ['/auth/login']}));

      节点服务器 req.body 接收数据,使用JavascriptSDK从Facebook获取所有个人资料的详细信息,并使用

      Node server receives data from req.body, fetches all profile details from facebook using the JavascriptSDK, and signs it using

      var token = expressjwt.sign({profile},);

      var token=expressjwt.sign({profile}, );

      现在,我的问题是:


      1. 我真的需要在数据库中存储JWT令牌吗?我肯定不会将请求标头中的令牌与数据库进行比较

      2. 每个人登录时,是否需要生成随机密钥?如果是,那么如何适应客户端和服务器?

      3. 我何时需要检查令牌过期?我如何刷新?

      我对设计流程和机制感到失落。

      I am kind of lost about the design flow and mechanism.

      推荐答案

      广告1.您不必在数据库中存储JWT。用户ID可以是有效载荷的一部分,因此不需要它。

      Ad 1. You do not have to store the JWT in the database. User ID can be part of the payload, therefore there's no need for it.

      广告2.服务器端应用程序使用一个密钥进行生成是常见的做法所有JWT。

      Ad 2. It's a common practice for the server side app to use one secret key for generating all JWT.

      广告3.检查每个请求到达API的令牌是否过期,如果令牌已过期,则不允许访问,返回401状态代码。客户端应用程序应提示用户获取凭据并请求新的JWT。如果您希望避免用户重新提交凭据,则可以发出刷新令牌,以后可以用于生成新的JWT。

      Ad 3. Check if token has expired on each request to your API and disallow access if the token has expired, return 401 status code. Client app should prompt user for credentials and request new JWT. If you want to avoid users re-submitting the credentials you can issue a refresh token that later can be used to generate new JWT.

      JWT刷新令牌流程

      http:// bitoftech。 net / 2014/07/16 / enable-oauth-refresh-tokens-angularjs-app-using-asp-net-web-api-2-owin /

      这篇关于保持密钥和访问令牌在快速和NodeJS与Facebook在Rest API中的JWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆