【发布时间】:2017-07-11 13:50:44
【问题描述】:
免责声明,我是 JWT 的新手,所以如果其中任何一个完全没有意义,你现在知道为什么大声笑了。
动机 这个实现试图解决的安全问题可以用这个场景来概括:
合法用户使用公共计算机登录网站并忘记退出,攻击者坐在那台计算机上,复制粘贴令牌并在他回家后随时使用它,(因为它总是有效的直到秘密发生变化,或者如果您将令牌存储在数据库中,直到用户更改某些有效负载信息[如果用户从不更新信息怎么办],那么令牌将永远有效)。
对上述问题进行排序的身份验证流程
1. Client logs in
1.1 Verify login details, and if valid:
1.2 Create token using user id, global secret and expiry date
1.3 Store token in Database
1.4 Send token to client
2. Client stores token [your choice where u wanna store it]
3. When client sends a request to an authenticated route, use authentication middleware to do the following checks
3.1 Verify token hasn’t been tampered with
3.1.1 If not tampered, go to 3.2
3.1.2 If tampered, redirect to /login
3.2 check if expiration date is less than current date
3.2.1 if not less, let user through to the requested route, by calling next()
3.2.2 if less, check in database if expired token matches the token stored in database
(to verify if it’s the latest expired token, or not)
3.2.2.1 if doesn’t match, redirect to /login
3.2.2.2 If matches
3.2.2.2.1 create token with renewed expiration date
3.2.2.2.2 store token in database
3.2.2.2.3 send token to client
上述实现的安全缺陷 如果攻击者可以访问令牌,并且是在令牌过期后发出第一个请求以获取新令牌的攻击者,那么当合法用户尝试获取新令牌并将其注销时,这将使合法用户无效因为他们的令牌与存储在数据库中的令牌不匹配。现在只有攻击者拥有与数据库中存储的相同的令牌。
缓解这种情况的方法 通过登录或注销无效:在登录时生成新令牌/在注销时删除令牌,覆盖数据库中的旧令牌,这将使所有先前发布的令牌在过期后立即失效。即下次攻击者尝试在过期时获取新令牌时,它不会匹配数据库中的一个,因此他们将永远被拒绝使用该令牌。
可用性问题 登录或注销会使所有其他设备上的令牌失效,因此您必须在这些设备上重新登录。
可能的解决方法 对设备类型进行简单的请求标头检查,并在登录和注销时为每个设备存储不同的令牌。然后在需要刷新令牌时根据不同设备的if语句进行不同的数据库查询,因此您知道要刷新哪个。
【问题讨论】:
-
详细场景! +1
标签: security authentication jwt