Toy Implementation of OAuth2 in python with fastapi
- Navigate to the root dir of the project and install third party packages
pip install -r requirements/requirements.txt
- Party time. Start the fastapi sever.
uvicorn main:app --reload
- In the browser of your choice, navigate to the docs page to interact with the authenticated root endpoint.
http://127.0.0.1:8000/docs
- Within the browser docs, click to the
Authorization
button and enter theclient_id
andclient_secret
client_id='johnsmith', client_secret='John smith'
- On authentication success you can now execute the root endpoint request.
This is a toy implementation and should NOT be used in a production environment. If you wish to implement OAuth2 more robustly, persistence should be utilized to securely store client ids and secrets.
sequenceDiagram
participant C as Client
participant A as Server (Authorization Resource)
C->>A: authorize(credentials, state)
A-->>C: Return code, state
C->>A: fetch_token(credentials, code)
A-->>C: access token
C->>A: get_user_profile(access_token)
A-->>C: User Profile