Implement OAuth 2.0 sign-in or API access with a third-party provider: client registration, authorization request with PKCE and state, token exchange, refresh and secure token storage.
Design and implement the OAuth 2.0 authorization code flow with PKCE between the application below and a third-party provider, following the OAuth 2.0 Security Best Current Practice (RFC 9700). If the goal is to sign users in, use OpenID Connect on top of OAuth and validate the ID token; OAuth alone grants API access, not identity. ## Inputs Provider and its documentation URL: [PROVIDER] Goal (sign-in, API access on the user's behalf, or both): [GOAL] Client type (server-side web app, single-page app, mobile or desktop app): [CLIENT_TYPE] Language and library (for example openid-client, Authlib, Spring Security): [STACK] Scopes needed and why: [SCOPES] Redirect URI(s): [REDIRECT_URIS] ## Instructions 1. **Client registration**: what to register (application name, exact redirect URIs, client type), and what the provider issues (client ID; a client secret only for confidential clients). Store secrets server-side. 2. **Authorization request**: build the URL to the provider's authorization endpoint with response_type=code, client_id, redirect_uri, scope, a random `state` bound to the user's session, and a PKCE `code_challenge` (S256) derived from a random `code_verifier`. Add `nonce` for OpenID Connect. 3. **User authentication and consent** happen at the provider; the app receives `code` and `state` at the redirect URI and rejects mismatched `state`. 4. **Token request**: exchange the code at the token endpoint with the `code_verifier` (and client authentication for confidential clients) over TLS. Validate the ID token if using OpenID Connect. 5. **Access token usage**: send it as `Authorization: Bearer` to the provider's API; handle 401 by refreshing or re-authorizing. 6. **Token refresh**: use the refresh token only if the provider issues one; for public clients the provider must rotate or sender-constrain refresh tokens. Store tokens server-side (for browser apps, a backend-for-frontend keeps tokens out of JavaScript). 7. Do not use the implicit grant or the resource owner password credentials grant. 8. Request the smallest [SCOPES] that meet [GOAL]. ## Return 1. Numbered flow with each HTTP request and response: method, URL, parameters, headers. 2. A sequence diagram in text or Mermaid. 3. Code for the login redirect, callback handler, token storage and refresh in [STACK]. 4. Provider-specific notes taken from [PROVIDER]'s documentation (endpoints, PKCE support, refresh token behaviour), each with its URL. 5. Error handling: denied consent, invalid state, expired code, revoked refresh token. 6. A security checklist. ## Acceptance checks - PKCE with S256 and `state` are present in every authorization request. - Redirect URIs are matched exactly. - No tokens or client secrets reach browser JavaScript or logs. - Provider-specific details cite the provider's docs; nothing is assumed from another provider.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Provider: Google Identity (OpenID Connect), https://developers.google.com/identity/openid-connect/openid-connect Goal: sign users in and read their primary calendar Client type: server-side web app (confidential) Stack: Node.js 22 with Express and openid-client Scopes: openid email profile, plus read-only calendar access Redirect URI: https://app.example.com/auth/callback
A flow with an authorization request including response_type=code, state, nonce and code_challenge_method=S256; a callback that checks state, exchanges the code with the code_verifier and client secret, validates the ID token (issuer, audience, nonce, expiry); server-side storage of access and refresh tokens encrypted at rest; a read-only calendar scope rather than full access; refresh handling for revoked tokens; and a checklist referencing RFC 9700. Provider details are cited from Google's documentation. Illustrative expectation; the PKCE S256 derivation was executed locally against the RFC 7636 Appendix B test vector (see report).
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: