# Scopes

A person can let another application act for them, such as a partner's app
holding a token their company's identity provider issued it. Such a token
carries scopes: what the person let that application do.
[`external_token:`](/docs/ways-to-sign-in.md#external_token) declares the scopes its
tokens carry, and every such token is narrowed to them. Your own app can
issue its API tokens narrowed the same way, from the scopes
[`api_token:`](/docs/narrowing-an-api-token-to-scopes.md) declares. A narrowed token may
do what both the person's grants and its scopes allow, and never more
than the person could do themselves.

An entity's `policy:` says, in `scopes:`, which scope a narrowed token
needs for each effect, as well as a grant. Here a partner's app reads a
diarist's notes with `notes:read`, and writes them with `notes.write`:

```jadpo
config {
    audience: Text
    issuer: Url
}

principal {
    user: {
        subject: Diarist.handle
    }
}

authentication {
    browser_session: {
        cookie: "notebook_session"
    }
    external_token: {
        issuer: config.issuer
        audience: config.audience
        scopes: {
            notes_read: "notes:read"
            notes_write: "notes.write"
        }
    }
}

type Handle = Text {
    max_length: 40
}

entity Diarist {
    id: Uuid
    handle: Handle {
        unique: true
    }

    policy: {
        read: [id]
    }
}

entity Note {
    id: Uuid
    diarist_id: Diarist.id {
        on_delete: OnDelete.cascade
    }
    text: Text

    policy: {
        create: [diarist_id]
        read: [diarist_id]
        update: [diarist_id]
        delete: [diarist_id]
        scopes: {
            create: OAuthScope.notes_write
            read: OAuthScope.notes_read
            update: OAuthScope.notes_write
        }
    }
}
```

- Each effect names one scope, `OAuthScope.<name>`. `OAuthScope` here
  holds only the scopes `authentication.jadpo` declares, so a misspelt or
  undeclared scope is `policy.binding_invalid`. A list would read as "any
  of", and a scope must hold as well as a grant, so there is none.
- An effect `scopes:` leaves out is refused to every narrowed token. So
  no token deletes a note, and none reads a `Diarist`, whose policy names
  no scope: a new entity is never open to other applications by accident.
- `Access.public` and `Access.authenticated` are grants like any other: a
  narrowed token still needs the effect's scope.
- Browser sessions and service keys carry no scopes, and neither do
  schedules and subscribers, so scopes never touch them. Nor do they touch
  a token from a provider whose `external_token:` declares none, or an API
  token issued without `scopes:`. An API token issued with them is
  narrowed exactly as a provider's token is.

A narrowed token is refused as any caller policy refuses is ([When policy
says no](/docs/when-policy-says-no.md)): a note it may not read is the code's own
`not_found` failure, a list leaves it out, and a create is 403
`not_permitted`. The check is in each statement's `WHERE`, as every grant
is.

Some things no effect's scope covers, so a narrowed token is refused them:

- an action listed under `actions:` that is checked as it starts, since
  no entity's `scopes:` names it. A listed query is let through, and its
  records are still filtered by their entity's `read:` scope;
- starting a session or issuing an API token, `sessions.start` and
  `sessions.issue_token` (403 `not_permitted`), since either would carry
  the person's whole authority;
- looking a person up by email, `<Entity>.lookup`
  ([Looking up one person by email](/docs/looking-up-one-person-by-email.md)),
  since no scope names a lookup;
- a slot for an upload, unless it carries the scope of its entity's
  `create:` or `update:`.

The [permissions report](/docs/the-permissions-report.md) gives each entity a
"scope" row, with "refused" where no scope lets a narrowed token in, and
`access.jadpo_record` keeps the declared scopes and each effect's.
Loosening them needs a person ([Protecting policy](/docs/protecting-policy.md)). A
route test says which scopes its token carries with `scopes:`
([Signing in for a route test](/docs/acting-as-a-user.md#signing-in-for-a-route-test)).
