You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Secure the API's mutating endpoints (POST, PUT, and DELETE) by introducing JWT-based authentication following the OAuth 2.0 Client Credentials Flow.
In this model, trusted machine-to-machine clients can obtain a short-lived JWT by submitting their client_id and client_secret to a dedicated authentication endpoint. This token is then included in the Authorization header to access protected resources.
sequenceDiagram
participant Client as Client (Machine-to-Machine app)
participant Server as Server (FastAPI RESTful API)
Note over Client,Server: Step 1 - Obtain Access Token
Client->>Server: POST /auth/token (client_id, client_secret)
Server-->>Client: 200 (OK) { access_token, expires_in, token_type }
Note over Client,Server: Step 2 - Use Token to Access Protected Resources
Client->>Server: POST /{resource}/{id} (Authorization: Bearer {access_token})
Server-->>Client: 201 (Created)
Client->>Server: PUT /{resource}/{id} (Authorization: Bearer {access_token})
Server-->>Client: 204 (No Content)
Client->>Server: DELETE /{resource}/{id} (Authorization: Bearer {access_token})
Server-->>Client: 204 (No Content)
Loading
This mechanism enhances API security by ensuring only authenticated clients can perform data mutations, while maintaining statelessness.
Proposed Solution
Add an /auth/token route to issue JWTs to clients with valid credentials.
Introduce a simple in-memory (or configurable) client registry (client_id, client_secret).
Secure mutating routes in player_route.py using a dependency that validates and decodes the incoming JWT.
Configure token expiration (e.g., 60 minutes) and signing via a secret key from environment/config.
Include test coverage and update Postman collection for authentication.
fromfastapiimportDepends, HTTPException, statusfromfastapi.securityimportHTTPBearer, HTTPAuthorizationCredentialsfromjoseimportjwt, JWTErrorimportossecurity=HTTPBearer()
ALGORITHM="HS256"# These values should be loaded from a secure configuration or environment variablesKEY="1LnBfWcu7gTDmqT41QCW4ANu1xsHMcseingKWruVveM="defverify_token(credentials: HTTPAuthorizationCredentials=Depends(security)):
token=credentials.credentialstry:
payload=jwt.decode(token, KEY, algorithms=[ALGORITHM])
exceptJWTError:
raiseHTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid or expired token")
returnpayload
4. Secure mutating routes in routes/player_route.py:
Description
Secure the API's mutating endpoints (
POST,PUT, andDELETE) by introducing JWT-based authentication following the OAuth 2.0 Client Credentials Flow.In this model, trusted machine-to-machine clients can obtain a short-lived JWT by submitting their
client_idandclient_secretto a dedicated authentication endpoint. This token is then included in theAuthorizationheader to access protected resources.sequenceDiagram participant Client as Client (Machine-to-Machine app) participant Server as Server (FastAPI RESTful API) Note over Client,Server: Step 1 - Obtain Access Token Client->>Server: POST /auth/token (client_id, client_secret) Server-->>Client: 200 (OK) { access_token, expires_in, token_type } Note over Client,Server: Step 2 - Use Token to Access Protected Resources Client->>Server: POST /{resource}/{id} (Authorization: Bearer {access_token}) Server-->>Client: 201 (Created) Client->>Server: PUT /{resource}/{id} (Authorization: Bearer {access_token}) Server-->>Client: 204 (No Content) Client->>Server: DELETE /{resource}/{id} (Authorization: Bearer {access_token}) Server-->>Client: 204 (No Content)This mechanism enhances API security by ensuring only authenticated clients can perform data mutations, while maintaining statelessness.
Proposed Solution
/auth/tokenroute to issue JWTs to clients with valid credentials.client_id,client_secret).player_route.pyusing a dependency that validates and decodes the incoming JWT.Suggested Approach
1. Install dependencies
2. Create
routes/auth_route.py:3. Create
services/auth_dependency.py:4. Secure mutating routes in
routes/player_route.py:5. Sample token request with
curlAcceptance Criteria
/auth/tokenendpoint issues valid JWT for known clientsPOST,PUT,DELETEroutes are secured and reject unauthenticated requests403 Forbiddenfor invalid/missing JWTsJWT_SECRET) is sourced from.envor config