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 flow, machine-to-machine communication is secured by issuing JWTs to clients authenticated using a client_id and client_secret. These short-lived tokens are then required in the Authorization header for protected endpoints. This ensures only registered clients can perform state-changing operations on the API.
sequenceDiagram
participant Client as Client (Machine-to-Machine app)
participant Server as Server (Spring Boot RESTful API)
Note over Client,Server: Step 1 - Obtain JWT 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 - Access Protected Resources
Client->>Server: POST /{resource} (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
Proposed Solution
Implement JWT issuance and validation using Spring Security and a lightweight in-memory client registry (or configurable store). The JWTs will be signed using a shared secret (HMAC) or an asymmetric key pair (RSA) depending on the security posture.
Key points:
Introduce a /token endpoint for clients to exchange their credentials for a JWT.
Secure mutating routes with Spring Security filters, enforcing valid JWTs.
Validate claims like expiration, audience, and scope (if required).
Add integration tests for token issuance and protected route access.
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 flow, machine-to-machine communication is secured by issuing JWTs to clients authenticated using a
client_idandclient_secret. These short-lived tokens are then required in theAuthorizationheader for protected endpoints. This ensures only registered clients can perform state-changing operations on the API.sequenceDiagram participant Client as Client (Machine-to-Machine app) participant Server as Server (Spring Boot RESTful API) Note over Client,Server: Step 1 - Obtain JWT 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 - Access Protected Resources Client->>Server: POST /{resource} (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 ContentProposed Solution
Implement JWT issuance and validation using Spring Security and a lightweight in-memory client registry (or configurable store). The JWTs will be signed using a shared secret (HMAC) or an asymmetric key pair (RSA) depending on the security posture.
Key points:
/tokenendpoint for clients to exchange their credentials for a JWT.Suggested Approach
1. Define a configuration for client credentials
2. Token issuance endpoint
3. JWT generation logic
4. Security configuration
5. JWT Authentication Filter
Sample request with
curlAcceptance Criteria
/tokenendpoint exists and accepts validclient_id/client_secretcredentials to return a JWT.sub,iat, andexpclaims at minimum.POST,PUT,DELETE) are protected and require a valid JWT viaAuthorization: Bearer <token>.401 Unauthorized.GETroutes.