learning-it by example

#233 Enterprise architecture and AI agents 5.9 min

Delegated Authority: Let an AI Agent Act for a User, Safely

I tried eight ways to use an agent's token, and only the allowed one worked.

1. In an OAuth token exchange for delegation, what does the new token say about who is acting?
2. In the Python token-exchange demo, an agent got a delegated token for Ana. Of eight attempts to use or swap tokens, how many worked?
3. Ana's own token was passed straight to the orders service and was refused. Why?
4. How does the orders service treat a token whose body was changed after it was signed?
5. In the Python token-exchange demo, the delegated token lasts only five minutes. What is the reason for such a short life?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Look inside the delegated token

    Copy the code folder first. In compare.py, change the import line to also import read from tokens, then add the line print(read(mine)) just before the first attempt line. Run python compare.py. When I ran it, the first line printed the claims: act with the support-agent, aud orders-api, exp 1000300, scope with only read_orders, and sub ana. Compare the exp value with NOW to work out the lifetime in seconds.

    A hint

    NOW is 1000000. The authority adds the lifetime to the clock when it signs.

  2. Make the token last longer

    In authority.py, change the 300 in the exchange function to 600. Predict which of the eight attempts changes before you run python compare.py. When I ran it, the token used 6 minutes later was answered 200 instead of 401, and the summary said 2 of 8 attempts worked. Write one sentence on why the video keeps the lifetime short. Then change 600 back to 300 before the next task.

    A hint

    The expiry check is claims['exp'] <= now. The late attempt uses a clock of NOW plus 360 seconds.

  3. Ask for the refund scope

    In compare.py, change the list in the first exchange from ["read_orders"] to ["read_orders", "refund"]. Predict what happens to the refund attempt, then run python compare.py. When I ran it, the refund attempt was answered 200 with refund done for ana via support-agent, and 2 of 8 attempts worked. Think about whether Ana should have given that scope to the agent.

    A hint

    Ana's own token already holds the refund scope, so the exchange allows it.

  4. Remove a scope from Ana

    Starting from the change in the previous task, now remove "refund" from the scope list in user_token in authority.py. Predict what happens before you run python compare.py. When I ran it, the program stopped at the first exchange with ValueError: asked for scope the user does not have, before any attempt was printed.

    A hint

    The exchange checks that every requested scope is one that the user token holds.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

One authority, one user and eight cases I chose, with a made-up key and hand-made signing. The exchange trusts the agent's name and grants any scope the user has, for any service. No revocation, no user consent step, no real logins. Use a reviewed library in production.

Up next in Enterprise architecture and AI agents

More in Enterprise architecture and AI agents

All Enterprise architecture and AI agents videos

Related videos