Labs / Identity & Access / Tickets & trust

Lab 3 of 4 · Tickets & trust

SSO: one sign-in for every app

Sign in once, open a second app with no password, and see what apps check.

Start this lab 6 steps

The idea

A sailing school's two web apps keep no passwords at all. They send everyone to one sign-in service instead. Sign in there once, open both apps, and see what lets the second one skip the password, and what each app checks before it believes who you are.

The wiki sends the browser away
The wiki sends the browser away
The browser carries a code, not the token
The browser carries a code, not the token
The cookie is the single sign-on
The cookie is the single sign-on
Signed with one key, checked with the other
Signed with one key, checked with the other
A code goes to registered addresses only
A code goes to registered addresses only
Three sessions, ended one at a time
Three sessions, ended one at a time

Steps

  1. Two apps, one sign-in

    Tidewell is a sailing school with two web apps: a wiki for the instructors' notes, and a rota for who teaches when. Each used to keep its own accounts, so Noor, an instructor, had two passwords, and someone leaving had two accounts to close.

    Now both apps use single sign-on: one sign-in service, login.lab, does the authentication, checking who someone is, for both. It holds everyone's password, and it's the identity provider (IdP).

    The apps keep no passwords at all. They rely on login.lab to say who someone is, so they're called relying parties, and they talk OpenID Connect (OIDC), the protocol behind most "Sign in with…" buttons.

    The laptop is Noor's browser. Ask the wiki for its front page, with curl -sI: -I shows only the headers of the answer, where a redirect lives.

    Then read what login.lab publishes about itself, at a fixed address every OIDC provider has.

    laptop:~# curl -sI http://wiki.lab/
    laptop:~# curl -s http://login.lab/.well-known/openid-configuration
  2. Sign in at login.lab

    A browser keeps cookies, small values each site hands it and gets back on every later visit. They're how a site remembers you between pages.

    curl keeps them too, in a file: -c jar writes them to jar, and -b jar sends them back. -L follows redirects, as a browser does.

    Open the wiki that way. The redirect lands on login.lab's sign-in form, which posts a username and password to /login. Then post Noor's, with -d, to the form's address.

    That one post sets off three redirects, and -L follows them all:

    1. login.lab checks the password and remembers Noor's browser, with a cookie of its own;
    2. it sends the browser back to the wiki's redirect_uri with a code, good once for 60 seconds;
    3. the wiki takes the code to login.lab's token_endpoint itself, with its own secret, and gets an ID token: a signed note saying who Noor is.

    The wiki checks that note and lets her in. It never sees her password.

    laptop:~# curl -sL -b jar -c jar http://wiki.lab/
    laptop:~# curl -sL -b jar -c jar -d username=noor -d password=harbour-light-7 http://login.lab/login
  3. The second app

    Noor hasn't opened the rota yet today. Rota knows nothing about her: it keeps no passwords either, and its own cookies are empty.

    Look at the jar first, though. login.lab handed the browser a cookie when Noor signed in, called login_session, and the browser sends it with every visit to login.lab. Rota will send her browser to login.lab exactly as the wiki did.

    Open rota, and keep a record of the trip on the way: -D trip saves the headers of every answer to a file, trip, while the page prints as usual. Then pick the status lines and the redirects out of it with grep.

    laptop:~# grep login_session jar
    laptop:~# curl -sL -b jar -c jar -D trip http://rota.lab/
    laptop:~# grep -E '^(HTTP|Location)' trip
  4. What the wiki checks

    An ID token is a JSON Web Token (JWT): three parts, joined by dots.

    • A header, which names the key that signed it (kid).
    • The claims: who it's about (sub, email, name), who made it (iss), which app it's for (aud), when it was made (iat) and when it runs out (exp).
    • A signature.

    login.lab signs each token with its private key, which never leaves it. Anyone can check the signature with the matching public key, which login.lab publishes at its jwks_uri. A token with one character changed no longer matches its signature.

    So before the wiki believes a word, it checks:

    • state: the callback belongs to a sign-in this browser started;
    • the signature, with login.lab's public key;
    • iss: made by the sign-in service it expects;
    • aud: made for the wiki, not another app;
    • exp: not run out;
    • nonce: the value it sent with this very sign-in.

    The wiki writes each check to its log. Read it on wiki, then fetch the public key it used.

    wiki:~# cat /var/log/wiki.log
    laptop:~# curl -s http://login.lab/jwks
  5. A link that fishes for a code

    Single sign-on has a catch. While Noor is signed in at login.lab, any link to its /authorize page gets a code with no password asked. The code goes wherever the link's redirect_uri says.

    Say a message tells Noor her wiki session ran out, with a link to sign in again. The link really goes to login.lab, so it looks right, but its redirect_uri is http://catch.lab/callback, an attacker's server. If login.lab followed it, Noor's code would land there.

    So login.lab keeps a list of each app's addresses, given when the app was registered, and sends a code to those only. It's in /etc/login/clients.json on login, with each app's secret.

    Click the link as Noor's browser would, with her cookies. -b jar sends them, and with no -L you see login.lab's first answer.

    laptop:~# curl -s -b jar 'http://login.lab/authorize?client_id=wiki&redirect_uri=http://catch.lab/callback'
    login:~# cat /etc/login/clients.json
  6. Signing out

    Noor is signed in three times over now, one cookie each:

    • login.lab's session, which hands out codes;
    • the wiki's own session, wiki_session;
    • rota's, rota_session.

    Each app's /logout ends that app's session. login.lab has a /logout too, for its own.

    Sign out of the wiki first, then open it again. Then sign out of login.lab and open rota.

    laptop:~# curl -sL -b jar -c jar http://wiki.lab/logout
    laptop:~# curl -sL -b jar -c jar http://wiki.lab/
    laptop:~# curl -sL -b jar -c jar http://login.lab/logout
    laptop:~# curl -sL -b jar -c jar http://rota.lab/

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab