OAuth 2.0 Core: Roles, Scopes, Consents & The Authorization Code Grant
OAuth 2.0 (RFC 6749) is a delegated authorization framework that allows third-party clients to access protected resources on behalf of a resource owner without sharing the owner's credentials. It defines four primary roles: Resource Owner, Client, Authorization Server, and Resource Server.
Step 1: Client directs the user's browser to the Authorization Server's /authorize endpoint with client_id, redirect_uri, scope, and state.
Before OAuth, if you wanted an app to print photos from your social media account, you had to give the app your raw username and password. The app could now do everything your account could do, change your password, and read private messages. OAuth introduced scoped, revocable delegation.
The Valet Key: Handing a valet driver your entire master keychain allows them to enter your house, open the trunk, and drive away. Handing them a valet key only allows them to drive the vehicle 5 miles per hour and park it, without unlocking the glove compartment.
[ Resource Owner (User) ]
│
▼ (1) Grants Consent to Scopes ('photos:read')
[ Authorization Server (IdP) ] ──(2) Issues Authorization Code──► [ Client App (CosmosApp) ]
│
├──(3) Trades Code for Access Token
▼
[ Resource Server (API) ] ◄──(4) Request with Bearer Token──────────────┘
│
▼ (5) Returns Protected PhotosCosmosApp integrates with GitHub so developers can import their repositories. Instead of asking users for their GitHub username and password, CosmosApp redirects the user to GitHub with scope=repo:read. The user reviews the consent screen, GitHub returns a short-lived authorization code, and CosmosApp exchanges it for a scoped access token.
Step 1: Client directs the user's browser to the Authorization Server's /authorize endpoint with client_id, redirect_uri, scope, and state.
Step 2: Authorization Server authenticates the user and presents a consent screen describing the requested scopes.
Step 3: User approves consent; Authorization Server redirects the browser back to redirect_uri with a temporary authorization code.
Step 4: Client makes a secure back-channel POST request to the /token endpoint, submitting the authorization code and client credentials.
Step 5: Authorization Server validates the code and parameters, and returns an access token (and optional refresh token).
Step 6: Client uses the access token in the Authorization: Bearer <token> header to query the Resource Server.
// CosmosApp OAuth 2.0 Authorization URL Generator:
export function buildOAuthAuthorizeUrl(clientId: string, redirectUri: string, scopes: string[]) {
const state = crypto.randomBytes(16).toString('hex');
const params = new URLSearchParams({
response_type: 'code',
client_id: clientId,
redirect_uri: redirectUri,
scope: scopes.join(' '),
state: state,
});
return {
url: `https://auth.provider.com/oauth/authorize?${params.toString()}`,
state,
};
}- ✕OAuth 2.0 is an authentication protocol (OAuth 2.0 only handles authorization/delegated access; OpenID Connect was built on top of OAuth to provide authentication).
- ✕The authorization code and the access token are the same thing (The code is delivered via public browser redirects and is single-use; the token is exchanged via a direct back-channel connection).
Key Architectural Takeaways
- ✓OAuth 2.0 decouples resource owners from client applications through scoped, revocable access tokens.
- ✓Always validate the state parameter on the callback to prevent Login CSRF attacks.
- ✓Always register exact, fully-qualified redirect URIs at the authorization server to prevent token interception.
Using OAuth 2.0 access tokens alone to identify a user without verifying an OpenID Connect ID token.
Use OpenID Connect (OIDC) when authentication and user profile data are needed, not raw OAuth 2.0 access tokens.
What is the primary architectural purpose of the Authorization Code in the OAuth 2.0 flow?