Skip to content
Ameen Abozaid
JNR-039

OAuth 2.0 and OIDC basics

  • 8 Lessons
  • 2.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Roles
  • flows
  • authorization server
  • resource server
  • login vs API authorization

Learn first

2.5 h of practice (estimate)

Learning outcomes

  1. Name the OAuth 2.0 roles (resource owner, client, authorization server, resource server).
  2. Explain the authorization code flow with PKCE and the client credentials flow, and when each applies.
  3. Distinguish OAuth 2.0 (delegated API access) from OIDC (login, ID tokens).
  4. Configure the Shop API as a resource server trusting an external authorization server.

Lessons

  1. 01The problem OAuth solves
  2. 02Roles and tokens
  3. 03Authorization code + PKCE
  4. 04Client credentials
  5. 05OIDC: adding login
  6. 06Spring as a resource server
  7. 07Demo: Keycloak setup and the authorization code + PKCE flow
  8. 08Demo: switching Shop to Keycloak-issued tokens

Lab and exercises

Lab

JNR-039-LAB1 · External identity

Outcomes: 1–4 · Time: ~2.5 h Run Keycloak locally, obtain tokens with Postman, and switch the Shop API to validate Keycloak-issued tokens, mapping realm roles to authorities. Acceptance: self-issued tokens from JNR-038 are no longer accepted.

Exercises (assessment: flow-selection scenarios)

IDOutcomeTask
JNR-039-EX011Name the four OAuth roles in this setup: a user, the Shop web app, Keycloak, the Shop API.
JNR-039-EX022Choose the flow: (a) a single-page app where users log in; (b) a nightly job calling an internal API; (c) a mobile app.
JNR-039-EX032Explain in your own words what PKCE protects against.
JNR-039-EX043Fill in a table of access token vs ID token: purpose, audience, sent to the API or not.
JNR-039-EX053A team sends the ID token to the API as a bearer token. Explain the problem.
JNR-039-EX064Write the YAML and converter to map realm_access.roles to ROLE_* authorities.
JNR-039-EX072Say why the implicit and password grants should not be used in new systems.
Show solutions
  • EX01 User: resource owner; Shop web app: client; Keycloak: authorization server; Shop API: resource server.
  • EX02 (a) authorization code + PKCE; (b) client credentials; (c) authorization code + PKCE.
  • EX03 A stolen authorization code cannot be exchanged for tokens without the original code_verifier.
  • EX04 Access token: authorizes API calls, audience is the API, sent to the API. ID token: tells the client who logged in, audience is the client, never sent to the API.
  • EX05 The ID token is meant for the client; its audience is wrong for the API, and it is not designed to authorize access.
  • EX06 Set issuer-uri; in a JwtAuthenticationConverter read the realm_access claim's roles list and map each to new SimpleGrantedAuthority("ROLE_" + role.toUpperCase()).
  • EX07 Implicit exposes tokens in the browser URL; password grant makes the client handle user passwords. Both are discouraged by current best-practice guidance (verify before recording).

Quiz

Outcomes: O1 roles · O2 flows and PKCE · O3 OAuth vs OIDC · O4 resource server configuration.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1The server that logs the user in and issues tokens is the:O1
  2. 2In our setup the Shop API is the:O1
  3. 3For a user logging in to a single-page app, use:O2
  4. 4For a back-end job with no user, use:O2
  5. 5PKCE protects against:O2
  6. 6OIDC adds to OAuth 2.0:O3
  7. 7The ID token should be sent to:O3
  8. 8OAuth 2.0 by itself is mainly about:O3
  9. 9issuer-uri lets Spring:O4
  10. 10Mapping realm roles to authorities is done with:O4

Practical part

Seven exercises JNR-039-EX01 to EX07 plus the lab; the flow-selection scenarios (EX02, EX07) are the assessment. Pass: at least six exercises correct, correct flow chosen in every scenario, and old self-issued tokens rejected.

Common mistakes

The implicit or password grant in new systems; using ID tokens to call APIs.

Assessment

Flow-selection scenarios.

Career relevance

Advanced API security (MID-051) and identity federation (ARC-027).

Type to search levels, phases, modules and lessons.