Repository navigation
whoami and describe: answer the caller this app actually serves #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5eaeef6
fix: whoami's 401 carries the credential hint
korivi-CraftOS 0d1eced
Merge remote-tracking branch 'origin/main' into korivi-whoami-401-hint
korivi-CraftOS 4baf6d6
Answer the caller this app actually serves
korivi-CraftOS 807af6f
Make the blueprint suite assert what the blueprints actually are
korivi-CraftOS fa781f1
Merge remote-tracking branch 'origin/main' into korivi-whoami-401-hint
korivi-CraftOS 049e576
Merge remote-tracking branch 'origin/korivi-blueprint-specs' into kor…
korivi-CraftOS 47d9483
Revert the FastAPI blueprint's spec; its author writes it
korivi-CraftOS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** | ||
| * whoami answers whoever the app will actually serve. | ||
| * | ||
| * The conformance suite covers the single-user side, where an uncredentialled | ||
| * caller reads records and whoami must say so. The multi-user side cannot live | ||
| * there: that harness boots one reference app, and it is `authMode: "none"`. | ||
| * This is the other half — an app where refusing an uncredentialled caller is | ||
| * the honest answer, so the 401 has to survive, and has to keep carrying the | ||
| * `how` that tells the caller where a credential comes from. | ||
| * | ||
| * Standard library only, run directly. | ||
| */ | ||
| import { createA2App, MemoryBinding } from "../dist/index.js"; | ||
|
|
||
| const failures = []; | ||
| const check = (label, got, want) => { | ||
| const g = JSON.stringify(got); | ||
| const w = JSON.stringify(want); | ||
| if (g !== w) failures.push(`${label}\n expected: ${w}\n actual: ${g}`); | ||
| }; | ||
| const ok = (label, cond) => { | ||
| if (!cond) failures.push(label); | ||
| }; | ||
|
|
||
| const entities = { | ||
| notes: { module: "desk", fields: [{ name: "title", type: "string", required: true }], seed: [] }, | ||
| }; | ||
| const operations = [ | ||
| { name: "count-notes", description: "Count them.", destructive: false, readOnly: true, module: "desk", params: {} }, | ||
| { name: "wipe-notes", description: "Delete them all.", destructive: true, module: "desk", params: {} }, | ||
| ]; | ||
|
|
||
| const build = (authMode) => | ||
| createA2App(new MemoryBinding({ appId: "whoami_test", appName: "Whoami Test", entities, authMode }), { | ||
| modules: [{ name: "desk", summary: "notes" }], | ||
| operations, | ||
| credentialHint: "Ask the owner.", | ||
| }); | ||
|
|
||
| const call = (app, path, headers = {}) => | ||
| app.handle({ method: "GET", path, headers, query: {}, body: undefined }); | ||
|
|
||
| /* ------------------------------------------------- multi-user: 401 stands */ | ||
|
|
||
| const multi = build("multi-user"); | ||
| const refused = await call(multi, "/api/_a2app/whoami"); | ||
| check("multi-user: an uncredentialled whoami is refused", refused.status, 401); | ||
| check("multi-user: with the credential code", refused.json.code, "agent_token_required"); | ||
| check("multi-user: and the hint the write path sends", refused.json.how, "Ask the owner."); | ||
|
|
||
| /* ------------------------------------------- single-user: the truthful answer */ | ||
|
|
||
| const single = build("none"); | ||
| const answered = await call(single, "/api/_a2app/whoami"); | ||
| check("single-user: an uncredentialled whoami is answered", answered.status, 200); | ||
| check("single-user: the caller is named as anonymous", answered.json.credentialId, "anonymous"); | ||
| check("single-user: with no agent name to report", answered.json.agentName, null); | ||
|
|
||
| // The set matters, not the order: reads and the readOnly operation, and nothing | ||
| // that changes anything. Anything else here would be whoami promising a write | ||
| // that `authorize` goes on to refuse. | ||
| const scopes = answered.json.scopes; | ||
| ok("single-user: the entity is readable", scopes.includes("data:notes:read")); | ||
| ok("single-user: the readOnly operation is runnable", scopes.includes("op:count-notes")); | ||
| ok("single-user: the entity is NOT writable", !scopes.includes("data:notes:write")); | ||
| ok("single-user: the state-changing operation is NOT runnable", !scopes.includes("op:wipe-notes")); | ||
| check("single-user: nothing else is claimed", scopes.length, 2); | ||
|
|
||
| /* ------------------------------ whoami and describe cannot disagree */ | ||
|
|
||
| // The whole point of deriving whoami's scopes from the same Access describe | ||
| // uses: one caller must not be told two different things about itself. | ||
| const described = await call(single, "/api/_a2app/describe"); | ||
| check("describe agrees: the caller is read-only, not full", described.json.modules[0].access, "read-only"); | ||
|
|
||
| /* ------------------------------------------- a credential still reports itself */ | ||
|
|
||
| const withGrant = createA2App( | ||
| new MemoryBinding({ appId: "whoami_test", appName: "Whoami Test", entities, authMode: "none" }), | ||
| { | ||
| modules: [{ name: "desk", summary: "notes" }], | ||
| operations, | ||
| credentials: [ | ||
| { token: "t_ro", credentialId: "cred_ro", agentName: "reader", principal: "owner", scopes: ["data:notes:read"] }, | ||
| ], | ||
| }, | ||
| ); | ||
| const asGrant = await call(withGrant, "/api/_a2app/whoami", { "x-a2app-token": "t_ro" }); | ||
| check("a credentialled caller reports its own grant", asGrant.status, 200); | ||
| check("by credential id", asGrant.json.credentialId, "cred_ro"); | ||
| check("with its agent name", asGrant.json.agentName, "reader"); | ||
| check("and exactly its granted scopes", asGrant.json.scopes, ["data:notes:read"]); | ||
|
|
||
| /* ---------------------------------------------------------------- report */ | ||
|
|
||
| if (failures.length > 0) { | ||
| console.error(`whoami: ${failures.length} failure(s)\n`); | ||
| for (const f of failures) console.error(" " + f); | ||
| process.exit(1); | ||
| } | ||
| console.log("whoami: all checks passed"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No changes needed but need to create a todo to place agent context/prompt/hint to somewhere. To prevent context injected without our knowledge.