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 base64urlA way of encoding data as ordinary text characters so it can travel safely in headers, URLs, and JSON. Anyone can decode it, so it hides nothing; Base64url is a variant safe for URLs, used in JWTs., hashTurning data into a short, fixed-size value called a hash, in a way that cannot be reversed and where changing one character changes the result completely. Services store password hashes instead of passwords, and signatures are made over a hash. a value and encode the digest, look inside a tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret., send a request with your key. You have already written most of them, one lab at a time:
| Function | What it does | Written in |
|---|---|---|
labnet | Sends a request to the practice service with your lab key | Writing a small script |
b64url | Encodes its input as base64url without padding | Encoding and hashing |
b64url_decode | Turns base64url text back into the original bytes | Encoding and hashing |
sha256_b64url | Hashes its input with SHA-256 and encodes the digest as base64url | This lesson, from the pipeline in Encoding and hashing |
random_value | Prints a fresh random value as a line of shell that sets the variable you name | This 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 '='
}
EOFThe 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-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQRun 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 b64urltype 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}"
43The 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' >> ~/.bashrctouch 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 protocolAn agreed set of rules for the messages two systems exchange and what each message means. HTTP, OAuth 2.0, OpenID Connect and SAML are all protocols. 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.mjsAs 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 signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message. 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-encodingWriting a character as % followed by a two-character code, such as %2F for /, so a value inside an address or form body is not mistaken for part of its structure. The receiving service decodes it before use. that curl --data-urlencode sent in Encoding and hashing.
The comparison that matters most is reading a token. A signed JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing., 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 JSONA text format for structured information, written as named values inside curly braces. APIs, tokens, and discovery documents all use it.. 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 issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts.'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.
Try it in the Lab
- Collect your functions into a toolkit, then install and check the lab toolkitGather the functions you wrote in Technical foundations into one tested file, load it in every terminal, install Node.js and btl-lab with checked digests, and compare the toolkit's answers with your own.