Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Building your identity lab toolkit

You have already written several small functions that identity work uses repeatedly. Now you will collect them into a toolkit every terminal can load, test it against known answers, and use the same checks to assess the ready-made toolkit in later labs.

Why keep a toolkit

Once you start reading tokens and following sign-in flows, the same handful of steps keeps coming back: encode some bytes as base64url, hash a value and encode the digest, look inside a token, send a request with your key. You have already written most of them, one lab at a time:

FunctionWhat it doesWritten in
labnetSends a request to the practice service with your lab keyWriting a small script
b64urlEncodes its input as base64url without paddingEncoding and hashing
b64url_decodeTurns base64url text back into the original bytesEncoding and hashing
sha256_b64urlHashes its input with SHA-256 and encodes the digest as base64urlThis lesson, from the pipeline in Encoding and hashing
random_valuePrints a fresh random value as a line of shell that sets the variable you nameThis lesson

Each lab so far wrote its functions again in a fresh shell, so each lab could be started on its own. Retyping them is slow, and the mistakes it invites are hard to see: one wrong character in a tr set, or one stray newline in the input, gives a different answer that looks just as plausible as the right one. The fix is to keep each function in one file, check it once against a value you know is correct, and load the same file everywhere.

A file of functions

The file is ~/btl-course/toolkit.sh, written with a here-document as in Connecting commands with pipes and redirection. The quotes in <<'EOF' matter more than ever here, because every function is full of $ signs and $( ). Without them, the shell would fill those in while writing the file, and the functions would be broken. The start of the file:

cat > ~/btl-course/toolkit.sh <<'EOF'
# toolkit.sh: shell helpers for identity labs.
# Load it with: source ~/btl-course/toolkit.sh

b64url() {
  # Encode standard input as base64url without padding.
  openssl base64 -A | tr '+/' '-_' | tr -d '='
}
EOF

The other functions follow in the same way. Two are new. sha256_b64url is the digest pipeline from Encoding and hashing with a name: openssl dgst -sha256 -binary prints the 32 raw bytes of the digest, and b64url encodes them. A function can call another function, as long as both are loaded.

sha256_b64url() {
  # Hash standard input with SHA-256 and print the digest as base64url.
  openssl dgst -sha256 -binary | b64url
}

random_value makes a value nobody can guess, of the kind sign-in requests use to tie a response to the request that caused it. openssl rand 32 prints 32 bytes from a cryptographically secure random generator, and b64url turns them into 43 characters:

random_value() {
  # random_value NAME: print an export line that sets NAME to a fresh random value.
  local name="$1"
  echo "export $name=$(openssl rand 32 | b64url)"
}

It does not set the variable itself. It prints a line of shell, export STATE=... when you call random_value STATE, and a later section explains why.

A file of functions is only worth trusting once each function gives an answer you already know. The known values from Encoding and hashing still work: Ready? Go! encodes to UmVhZHk_IEdvIQ and decodes back, and the SHA-256 digest of hello is:

$ printf '%s' 'hello' | sha256_b64url; echo
LPJNul-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ

Run the same tests after every change to the file. They take a few seconds and catch most mistakes, and bash -n ~/btl-course/toolkit.sh checks the whole file for syntax errors without running anything.

Sourcing a file or running it

A file of functions is loaded with source, the way Variables, quoting and the environment loaded a file of settings:

source ~/btl-course/toolkit.sh
type b64url

type says what a name means to the shell. Here it confirms that b64url is a function and prints its definition. Running the file with bash ~/btl-course/toolkit.sh instead would start a separate shell, which defines the functions and then exits, taking them with it; type b64url would then answer not found.

For the same reason, the file does not start with set -euo pipefail the way a script does. A script runs in a shell of its own, so stopping at the first failure ends only that shell. Sourced lines run in yours, and with -e set, the first command that failed at your prompt would close the terminal window.

Sourcing also means trusting. source runs every line of a file as a command in your shell, which is why Time and timestamps read the values out of a downloaded file with grep and cut instead. Source only files you wrote, or have read and understood.

Handing values back with eval

A separate program cannot change your shell's variables: when it ends, anything it set ends with it. So command-line tools that make values for you often print them as lines of shell instead, in the form export NAME=value, and leave you to run those lines. random_value works the same way, so it behaves like the tools you will use later.

eval runs text as shell commands. Wrapped around $( ), it runs whatever the command printed:

$ random_value STATE
export STATE=W3n2k8u0V4o1tq8ZbQe9y3H2mLr5cXa7JpD6sFg0kNw
$ eval "$(random_value STATE)"
$ echo "${#STATE}"
43

The first command only prints. The second runs random_value again, which makes a new value, and eval runs the line it printed, so STATE now holds 43 characters. Printing first lets you see exactly what will run before anything changes.

That is also the danger. eval runs whatever text it receives, with all of your shell's access, so a printed line that contained ; rm -rf ~ would run that too. Use eval "$(...)" only on output from a source you trust: your own function, or a tool whose download you checked. Never use it on text that came from a server, a file someone sent you or a web page.

Loading it in every terminal

Functions you source last only as long as the shell that loaded them. To have them in every new terminal, add a line to ~/.bashrc, the file an interactive bash sources when it starts. As Variables, quoting and the environment explained, Ubuntu's login shell reads ~/.bashrc through ~/.profile, and on macOS the bash you start by typing bash is not a login shell, so in this course ~/.bashrc is always read:

