A token, the usual way
The usual form of such a token is a JSON Web Token, a JWT. It is three pieces of text joined by dots:
- A header, in JSON: which algorithm made the signature, and with which key.
- The claims, in JSON: who the token is about, who issued it, when it expires, what it allows.
- A signature over the first two pieces, made with a secret key.
Each piece is then written in base64, an alphabet of 64 harmless characters, so that the whole token can travel anywhere text can. Base64 writes every three bytes as four characters.
Below is such a token, and the same claims as a Dagr graph. Change the values and watch both.
The claims are the same and the signature is the same kind, 32 bytes from the same algorithm. What differs is everything around them.
- Names. The JWT's claims are JSON, so
"sub","iss","exp"are in every token. That is why the standard gave them three-letter names. The Dagr claims are a packed node from chapter 6: a tag and a value each, and the schema holds the names. - Numbers. A time is ten digits of text in the JWT and five bytes in Dagr.
- Base64, twice over. The JWT's signature is 32 bytes and takes 43 characters. Its JSON is text already and is still put through base64, which makes it a third longer again.
The Dagr token is binary. If it has to travel where only text can, in a web request's header for example, it needs base64 too, once, over the whole token. The panel shows that size as well, because that is the fair comparison there.
Where the signature goes
Every Dagr buffer in this guide has started with a framing byte, and until now it was a dull one. Its two lowest bits say what follows. One of them means: before the data, there is a header. This is a header for the whole buffer, and not the byte or two in front of a union's value that chapter 8 called by the same name.
Point at the first bytes of the Dagr token above. After the framing comes the header, a small packed node with a length in front, and after the header come the claims, exactly as they would be without one.
TOKEN = DataGraph("Token", root_type=t.ref("Claims"),
node_types=[
Node("Claims", fields=[
"subject" >> t.utf8 >> required,
"issuer" >> t.utf8 >> required,
"issuedAt" >> t.u64 >> required,
"expiresAt" >> t.u64 >> required,
"scopes" >> t.utf8.array >> required,
], packed=True),
],
header=Node("Signature", fields=[
"algorithm" >> t.utf8 >> required,
"keyId" >> t.utf8 >> required,
"signature" >> t.data >> required,
]))
The header is a node like any other, and what goes in it is up to you. Dagr brings no cryptography. When writing, it hands your code the finished bytes of the data and asks for the header to put in front. Your code computes a signature over those bytes, with whatever algorithm you chose, and returns it.
That this works at all rests on something from chapter 4: every pointer in Dagr is a distance, never an address. Nothing inside the data knows where the data begins, so bytes can be put in front of it without changing one byte of it. A header can be added, replaced or taken away, and the signed bytes stay what they were.
Check first, read afterwards
The other half is reading. A token arrives from a stranger. Its contents are not to be believed, and ideally not even looked at, until the signature has been checked. Dagr's reader takes a function of yours that is given the header and the bytes of the data, and decides. Only if it agrees is the data read at all.
Here is a verifier doing that. It holds one key, named k1, and accepts one algorithm. Try to get a changed token past it: use the buttons, or click any byte to change it.
- A changed claim is caught before it is read. The signature is computed over the bytes as they arrived. Change one bit of the claims and it no longer matches, and the faded bytes show that the verifier never went on to decode them.
- The header is not believed either. The signature covers the claims, not the header. So the verifier does not ask the header what to do: it accepts the algorithm it was built for and the key it holds, and refuses anything else. Early JWT libraries famously did ask, and accepted tokens whose header said the algorithm was
none. - A genuine token can still be refused. Expiry is a claim, so it can only be judged after the claims are trusted and read. That is the one refusal that happens after the gate.
The same bytes every time
Chapter 10 ended with a warning: the same value can be encoded in several ways, and a signature is over bytes, not over values. There are two ways out of that, and tokens use the simple one. A verifier never encodes anything. It checks the bytes it received, and reads the claims from those same bytes. A JWT does this by carrying the exact text that was signed. A Dagr token does it by signing the bytes of the data as they are, and reading them in place.
The harder way out is to demand that every writer produce identical bytes for the same value. Dagr's writers are held to that, by tests that compare the output of all of them byte for byte, and it matters as soon as two parties each compute a hash or a signature of what should be the same data.
Next to the alternatives
- Against a JWT, the Dagr token is clearly smaller, less than half as bytes and about three fifths when both are sent as text, and a verifier has no JSON and no base64 to work through.
- Against a CBOR token, which is a real standard too (CWT, signed with COSE), the size advantage mostly disappears. CBOR tokens already use small numbers as names and need no base64. What is left is the reading: the Dagr claims are used where they lie.
- What both of those have and a Dagr token does not is a standard, libraries in every language written by people who think about attacks for a living, and tools that will show you the inside of any token. A Dagr token means something only to programs that share its schema. Inside one system, between your own services, that can be a fine trade. For logging in to other people's services, it is not on offer.
A fuller version of this token, with free-form claims using the union from chapter 10, exists as a project of its own. It builds the token in ten languages, checks that all ten produce the same bytes and can verify each other's, and measures it against JWT and CBOR tokens: dagr-signed-token.
Headers are not only for signatures
A header is a place for anything that should be known about the data before the data is read:
- What it is. A fingerprint of the schema or a version number, so that a program can pick the right reader, or refuse politely.
- Where it should go. A router can read a small header and pass the rest on without understanding it.
- Who made it, and when.
Because the header is a packed node, it can grow new fields later, and a tool that knows nothing about your schema can still see from the first byte that a header is there, and step over it by its length.
What these panels are.
- Everything is computed in your browser, the signatures included: they are real HMAC-SHA256. The JWT is checked against Python's own cryptography and JSON, and the Dagr token, header and all, against Dagr's reference writer: identical.
- The verifier is checked too. For a range of tokens, every single byte was changed in turn and every possible cut-off tried. It refused all of them.
- The key in the page is not a secret, and a secret typed into a web page should not be one you use anywhere.
- The times are fixed: the token is issued at a set moment and expires an hour later.
Try it
- In the first panel, change one letter of the secret key. Which bytes of the Dagr token change, and which do not?
- Add a scope. By how many characters does the JWT grow, and by how many bytes the Dagr token? Where does the difference come from?
- In the second panel, click a byte of the key's name in the header. The signature is still correct. Why is the token refused anyway, and should it be?
- Click a byte, then click the same byte again. What does the verifier say now?
- Find the earliest byte you can change so that the verifier refuses without computing a signature at all.
What comes next
A token is one small message. Chapter 12 is about the opposite: data that never ends, such as a log or a stream of events, where records are added at the end forever and a reader often wants only the newest ones. Dagr's name for that is a DataSink.
