Let an agent write#
A change the person can undo: one approval, reused like a read.
| Level | 1: A person approves with a passkey. |
| Class | write: Something changes and can be undone. The approval can be reused. |
routes.append(route("POST", "/notes", "notes:write", cls="write", level=1, resource="note:*", summary="Add a note",
body={"text": "the note"}))
templates["notes:write"] = "Add and change your notes"
@app.post("/notes")
def add_note():
return notes.add(g.allowedby.human_id, str(request.json.get("text", ""))), 201routes.push(route("POST", "/notes", "notes:write", {
class: "write", level: 1, resource: "note:*", summary: "Add a note", body: { text: "the note" },
}));
templates["notes:write"] = "Add and change your notes";
api.post("/notes", (req, res) => {
const person = req.allowedby.humanId;
if (person === null) return res.status(403).json({ error: "no person at level 0" }); // level 0: no person to key by
res.status(201).json(notes.add(person, String(req.body?.text ?? "")));
});routes = append(routes, allowedby.Route{Method: "POST", Path: "/notes", Capability: "notes:write",
Class: allowedby.Write, Level: 1, Resource: "note:*", Summary: "Add a note",
Body: map[string]string{"text": "the note"}})
templates["notes:write"] = allowedby.Text("Add and change your notes")
mux.HandleFunc("POST /notes", func(w http.ResponseWriter, r *http.Request) {
who, _ := allowedby.ContextFrom(r.Context())
me, ok := who.Human() // false at level 0: an agent calls, with no person to keep data for
if !ok {
writeJSON(w, http.StatusForbidden, map[string]string{"error": "no person at level 0"})
return
}
var input struct{ Text string }
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "text: a string"})
return
}
writeJSON(w, http.StatusCreated, notes.Add(me, input.Text))
})@Bean
AllowedByCustomizer write() {
return aby -> aby
.route(Route.post("/notes").capability("notes:write").actionClass(ActionClass.WRITE).level(1)
.resource("note:*").summary("Add a note").body(Map.of("text", "the note")))
.template("notes:write", "Add and change your notes");
}
@PostMapping("/notes")
@ResponseStatus(HttpStatus.CREATED)
Note addNote(@AllowedByContext Context ctx, @RequestBody NoteInput input) {
String person = ctx.getHumanId();
if (person == null) { // level 0: no person, and never a key
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "no person at level 0");
}
return notes.add(person, input.text() == null ? "" : input.text());
}routes.Add(Route("POST", "/notes", "notes:write", cls: "write", level: 1, resource: "note:*", summary: "Add a note",
body: new Dictionary<string, string> { ["text"] = "the note" }));
templates["notes:write"] = "Add and change your notes";
app.MapPost("/notes", (NoteInput input, AllowedByContext ctx) =>
Results.Json(notes.Add(Person(ctx), input.Text ?? ""), statusCode: 201));$routes[] = route('POST', '/notes', 'notes:write', class: 'write', level: 1, resource: 'note:*',
summary: 'Add a note', body: ['text' => 'the note']);
$templates['notes:write'] = 'Add and change your notes';
$app->post('/notes', function (Request $request, Response $response) use ($json, $notes) {
$person = $request->getAttribute('allowedby.context')->humanId ?? throw new LogicException('no person at level 0');
$input = json_decode((string) $request->getBody(), true);
return $json($response, $notes->add($person, (string) ($input['text'] ?? '')), 201);
});ROUTES << AllowedBy.route(
"POST", "/notes", "notes:write",
class: "write", level: 1, resource: "note:*", summary: "Add a note", body: { text: "the note" }
)
TEMPLATES["notes:write"] = "Add and change your notes"
post "/notes" do
person = env["allowedby.context"].human_id or halt 403, { error: "no person: this route needs one" }.to_json
text = JSON.parse(request.body.read)["text"].to_s
[201, notes.add(person, text).to_json]
endlet aby = aby
.route(
Route::post("/notes")
.capability("notes:write")
.class(Class::Write)
.level(1)
.resource("note:*")
.summary("Add a note")
.body_field("text", "the note"),
)
.template("notes:write", "Add and change your notes");
let app = app.route("/notes", post(add_note));
async fn add_note(
ctx: AllowedByContext,
State(notes): State<Notes>,
Json(input): Json<NoteInput>,
) -> (StatusCode, Json<Note>) {
(StatusCode::CREATED, Json(notes.add(&ctx.human_id, &input.text)))
}Run in 8 languages on this version. See the results
What the test proves#
- without an approval: 401 grant_required
- the person approves, the note is added: 201
- a body changed on the way is refused and nothing is written