Serve only the person's own data#
human_id is the same person every time at your app: look their data up with it.
| Level | 1: A person approves with a passkey. |
| Class | read: Nothing changes. The approval can be reused until it ends or the person withdraws it. |
routes.append(route("GET", "/notes/{id}", "notes:read", level=1, resource="note:{id}", summary="Read one note"))
@app.get("/notes/<note_id>")
def get_note(note_id):
# human_id is this person's id at your app, always the same. Look up with it, never by note_id alone.
note = notes.get(g.allowedby.human_id, note_id)
return note or ({"error": "no such note"}, 404)routes.push(route("GET", "/notes/{id}", "notes:read", { level: 1, resource: "note:{id}", summary: "Read one note" }));
api.get("/notes/:id", (req, res) => {
// humanId is this person's id at your app, always the same. Look up with it, never by note id alone.
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
const note = notes.get(person, req.params.id);
if (!note) return res.status(404).json({ error: "no such note" });
res.json(note);
});routes = append(routes, allowedby.Route{Method: "GET", Path: "/notes/{id}", Capability: "notes:read",
Class: allowedby.Read, Level: 1, Resource: "note:{id}", Summary: "Read one note"})
mux.HandleFunc("GET /notes/{id}", func(w http.ResponseWriter, r *http.Request) {
// The person's id at your app is always the same. Look up with it, never by note id alone.
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
}
note, found := notes.Get(me, r.PathValue("id"))
if !found {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "no such note"})
return
}
writeJSON(w, http.StatusOK, note)
})@Bean
AllowedByCustomizer ownData() {
return aby -> aby.route(Route.get("/notes/{id}").capability("notes:read").level(1).resource("note:{id}")
.summary("Read one note"));
}
@GetMapping("/notes/{id}")
ResponseEntity<Object> getNote(@AllowedByContext Context ctx, @PathVariable String id) {
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");
}
// getHumanId is this person's id at your app, always the same. Look up with it, never by note id alone.
return notes.get(person, id).<ResponseEntity<Object>>map(ResponseEntity::ok)
.orElse(ResponseEntity.status(404).body(Map.of("error", "no such note")));
}routes.Add(Route("GET", "/notes/{id}", "notes:read", level: 1, resource: "note:{id}", summary: "Read one note"));
app.MapGet("/notes/{id}", (string id, AllowedByContext ctx) =>
// Person(ctx) is this person's id at your app, always the same. Look up with it, never by note id alone.
notes.Get(Person(ctx), id) is { } note
? Results.Json(note)
: Results.Json(new { error = "no such note" }, statusCode: 404));$routes[] = route('GET', '/notes/{id}', 'notes:read', level: 1, resource: 'note:{id}', summary: 'Read one note');
$app->get('/notes/{id}', function (Request $request, Response $response, array $args) use ($json, $notes) {
// humanId is this person's id at your app, always the same. Look up with it, never by note id alone.
$person = $request->getAttribute('allowedby.context')->humanId ?? throw new LogicException('no person at level 0');
$note = $notes->get($person, $args['id']);
return null === $note ? $json($response, ['error' => 'no such note'], 404) : $json($response, $note);
});ROUTES << AllowedBy.route(
"GET", "/notes/{id}", "notes:read",
level: 1, resource: "note:{id}", summary: "Read one note"
)
get "/notes/:id" do
# human_id is this person's id at your app, always the same. Look up with it, never by note id alone.
person = env["allowedby.context"].human_id or halt 403, { error: "no person: this route needs one" }.to_json
note = notes.get(person, params["id"])
halt 404, { error: "no such note" }.to_json unless note
note.to_json
endlet aby = aby.route(
Route::get("/notes/{id}").capability("notes:read").level(1).resource("note:{id}").summary("Read one note"),
);
let app = app.route("/notes/{id}", get(get_note));
async fn get_note(
ctx: AllowedByContext,
State(notes): State<Notes>,
Path(id): Path<String>,
) -> Result<Json<Note>, Refusal> {
// human_id is this person's id at your app, always the same. Look up with it, never by note id alone.
let note = notes.get(&ctx.human_id, &id);
note.map(Json).ok_or((StatusCode::NOT_FOUND, Json(json!({"error": "no such note"}))))
}Run in 8 languages on this version. See the results
What the test proves#
- the person's own note is served, under the approval they already gave
- someone else's agent, approved by its own person, gets 404 for that note and an empty list