# Let an agent read

The person approves once with a passkey; the approval is reused until it expires or they withdraw 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. |

## What the test proves

- an agent the person did not approve: 401 grant_required
- the person approves the app's own sentence, then 200
- the approval is reused: nobody is asked again
- the same signed request sent twice is refused

## Python

```python
routes.append(route("GET", "/notes", "notes:read", level=1, resource="note:*", summary="List your notes"))
templates["notes:read"] = "Read your notes"


@app.get("/notes")
def list_notes():
    return {"notes": notes.list(g.allowedby.human_id)}
```

## JavaScript

```js
routes.push(route("GET", "/notes", "notes:read", { level: 1, resource: "note:*", summary: "List your notes" }));
templates["notes:read"] = "Read your notes";

api.get("/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.json({ notes: notes.list(person) });
});
```

## Go

```go
routes = append(routes, allowedby.Route{Method: "GET", Path: "/notes", Capability: "notes:read",
	Class: allowedby.Read, Level: 1, Resource: "note:*", Summary: "List your notes"})
templates["notes:read"] = allowedby.Text("Read your notes")

mux.HandleFunc("GET /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
	}
	writeJSON(w, http.StatusOK, map[string][]Note{"notes": notes.List(me)})
})
```

## Java

```java
@Bean
AllowedByCustomizer read() {
    return aby -> aby
            .route(Route.get("/notes").capability("notes:read").level(1).resource("note:*")
                    .summary("List your notes"))
            .template("notes:read", "Read your notes");
}

@GetMapping("/notes")
Map<String, List<Note>> listNotes(@AllowedByContext Context ctx) {
    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 Map.of("notes", notes.list(person));
}
```

## C#

```dotnet
routes.Add(Route("GET", "/notes", "notes:read", level: 1, resource: "note:*", summary: "List your notes"));
templates["notes:read"] = "Read your notes";

app.MapGet("/notes", (AllowedByContext ctx) => new { notes = notes.List(Person(ctx)) });

// The person the data belongs to. A level-0 route has no person (HumanId is null): stop here rather than key
// anyone's data by a missing id.
static string Person(AllowedByContext ctx) =>
    ctx.HumanId ?? throw new InvalidOperationException("HumanId is null: level-0 routes have no person; use level 1+");
```

## PHP

```php
$routes[] = route('GET', '/notes', 'notes:read', level: 1, resource: 'note:*', summary: 'List your notes');
$templates['notes:read'] = 'Read your notes';

$app->get('/notes', function (Request $request, Response $response) use ($json, $notes) {
    $person = $request->getAttribute('allowedby.context')->humanId ?? throw new LogicException('no person at level 0');

    return $json($response, ['notes' => $notes->list($person)]);
});
```

## Ruby

```ruby
ROUTES << AllowedBy.route("GET", "/notes", "notes:read", level: 1, resource: "note:*", summary: "List your notes")
TEMPLATES["notes:read"] = "Read your notes"

get "/notes" do
  # no person (a level-0 route): refused, never a store keyed by nil
  person = env["allowedby.context"].human_id or halt 403, { error: "no person: this route needs one" }.to_json
  { notes: notes.list(person) }.to_json
end
```

## Rust

```rust
let aby = aby
    .route(Route::get("/notes").capability("notes:read").level(1).resource("note:*").summary("List your notes"))
    .template("notes:read", "Read your notes");
let app = app.route("/notes", get(list_notes));

async fn list_notes(ctx: AllowedByContext, State(notes): State<Notes>) -> Json<Value> {
    Json(json!({"notes": notes.list(&ctx.human_id)}))
}
```
