Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Level 1 challenge: connect an application and an API to Keycloak

11 min read
Improve this page

This challenge closes level 1: you deploy a complete identity provider from a brief, with no commands in front of you, then you prove each setting by the state of the system. It draws on the installation of Keycloak on PostgreSQL, LDAP federation, an application using OIDC with PKCE and the sample API protected by audience and scopes. Allow an hour and a half. It is meant for those who have completed the modules "First identity provider", "OAuth 2 and OpenID Connect" and "Federation and directories".

What you will learn

The challenge teaches no new gesture: it teaches you to combine on your own the ones from the three level 1 Keycloak modules, brought together in a single scenario and graded on their proofs. You will be able to:

  • Install a durable Keycloak that keeps its realms and clients after a complete restart
  • Federate an LDAP directory read-only and authenticate a directory user through it
  • Declare a confidential application with an exact redirect URI and PKCE enforced
  • Protect the sample API with audience and scopes, and give a service read-only access
  • Prove each constraint with a refusal or an observable state

The brief

Atelier Mercier, a joinery workshop of forty people, wants to stop managing one password per application. Its accounts already live in an LDAP directory (the LLDAP from the course will do, or any directory for which you have a read account). Management wants a single identity provider that the team can stop and restart without losing anything.

Two consumers must connect to it. The internal portal is a web application with a backend, served in development at http://localhost:3000 and receiving the user at http://localhost:3000/callback. The site notes API is the sample API from the lesson Protect an API with access tokens (in French), unmodified: it reads its scopes notes:lecture and notes:ecriture, and its audience notes-api. A nightly job, inventaire, must read the notes with no user and must never be able to write them.

Nobody dictates the commands. You choose the path, console or kcadm.sh; only what the system shows at the end counts.

The constraints

Each constraint has an associated proof, and the proof is what counts, not the statement. A configuration read in the console says what you meant to do; a refusal obtained with the wrong token says what the system actually does. The table sets, for each one, the expected observable.

#ConstraintExpected proof
1Keycloak stores everything in PostgreSQL and survives a complete shutdown of the containers, volume keptthe realm, its users and its clients read back after a shutdown and a restart
2A dedicated realm atelier holds everything; master is only for administrationthe list of master clients, unchanged by the challenge
3The directory is federated read-only, with the entryUUID anchora directory user listed with their federation link, and a token obtained with their directory password
4The portal is a confidential client, in Authorization Code only, with an exact redirect URI and PKCE S256 enforcedthree responses: 200 with an S256 code_challenge, refusal without code_challenge, 400 on /callback/
5The API rejects a token that is not meant for it and only allows writing with the write scope401 with Audience doesn't match, 200 for reading, 403 insufficient_scope for writing
6The inventaire service reads with no user, cannot write, and its secret never appears on the command line200 for reading and 403 for writing with its token, refresh_expires_in at 0, secret file in 600
7The realm events are recordedthe LOGIN_ERROR event from the refusal in constraint 4, with the redirect_uri value received

An eighth requirement is settled with a sentence: explain why a token issued for a directory user without the notes:lecture scope is perfectly signed and yet rejected by the API, and what an API that only checked the signature would let through.

What you must deliver

A file of five items, readable by someone who did not attend the session. The format is free, the content is not.

  • The Compose file for Keycloak and PostgreSQL, images pinned by digest, with no clear-text secret other than the lab passwords flagged as such.
  • The configuration script for the atelier realm, if it was done on the command line, or the ordered list of console screens if you did it by hand: a colleague must be able to replay it.
  • The seven proofs from the table, as copied and dated command outputs.
  • The written answer to the eighth requirement, backed by a decoded token.
  • The cleanup: containers, volume and secret files removed, with the command for each removal.

How to grade yourself

The grid is read line by line, and a zero on constraints 4, 5 or 6 makes the rest secondary: an application without PKCE, an API that accepts a token from another audience or a service that can write are not "almost" secure.

CriterionWhat earns the pointsPoints
Constraints 1 to 7 provedeach proof is a dated command output tied to its constraint7 × 2
Eighth requirementthe answer relies on the aud of a decoded token2
Replayabilitythe Compose file and the script recreate the same state on a clean machine2
Cleanupnothing remains, secret included2

Threshold: 14 out of 20, with no zero on lines 4, 5 and 6. Below it, replay the challenge a week later without rereading the solution in between. Above it, level 1 is achieved: level 2 assumes this identity provider is up and running.

The solution, folded

Open this block only after delivering, or after half an hour of real blockage. It contains no new command: each gesture points to the lesson that replayed it on Keycloak 26.7.0, and only the checks are recalled.

The plan, the traps and the checks

Constraint 1, a durable Keycloak. The Compose file from Install Keycloak with Docker (in French) is the basis: PostgreSQL with its healthcheck, Keycloak waiting for service_healthy, a named volume for the data. The proof is a docker compose down without -v, a docker compose up -d, then reading back the realm, users and clients. The trap is the opposite one: down -v destroys the volume, and with it everything the constraint asks you to keep.

Constraints 2 and 3, the realm and the directory. The atelier realm is created as in Administer Keycloak, the federation as in Federate an LDAP or AD directory (in French): READ_ONLY mode, entryUUID anchor, full synchronization, then the user list showing the federationLink field of the accounts that came from the directory. The token obtained with the directory password proves the delegation; it requires a test client using the password flow, to be deleted afterwards. A common trap is the network: seen from the Keycloak container, localhost means the container itself, not the machine running LLDAP.

Constraint 4, the portal. The client is declared as in Secure an application with Keycloak, with PKCE enabled in the Settings tab, Capability config section (Require PKCE switch, S256 method), or through the pkce.code.challenge.method attribute. The three responses are read on the authorization address, called with curl: with an S256 code_challenge, the login page answers 200; without it, Keycloak sends back to the application a redirect carrying error=invalid_request and Missing parameter: code_challenge_method; with /callback/, it displays Invalid parameter: redirect_uri with a 400.

Constraint 5, the API. Everything is in Protect an API with access tokens (in French): notes-api client with no flow, client scopes notes:lecture and notes:ecriture each carrying the audience mapper, API started with ISSUER and AUDIENCE. The token requested without a scope gives 401 and Audience doesn't match, the one with notes:lecture gives 200 for reading and 403 for writing. The API's issuer must be exactly the one in the discovery document: localhost and 127.0.0.1 are not interchangeable, as the lesson Troubleshoot an integration (in French) shows.

Constraint 6, the service. Same lesson, section on machine access: confidential client with a service account, notes:lecture alone as default scope, secret stored by a subshell under umask 077 and read by curl with --data-urlencode "client_secret@fichier". The response announces "refresh_expires_in": 0: the client credentials flow does not issue a refresh token.

Constraint 7, the events. A new realm does not record them: kcadm.sh get events returns [ ] there. Enable eventsEnabled before triggering the refusal from constraint 4, then read the latest LOGIN_ERROR: it carries invalid_redirect_uri and, in details, the address received. The full method is in Troubleshoot an integration (in French).

The eighth requirement. Decoded, the token without a scope carries "aud":"account": Keycloak signed it without error, but for another audience. An API that only checks the signature would accept it, which is what the sigonly.py script from the API lesson establishes by disabling only verify_aud.

Key takeaways