简体   繁体   English

在OAuth2.0中使用刷新令牌

[英]Usage of Refresh Token in OAuth2.0

I am implementing an OAuth2.0 server and trying to read the concepts of refresh token and how to use to call the access token also how to securely store it. 我正在实现OAuth2.0 server并尝试阅读concepts of refresh tokenconcepts of refresh token以及如何使用它来调用访问令牌以及如何安全地存储它。

One this which sounds very confusing to me is that `since Auth2.0 token is short lived tokens and suppose after login successfully the server gave me a token which is like that: 其中一个让我感到困惑的是,“因为Auth2.0令牌是短暂的令牌,并且假设在成功登录后服务器给了我一个令牌,就像这样:

{
    "token_type":"bearer",
    "access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiVlx1MDAxNcKbwoNUwoonbFPCu8KhwrYiLCJpYXQiOjE0NDQyNjI1NDMsImV4cCI6MTQ0NDI2MjU2M30.MldruS1PvZaRZIJR4legQaauQ3_DYKxxP2rFnD37Ip4",
    "expires_in":3600,
    "refresh_token":"fdb8fdbecf1d03ce5e6125c067733c0d51de209c"
}

Since access tokens are short lived tokens and it will expired after 1 hour in my case. 因为访问令牌是短期令牌,并且在我的情况下它将在1小时后过期。

Suppose a user is browsing a protected resource with its access tokens credentials and after some time its access tokens get expired and his request returns a response like this. 假设用户正在使用其access tokens凭证browsing a protected resource ,并且在一段时间之后其访问令牌过期并且他的请求返回这样的响应。

{
  "code":401,
  "error":"invalid_token",
  "error_description":"The access token provided has expired."
}

Now a new token can be generated by using the new refresh token stored in the browser cookie, but 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中的新refresh token生成新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.

Does fetching of refresh token works like that only or I am missing some important concept? 获取刷新令牌是否只是这样,或者我错过了一些重要的概念?

Also how can one store refresh token securely at cookie as it is also not the best secure way to store? 另外,如何在cookie中安全地刷新令牌,因为它也不是最安全的存储方式?

A refresh token is a special kind of token that can be used to obtain a renewed id_token at any time. 刷新令牌是一种特殊的令牌,可用于随时获取更新的id_token。 Refresh tokens must be stored securely by an application because they essentially allow a user to remain authenticated forever. 刷新令牌必须由应用程序安全存储,因为它们基本上允许用户永久保持身份验证。

The response of an authentication request can result in an id_token being issued by OAuth. 验证请求的响应可能导致OAuth发出id_token。 This token can be used to make authenticated calls to a secured API. 此令牌可用于对安全API进行经过身份验证的调用。

Among other security measures like signing, OAuths have an expiration date indicated by the exp claim. 在签名等其他安全措施中,OAuths具有exp声明所指示的到期日期。 However, applications that are locally installed on a device such as a desktop or smartphone might want to avoid asking the user to enter credentials each time a token expires. 但是,本地安装在桌面或智能手机等设备上的应用程序可能希望避免在每次令牌过期时要求用户输入凭据。

A refresh token allows the application to request OAuth to issue a new id_token directly, without needing to re-authenticate. 刷新令牌允许应用程序直接请求OAuth发出新的id_token,而无需重新进行身份验证。 This works as long as the refresh token has not been revoked. 只要尚未撤消刷新令牌,这就有效。

Security considerations 安全考虑

Because a refresh token never expires, it is important to provide a way to revoke them. 由于刷新令牌永不过期,因此提供撤消它们的方法非常重要。 This can be done manually from the dashboard or programatically through Auth's API. 这可以通过仪表板手动完成,也可以通过Auth的API以编程方式完成。

Refresh tokens can be issued and revoked for each combination of app, user and device. 可以为应用,用户和设备的每个组合发布和撤消刷新令牌。 To revoke a refresh token, you can call the revoke refresh token endpoint: 要撤消刷新令牌,可以调用撤消刷新令牌端点:

DELETE https://YOUR_NAMESPACE/api/users/<user id>/refresh_tokens/<refresh token>

{
  "Authorization":   "Bearer <your access token>",
}

Obtaining a refresh token 获取刷新令牌

To obtain a refresh token, the offline_access scope and an arbitrary device name must be included when initiating an authentication request through the /authorize endpoint. 要获取刷新令牌,在通过/ authorize端点启动身份验证请求时,必须包含offline_access范围和任意设备名称。 For example: 例如:

GET https://YOUR_NAMESPACE/authorize/?
    response_type=token
    &client_id=YOUR_CLIENT_ID
    &redirect_uri=YOUR_CALLBACK_URL
    &state=VALUE_THAT_SURVIVES_REDIRECTS
    &scope=openid%20offline_access
    &device=my-device

Using a refresh token 使用刷新令牌

To obtain a new id_token, the delegation endpoint is used: 要获取新的id_token,使用委托端点:

POST https://YOUR_NAMESPACE/delegation
Content-Type: 'application/json'
{
  "client_id":       "YOUR_CLIENT_ID",
  "grant_type":      "urn:ietf:params:oauth:grant-type:jwt-bearer",
  "refresh_token":   "your_refresh_token",
  "api_type":        "app"
}

A response from this request could be as follows: 该请求的答复如下:

{
  "token_type": "Bearer",
  "expires_in": 30000,
  "id_token": "eyJ..."
}

The expires_in parameter indicates the lifetime of the new JWT in seconds. expires_in参数以秒为单位指示新JWT的生存期。 It can be calculated by the difference between the exp and iat claims of the JWT. 它可以通过JWT的exp和iat声明之间的差异来计算。

IMPORTANT ADVICE: obtaining new tokens using the refresh_token should happen only if the id_token has expired. 重要建议:仅当id_token已过期时,才应使用refresh_token获取新令牌。 For example, it is a bad practice to call the endpoint to get a new token every time you do an API call. 例如,每次执行API调用时调用端点获取新令牌是一种不好的做法。 There are rate limits in Auth0 that will throttle the amount of requests that can be done using the same token from a certain IP to this endpoint. Auth0中的速率限制将限制使用相同令牌从某个IP到此端点可以完成的请求数量。

for further reading try link below 进一步阅读尝试链接如下

https://auth0.com/docs/refresh-token https://auth0.com/docs/refresh-token

Refresh token is a token that you use to get another valid token to interact with the API you're using since the tokens are short lived. 刷新令牌是一种令牌,用于获取另一个有效令牌以与您正在使用的API进行交互,因为令牌是短暂的。 Then, since tokens are short lived you would have to get another oauth2.0 credential from the user every time you want to acces to the API. 然后,由于令牌是短暂的,因此每次要访问API时,都必须从用户那里获得另一个oauth2.0凭证。 How to avoid this? 怎么避免这个? -> Refresh token. - >刷新令牌。

As told, refresh token is not a token for API acces at all, its just a kind of token that you use to get new short lived tokens for each time. 如上所述,刷新令牌根本不是API访问的令牌,它只是一种用于每次获取新的短期令牌的令牌。

Then your first login credential gets a token and a refresh token with the user consent, then you dont need user consent anymore, just use refresh token. 然后,您的第一个登录凭据获得用户同意的令牌和刷新令牌,然后您不再需要用户同意,只需使用刷新令牌。

Not sure if I'm answering your question :) 不确定我是否回答你的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM