【问题标题】:Authentication for website and adding an expiry网站身份验证并添加到期时间
【发布时间】:2021-03-20 17:55:58
【问题描述】:

我正在更改身份验证以处理仍处于开发阶段的网站上的过期问题。 (因此现在可以完全控制更改)。这就是我目前拥有的(伪代码)。我已经包含了 4 条路由,数据库当前包含 userToken(用户的 id)、userSecret(用户注销时的更改)

SignUpRoute.post('/signup', (req, res) => {
//Save userdetails, userToken, userSecret in DB
}


BidderRoute.post('/logout', userAuth, (req, res) => {
find account with matching email
update db.userSecret
response success
}

userRoute.post('/login', (req, res) => {
if req.body.email === db.email
if req.body.password === db.password(hashed)  
response (db.userToken, db.userToken)  //can this be a cookie?
    
ProductRoute.post('/data', userAuth, (req, res) => {
// some action here
}


userAuth middleware:
req.body.userSecret, req.body.userToken from body
fetch db collection where req.body.userToken === db.userToken
if userToken !== db.userToken && userSecret !==db.userSecret then error (401) - redirect to login
next()

我需要添加一个过期时间以防止用户登录超过 30 分钟,所以我想我应该在集合中添加一个过期字段并更改 /login 路由和中间件:

userRoute.post('/login', (req, res) => {
if req.body.email === db.email
if req.body.password === db.password(hashed)    
updated and save db.userSecret
updated and save db.expiryDate //30mins
response (db.userToken, db.userToken)  //can this be a cookie?

ProductRoute.post('/data', userAuth, (req, res) => {
// some action here
}
    
userAuth middleware:
req.body.userSecret, req.body.userToken from body
fetch db collection where req.body.userToken === db.userToken
if  now > db.expiryDate  then error (401) - redirect to login
if userToken !== db.userToken && userSecret !==db.userSecret then error (401) - redirect to login
if db.expiryDate < 2mins remaining then renew db.expiryDate (save in db)
next()

第一季度。在我实施之前,我想知道我是否遗漏了步骤中的任何明显内容。

第二季度。目前,前端将 userSecret 和 User 令牌存储在本地存储中,服务器发送带有 userToken 和 userSecret(不是 cookie)的 200 响应(参见 /login)。如果我想将这些数据保存在 FE 的 cookie 中,上面的代码应该发送一个 cookie 还是 FE 可以将响应保存为 cookie 无关紧要?

更新 - 如果我要使用 cookie - 因为 cookie 有一个过期时间,我想我可以使用它而不是尝试在 db 中保持过期时间。下面的工作吗?

userRoute.post('/login', (req, res) => {
if req.body.email === db.email
if req.body.password === db.password(hashed)    
updated and save db.userSecret
 send cookie (with userSecret, userToken)  with 30mins expiry


ProductRoute.post('/data', userAuth, (req, res) => {

Refresh cookie here ?
}


userAuth middleware:
If cookie received //i.e. not expired
    else re-direct to /login
    
Parse userSecret, userToken from COOKIE
fetch db collection where req.body.userToken === db.userToken
if userToken !== db.userToken && userSecret !==db.userSecret then error (401) - redirect to login
if db.expiryDate < 2mins remaining then renew db.expiryDate (save in db)
next()

【问题讨论】:

    标签: node.js authentication


    【解决方案1】:

    我认为您可以在标头中添加 jwt-token 以验证登录用户。

    1. 一旦用户登录,创建一个有效负载并创建一个 jwt-token 并将其添加到响应中。

    2. 在来自 FE 的后续请求中,您可以添加具有相同令牌的 auth 标头。

    3. 在后端,验证令牌,如果它已过期,请尝试刷新它并再次执行第一个步骤。

    有几个在线教程可以帮助您创建 jwt-token。您可以查看here。还有,这篇文章对node.js上的setup authentication真的很赞。

    第一季度。在我实施之前,我想知道我是否遗漏了任何明显的东西 在步骤中。

    其中有不少错误。您不应该将令牌保存在 DB 上,而是使用 JWT-token 来检查到期,您也可以在 node.js 中使用会话

    第二季度。目前前端将 userSecret 和 User token 存储在 本地存储,服务器发送带有 userToken 的 200 响应 和 userSecret(不是 cookie)(参见 /login)。如果我想持有这个 FE 上的 cookie 中的数据,上面的代码是否应该发送 cookie 相反还是没关系,因为 FE 可以将响应保存为 a 饼干?

    要让 FE 将此响应保存在 cookie 中,您需要在后端创建一个。当您将其附加到响应浏览器时,后续请求将自动附加它。通过how cookies work

    【讨论】:

    • 链接非常好,谢谢。我认为 JWT 可能会变得复杂,尤其是在刷新令牌方面。如果我选择 cookie,我想我可以避免像最初的想法一样在 db 中保持过期。我添加了一个更新的伪问题 - 这是正确的吗?另外,我知道如果我要使用 cookie,服务器应该发送 res.cookie('session_id', mycookie)。是吗?
    • 当您说“您不应该在 DB 上保存令牌”时,您指的是哪个令牌,如果我要使用更新后的伪文件中描述的 cookie,这是真的吗?
    • 您在问题中提到的用户令牌提供了对不同路径上的用户的访问权限。通常,这些会话/用户令牌应该像 Redis 或会话存储一样存储在缓存中。请查看此链接:security.stackexchange.com/questions/72475/…
    • 如果我选择 cookie 选项,我想我可以避免像最初的想法一样在 db 中保持过期。我添加了一个更新的伪问题 - 这会工作吗?
    • 是的,看起来不错。这是可以帮助您执行此操作的链接。如果答案有帮助,您可以接受并投票。谢谢!
    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多