# Table middleware and security

> Authorization lives once per table, in its middleware. One secured door per domain, and the security model of asasvirtuais apps.

Source: https://asasvirtuais.com/docs/middleware

Nobody reads every table's authorization rules together. You read them by domain. So each table keeps its own rules in
`packages/{model}/middleware.ts`, wrapping the raw database interface with `asasvirtuaisInterface`, typed by the
table's schema:

```ts
// packages/todos/middleware.ts
import { asasvirtuaisInterface } from 'asasvirtuais/interface'
import { db } from '@/app/db'
import { schema } from './schema'

export const todos = asasvirtuaisInterface(schema, {
  find: async (props) => { /* authorization */ return db.find(props) },
  list: async (props) => { /* authorization */ return db.list(props) },
  create: async (props) => { /* authorization */ return db.create(props) },
  update: async (props) => { /* authorization */ return db.update(props) },
  remove: async (props) => { /* authorization */ return db.remove(props) },
})
```

`app/actions.ts` then only switches on the table and calls its middleware. It is still the one interface for every
table:

```ts
// app/actions.ts
'use server'
import type { TableInterface, FindProps, ListProps, CreateProps, UpdateProps, RemoveProps } from 'asasvirtuais/interface'
import { action } from '@/lib/action'
import { projects } from '@/packages/projects/middleware'
import { todos } from '@/packages/todos/middleware'

function middleware(table?: string): TableInterface<any, any> {
  switch (table) {
    case 'projects': return projects
    case 'todos': return todos
    default: throw new Error(`Unknown table: ${table}`)
  }
}

export const find = action(async (props: FindProps) => middleware(props.table).find(props))
export const list = action(async (props: ListProps) => middleware(props.table).list(props))
export const create = action(async (props: CreateProps) => middleware(props.table).create(props))
export const update = action(async (props: UpdateProps) => middleware(props.table).update(props))
export const remove = action(async (props: RemoveProps) => middleware(props.table).remove(props))
```

## The security model

- **One secured door per domain.** Every read and write of a table goes through its middleware. There is no second
  path to the data from the client.
- **Your rules, your code.** The framework adds no rules of its own: what a middleware allows is what you write in it.
- **Writable schemas, enforced at runtime.** A middleware made with `asasvirtuaisInterface(schema, …)` parses `data`
  with the table's `writable` schema before `create` and `update` run. Fields outside it are dropped, and data that
  breaks it is refused with `Invalid data`. On `update` only the fields sent are checked and kept, so defaults never
  overwrite stored values. Server-set fields (owner, tenant, timestamps) are added inside the middleware, after the parse.
- **Server actions carry their own checks.** What doesn't go through the interface has its authorization inside the action.
- **Fail fast.** Throw `new Error('Unauthorized')` or `new Error('Forbidden')`, and call `notFound()` for missing
  records.

This is how asasvirtuais answers the two usual criticisms of AI-built apps: they don't break, because the structure is
fixed, and they aren't insecure, because authorization is in one place per table and easy to audit.

## When to write it

The middleware is written in stage 4 of [Building an app](https://asasvirtuais.com/docs/building-an-app.md), once the features are defined. While
the app is being prototyped, these rules would change with every iteration of the UI. Until then, `app/actions.ts`
passes straight to `app/db.ts`.

`asasvirtuaisInterface` takes one table's schema (`{ readable, writable }`) for a middleware, or the whole database
schema (`{ projects, todos }`) for an interface over every table, such as an adapter.