touch ~/.bashrc
grep -qF 'btl-course/toolkit.sh' ~/.bashrc || echo '[ -f ~/btl-course/toolkit.sh ] && source ~/btl-course/toolkit.sh' >> ~/.bashrc

touch creates ~/.bashrc if it does not exist yet, which is common on macOS. Read the second line from the left. grep -q prints nothing and reports only through its exit status whether it found a match, and -F treats the pattern as plain text rather than a pattern with special characters. If the line is already there, || stops, so running the command twice never adds a second copy. Otherwise echo appends the line. The single quotes keep ~ as written, so it is expanded each time bash starts, and the [ -f ... ] test checks that the file exists first, so a moved or deleted toolkit does not print an error in every new window.

Open a new terminal and run type b64url to confirm it loaded. When you change the file later, run source ~/btl-course/toolkit.sh in any terminal that is already open, then the known-value tests again.

Installing the lab toolkit

Your functions cover encoding, hashing and practice requests. The protocol work in later labs needs much more: building sign-in requests, catching the answers, and checking tokens. For that the labs use btl-lab, the lab toolkit: one file, btl-lab.mjs, written in JavaScript. It runs on Node.js, a program that runs JavaScript outside a browser, version 22 or later. Ubuntu 24.04's own package is version 18, so the lab installs the official Node.js 22 archive from nodejs.org on Ubuntu, and Homebrew's node@22 on macOS. The browser workspace already has Node.js 22, so there the lab checks its version instead of downloading it.

Anything you download and then run deserves a check first. Publishers list the SHA-256 digest of each file they offer: nodejs.org in a file called SHASUMS256.txt, and this site on the Lab toolkit page. Compare the digest of your copy with the listed one before you run the file, never after:

openssl dgst -sha256 btl-lab.mjs

As What a digest proves explained, a match shows that your copy has the bytes the listed digest describes, and the check is only as trustworthy as the place the digest came from. A digest fetched from the same site as the file catches a damaged or incomplete download, but someone who could change the file on that site could change its digest too. Node.js also signs its list of digests, and checking that signature with the Node.js release keys, which are published separately on GitHub, shows that the list came from the Node.js release team; the lab shows the commands as an optional step.

To run the toolkit by name, the lab saves it as ~/btl-lab.mjs and writes a two-line script called btl-lab into ~/btl-course/bin:

#!/usr/bin/env bash
node "$HOME/btl-lab.mjs" "$@"

"$@" stands for all of the script's arguments, each kept as one word, as in Writing a small script, so btl-lab decode "$TOKEN" passes decode and the token on to the toolkit unchanged. A second line in ~/.bashrc, added the same way as the toolkit's, puts ~/btl-course/bin on your PATH, so btl-lab works in every terminal and in your own scripts.

Your functions and the toolkit

Several toolkit commands do what you have already done by hand, which makes them easy to check. btl-lab state prints export STATE=... and export NONCE=..., two values made like yours from 32 random bytes, for you to run with eval "$(btl-lab state)". btl-lab urlencode '1+1=2' prints 1%2B1%3D2, the percent-encoding that curl --data-urlencode sent in Encoding and hashing.

The comparison that matters most is reading a token. A signed JWT, from Keys, encryption and signatures, is three base64url parts joined by dots: a header, a payload of claims, and a signature. Your own functions can read the payload:

TOKEN=eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2xvZ2luLmV4YW1wbGUuY29tIiwic3ViIjoiYXZhIiwiYXVkIjoicGhvdG9zLmV4YW1wbGUiLCJleHAiOjE3OTEyMTYwMDB9.cGxhY2Vob2xkZXItc2lnbmF0dXJl
printf '%s' "$TOKEN" | cut -d. -f2 | b64url_decode | jq .
{
  "iss": "https://login.example.com",
  "sub": "ava",
  "aud": "photos.example",
  "exp": 1791216000
}

cut -d. -f2 keeps the second dot-separated part, and jq . prints the decoded JSON. btl-lab decode --json "$TOKEN" prints the same payload, and plain btl-lab decode "$TOKEN" adds the header and writes the times in ISO 8601.

Neither of them checked anything. This token's third part is a placeholder, not a real signature, and its exp is 2026-10-05T16:00:00Z, a moment that has already passed, yet it decoded without complaint. That is why decode ends with Decoded, not validated. Anyone can write a token that decodes, so reading one tells you only what it claims. Trusting it takes checking its signature with the issuer's key, its issuer, its audience and its times, which is what the toolkit's verify does and what the identity lessons explain.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

You download a tool and its SHA-256 digest from the same website, and the digest of your copy matches. What does the match show? 1 of 4

Choose one answer.

You run bash ~/btl-course/toolkit.sh, and then b64url answers command not found. What happened? 2 of 4

Choose one answer.

btl-lab state prints two lines that start with export STATE= and export NONCE=. Why do the labs run it as eval "$(btl-lab state)" instead of on its own? 3 of 4

Choose one answer.

btl-lab decode shows a token whose iss is your tenant, whose sub is ava and whose exp is an hour from now. What has that established? 4 of 4

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity