Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Authentication Methods  ›  Capstone Project

OAuth 2.0 and OpenID Connect

OAuth 2.0 16 minOAuth 2.0 lets a user grant an app limited, scoped access to an API without sharing their password, and OpenID Connect adds an ID token so the app also learns who the user is.

OAuth 2.0 and OpenID Connect

A reading-tracker app, ShelfLog, wants to show a member's BookNook loans. The old, bad answer was to ask the member for their BookNook password. OAuth 2.0 lets the member grant ShelfLog limited access, "read my loans", without ever sharing the password, and OpenID Connect adds a way for ShelfLog to learn who the member is.

The rule

OAuth grants access; OpenID Connect proves identity.

OAuth 2.0 has four roles:

  • Resource owner: the member, who owns the loan data.
  • Client: ShelfLog, the app that wants access.
  • Authorization server: BookNook's sign-in service, which asks the member and issues tokens.
  • Resource server: the BookNook API, which accepts the tokens.

Access is limited by scopes, named permissions such as loans:read. The member sees exactly what is requested and can refuse.

The recommended flow for apps is the authorization code flow with PKCE:

  1. ShelfLog sends the member's browser to BookNook's authorization server, asking for scope=loans:read, with a hashed one-time secret (the PKCE challenge).
  2. The member signs in at BookNook and approves.
  3. BookNook redirects back to ShelfLog with a short-lived authorization code.
  4. ShelfLog exchanges the code, plus the original unhashed secret, for an access token (and often a refresh token).
  5. ShelfLog calls the API with Authorization: Bearer <token>.
GET /authorize?response_type=code&client_id=shelflog&scope=openid%20loans:read
    &redirect_uri=https://shelflog.example/callback&code_challenge=...&state=xyz

The state value lets ShelfLog confirm the redirect answers its own request.

OAuth only says "this token may read loans". It does not define who the user is. OpenID Connect (OIDC) is a thin layer on top: request the openid scope and you also receive an ID token, a JWT with claims like sub (a stable user id), email and name, meant for the client to read. That is what powers "Sign in with..." buttons.

  • Access token: for the API. The client should treat it as opaque.
  • ID token: for the client, to know who signed in. Do not send it to APIs as an access token.

The shortcut

For any sign-in design, ask two separate questions: "What may this app do?" → OAuth scopes and an access token. "Who is the user?" → OIDC and an ID token.

ShelfLog connects to BookNookStep 1 of 4
  1. What may ShelfLog do?

    Read loans only: request scope=loans:read. The member approves it on BookNook's own page.

Drills

Answer fast — trust the rule.

quick drill

What problem does OAuth 2.0 mainly solve?

quick drill

In the ShelfLog story, which role does the BookNook API play?

quick drill

What does OpenID Connect add on top of OAuth 2.0?

quick drill

ShelfLog should only read loans, never renew them. What limits it?

quick drill

A developer sends the ID token to the BookNook API as its bearer token. What is wrong?

Remember

  • OAuth grants access; OpenID Connect proves identity.
  • Shortcut: "what may this app do?" → scopes and access token; "who is the user?" → ID token.
  • Trap: sharing passwords with third-party apps, and using ID tokens as access tokens.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainConnect a third-party app

Walk through how the ShelfLog app gets read-only access to a member's BookNook loans and shows the member's name, naming the tokens involved and where each one goes.