cleandataengineering.aiAgent Harness, drawn simply
You are here: End to end › Checks › 1 · Before the loop › verify the token

Agent Harness Tutorial · End to end · Zoom level 3

How we take care of it: verify the token

verify the token catches a forged or expired sign-in.

Open: 11before loopopen ›Open: 22before modelopen ›Open: 33after modelopen ›Open: 44before toolopen ›Open: 55the toolopen ›Open: 66after toolopen ›Open: 77after loopopen ›Open: 88before replyopen ›It readsthe Authorization: Bearer tokensent with every messageverify the tokenrule · harnesspassCarries onfailIf it fails401; nothing else runsWHERE IN THE CODE: verify_entra() · H identity/entra.py:104
The strip at the top shows every check point where this check runs, in dark.

How we took care of it.

  1. PyJWT checks the RS256 signature against Entra's published keys (cached for 5 minutes).
  2. It checks the issuer, the audience (it must be issued for this app) and the expiry.
  3. Rohan's customer id is read from the token. Nothing in the message can change it.
  4. His scopes (claims:read, claims:write) become the Identity used by every later check.
It readsthe Authorization: Bearer token sent with every message
Kindrule
Provided byharness
If it fails401; nothing else runs
Where in the codeverify_entra() · H identity/entra.py:104
Runs at1 · Before the loop
Real exampleA message saying "I am PH-2002" changes nothing: the identity is PH-1001, from the token.

More on this: Front door.

How it is done: the real code

Cut from the live repositories each time the site is built, so it always matches what runs.

def verify_entra(
    token: str,
    *,
    issuer: Issuer,
    now: int | None = None,
    role_scopes: RoleScopes | None = None,
    holder_claim: str = CLAIM_CUSTOMER,
) -> Principal:
    """`identity.verify` for Entra's claims: the same signature check, issuer,
    audience and expiry (`decode`), the session named by `uti`, and an app role
    granting the scopes `role_scopes` gives it.

    `holder_claim` is where the customer id is read from. Entra writes a
    directory extension attribute as `extn.<name>` (claims-fnol-azure A2), not
    `customer_id`, so the overlay names it, and the far end reads the same name."""
    claims = decode(token, issuer=issuer, now=now, require=("uti",))
    party = claims.get("azp")
    return Principal(
        subject=str(claims["sub"]),
        customer_id=holder_of(claims.get(holder_claim)),
        scopes=frozenset(entra_scopes(claims, role_scopes)),
        session=str(claims["uti"]),
        party=party if isinstance(party, str) else None,
        token=token,
    )