在 Rest API 中使用 Facebook 在 Express 和 NodeJS 中维护 JWT 的密钥和访问令牌 [英] Maintaining Secret key and Access Token for JWT in Express and NodeJS with Facebook in Rest API

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

问题描述

我有两个应用程序:

  • 服务器(REST API 服务器)
    • 节点js
    • 快递
    • jsonwebtokens
    • express-jwt
    • 猫鼬
    • 引导
    • Angular JS
    • 本地存储
    • 角脸书
    • 角度-jwt

    稍后,客户端应用程序将被移植到使用 phonegap 的 android、iphone 和其他平台.对于 OAuth,我使用 Facebook 作为提供者.现在,我刚刚意识到 JSON Web Tokens 是这种设置的方式.我的问题是架构问题而不是语法问题 - 在 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. Angular 客户端有一个登录按钮
    2. 用户点击按钮 > Facebook Auth 开始
    3. 客户接收 user_id 和 FB Access Token
    4. Client 将 [POST json body] user_id 和 Access Token 发送到 'Node+Express Server"http://server.com/auth/login">http://server.com/auth/login'
    5. Node Server 已将 express-jwt 应用于除 /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

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

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

    var jwt = require('jsonwebtoken');

    var jwt = require('jsonwebtoken');

    app.use(expressjwt({ secret: ''}).unless({path: ['/auth/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.

    推荐答案

    Ad 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/

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

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