【发布时间】:2016-09-13 05:07:29
【问题描述】:
我正在实现OAuth2.0 server 并尝试读取concepts of refresh token 以及如何使用来调用访问令牌以及如何安全地存储它。
这对我来说听起来很令人困惑的是`因为 Auth2.0 令牌是短暂的令牌,并且假设在成功登录后服务器给了我一个类似这样的令牌:
{
"token_type":"bearer",
"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiVlx1MDAxNcKbwoNUwoonbFPCu8KhwrYiLCJpYXQiOjE0NDQyNjI1NDMsImV4cCI6MTQ0NDI2MjU2M30.MldruS1PvZaRZIJR4legQaauQ3_DYKxxP2rFnD37Ip4",
"expires_in":3600,
"refresh_token":"fdb8fdbecf1d03ce5e6125c067733c0d51de209c"
}
由于访问令牌是短暂的令牌,在我的情况下它将在 1 小时后过期。
假设一个用户是browsing a protected resource,其凭据为access tokens,一段时间后其访问令牌过期,他的请求返回这样的响应。
{
"code":401,
"error":"invalid_token",
"error_description":"The access token provided has expired."
}
现在可以使用浏览器 cookie 中存储的新 refresh tokenbut doesn't the user experience is getting affected as each time an access token expires in an hour a valid request by a client is getting rejected due to expired access token and then we have to first fetch a new access token and then try that request again. 生成新令牌
刷新令牌的获取是否只能这样工作,或者我错过了一些重要的概念?
另外,如何在 cookie 中安全地存储刷新令牌,因为它也不是最好的安全存储方式?
【问题讨论】:
-
这就是为什么大多数开发人员会在访问令牌过期前 5 分钟检查 expires_in 并获得一个新的。这样用户就不会受到影响。通常,当我获得访问令牌时,我会将 3600 添加到当前时间,然后存储它,我知道何时需要获得新的访问令牌。
-
@DaImTo 我怎样才能在浏览器上安全地存储
refresh token? -
只需将其转储到 cookie 或会话值中,对于没有创建它的客户端 ID 和客户端密钥的任何人来说,它都是无用的,我会假设代码保留在服务器端。
标签: authentication cookies oauth oauth-2.0 openid-connect