# `AttestoPhoenix.AuthorizationServer.ScopeAuthorization`
[🔗](https://github.com/XukuLLC/attesto_phoenix/blob/v5.0.0/lib/attesto_phoenix/authorization_server/scope_authorization.ex#L1)

Runs the host's scope policy (RFC 6749 §3.3), as conn-free core.

The authorization endpoint and every token-endpoint grant decide scope
through `authorize/4`, so a host writes its policy once and both endpoints
apply it to the same inputs: the client, the requested scope, and the RFC
8707 resource indicators the request resolved to.

The host callback has two forms (see `AttestoPhoenix.ScopePolicy`):

  * `authorize_scope(client, requested_scope, resources)` - preferred when
    the host provides it. It may refuse with `{:error, :invalid_scope}` or
    `{:error, :invalid_target}`.
  * `authorize_scope(client, requested_scope)` - sees no resources, so it
    may refuse only with `{:error, :invalid_scope}`.

RFC 8707 gives no rule that separates `invalid_target` from `invalid_scope`
for a refusal about a resource and scope together, so the callback's return
value says which one applies.

# `refusal`

```elixir
@type refusal() :: :invalid_scope | :invalid_target
```

# `authorize`

```elixir
@spec authorize(AttestoPhoenix.Config.t(), term(), [String.t()], [String.t()]) ::
  {:ok, [String.t()]} | {:error, refusal()}
```

Decide the scope to grant `client` for `requested` scope values and the
resolved `resources`.

Returns `{:ok, granted}` or `{:error, :invalid_scope | :invalid_target}`.
Raises `RuntimeError` when the callback returns anything outside its
contract, including `:invalid_target` from the two-argument form.

# `authorize_subject`

```elixir
@spec authorize_subject(AttestoPhoenix.Config.t(), term(), map(), [String.t()], [
  String.t()
]) ::
  {:ok, [String.t()]} | {:error, refusal()}
```

Apply the authenticated principal's scope and resource ceiling.

The optional `:authorize_subject_scope` callback can only narrow the
effective scope. Unlike default scope selection, an empty effective scope
remains empty. Malformed or expanded grants are refused as `:invalid_scope`.

# `within_request?`

```elixir
@spec within_request?([String.t()], [String.t()]) :: boolean()
```

Whether a policy result stays within what the client asked for (RFC 6749
§3.3).

A policy may narrow the requested scope but may not add to it. When the
client omitted `scope` (an empty request), RFC 6749 §3.3 lets the server
"process the request using a pre-defined default value", so any granted
scope is within the request. Both endpoints use this one rule.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
