Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SEC
Authentication & Security
0/10 completed
Beginner
Authentication vs authorizationHashing passwords
Intermediate
Sessions and cookiesJSON Web TokensOAuth 2.0 and OpenID ConnectMulti-factor authentication and passkeys
Advanced
The OWASP Top 10XSS, CSRF and CORSSecrets management and encryption
Pro
Security in practice
Tracks / Authentication & Security / Intermediate

OAuth 2.0 and OpenID Connect

"Sign in with Google." "Allow this app to access your GitHub repositories." "Connect your calendar." All of these use OAuth 2.0, and logging in with another company's account uses OpenID Connect (OIDC), which is built on top of it. They let users grant access without ever giving their password to your app.

The problem

A photo-printing website wants your Google Photos. The terrible old solution: type your Google password into the printing site. Now it can read your email, change your password, and you can't revoke it without changing your password everywhere.

OAuth replaces that with a limited, revocable access token: "this app may read photos, for this user, until revoked".

The roles

Role In the example
Resource owner You, the user
Client The printing website (your app)
Authorization server Google's login and consent service
Resource server The Google Photos API

Scopes describe what's being granted: photos.readonly, repo, calendar.events. Request the fewest you need.

OAuth 2.0 vs OpenID Connect

  • OAuth 2.0 is about authorization: getting an access token to call an API on the user's behalf.
  • OpenID Connect adds authentication: an ID token (a JWT) that tells your app who the user is (sub, email, name). Request it with the openid scope (plus email, profile).

"Sign in with Google/Apple/Microsoft/GitHub" is OIDC (GitHub uses plain OAuth with a user-info API).

The authorization code flow with PKCE

This is the flow to use for web apps, mobile apps and single-page apps alike (OAuth 2.1 makes it mandatory):

 Browser                   Your app (client)                 Authorization server
    │  click "Sign in"           │                                     │
    │───────────────────────────▶│ create state + code_verifier        │
    │◀── redirect to /authorize?client_id&scope&state&code_challenge ──│
    │──────────────────────── user logs in and consents ─────────────▶│
    │◀────────── redirect to /callback?code=abc&state=… ───────────────│
    │───────────────────────────▶│ check state                         │
    │                            │── POST /token (code + code_verifier)▶│
    │                            │◀──── access_token, id_token, refresh │
    │◀──── logged in (session) ──│                                     │
  1. Your app redirects the user to the provider's authorize URL with its client_id, the redirect_uri, the scopes, a random state and a PKCE code challenge.
  2. The user logs in at the provider (your app never sees the password) and approves the scopes.
  3. The provider redirects back to your redirect_uri with a short-lived authorization code.
  4. Your backend exchanges the code (plus the PKCE code verifier) for tokens at the token endpoint.

Why state and PKCE?

  • state is a random value your app remembers and checks when the user comes back. It proves the callback belongs to a login you started (CSRF protection).
  • PKCE (Proof Key for Code Exchange, "pixy") stops a stolen authorization code from being useful. Your app creates a secret verifier, sends only its SHA-256 challenge at the start, and must present the verifier to redeem the code:
Node.js · editable

Simulating the whole flow

Here a tiny authorization server and a client app run side by side, so you can watch every step, including an attacker trying to use a stolen code:

Node.js · editable

After step 4 your app usually creates its own session for the user, keyed by the provider's sub (the stable user ID; emails can change).

Other flows

Flow For
Authorization code + PKCE Users logging in: web, mobile, SPA. Use this.
Client credentials Machine-to-machine: a backend job calling an API as itself, no user
Device code TVs and command-line tools ("go to example.com/device and enter ABCD-1234")
Refresh token Getting a new access token without the user
Implicit, password Deprecated: don't use them

Doing it in practice

Don't implement OAuth by hand. Use a certified library for your stack (openid-client or Auth.js / Better Auth in Node, Authlib in Python, Spring Security OAuth2 Client in Java) or a hosted identity provider (Auth0, Okta, Microsoft Entra ID, AWS Cognito, Clerk) or a self-hosted one (Keycloak, Zitadel, Authentik). Your checklist:

  • Register exact redirect URIs with the provider (no wildcards).
  • Always use state and PKCE; for OIDC, also a nonce checked in the ID token.
  • Verify ID tokens fully: signature (keys from the provider's JWKS), iss, aud, exp.
  • Keep the client secret on the server only; SPAs and mobile apps are "public clients" and rely on PKCE.
  • Identify users by sub + issuer, not by email.

Practice

  1. In the simulation, make the token endpoint reject a mismatched redirect_uri.
  2. Add a nonce to the authorize request, have the provider put it in the ID token, and check it.
  3. Which flow would you use for a nightly job that copies invoices to an accounting system? Why?
✎ Check your understanding

1.What's the difference between OAuth 2.0 and OpenID Connect?

2.What does PKCE protect against?

3.What is the state parameter for?

← JSON Web Tokens