[FEATURE] Add OAuth2 refresh token Form dependency in security/oauth2.py
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- Or, I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
Description
security/oauth2.py
already contains OAuth2PasswordRequestForm
, why don’t we add OAuth2RerefreshRequestForm
to FastAPI as well? It is also well defined in the OAuth RFC. To implement a complete OAuth2 with FastAPI, token refresh is needed. Also, the docs can be updated with a full example.
As the RFC states, the refresh request has to be exactly like this:
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
The solution you would like
from fastapi import FastAPI, Depends
from fastapi.security import OAuth2RefreshRequestForm
app = FastAPI()
@app.route("/refresh")
def refresh_token(form: OAuth2RefreshRequestForm = Depends()):
# check if the refresh token is valid
return {
"access_token":"2YotnFZFEjr1zCsicMWpAA",
"token_type":"example",
"expires_in":3600
}
A possible implementation would be:
class OAuth2RefreshRequestForm:
def __init__(
self,
grant_type: str = Form(None, regex="refresh_token"),
refresh_token: str = Form(...)
):
self.grant_type = grant_type
self.refresh_token = refresh_token
It is indeed simple, but having it in builtin still saves users some time to look up in the RFC. Also, the tutorial in docs can be more complete.
Additional context
Issue Analytics
- State:
- Created 2 years ago
- Reactions:14
- Comments:9
Top Results From Across the Web
Tutorial | Spring Boot and OAuth2
To make the application secure, you can simply add Spring Security as a dependency. Since you're wanting to do a "social" login (delegate...
Read more >Spring Boot OAuth2 | Securing REST API - LinkedIn
This dependency will add all the prerequisite to use Oauth2 features for our application. The next step is to add some configurations for ......
Read more >OAuth2 with Password (and hashing), Bearer with JWT tokens
We need to install python-jose to generate and verify the JWT tokens in Python: ... Create a utility function to hash a password...
Read more >OAuth2 Remember Me with Refresh Token - Baeldung
Learn how to implement remember-me functionality with an Angular frontend, for an application secured with Spring Security OAuth.
Read more >Using OAuth 2.0 for Web Server Applications | YouTube Data ...
Using OAuth 2.0 for Web Server Applications. bookmark_border ... your app can refresh the access token without user interaction.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can take a shot at the implementation
@Jan-Jasek - do you still have plans to work on this feature?