Skip to content

feat(auth): narrow authorization_details to what a resolver still allows - #257

Merged
ChiragAgg5k merged 4 commits into
mainfrom
feat/auth-authorization-details-restrict
Sep 10, 2026
Merged

ChiragAgg5k merged 4 commits into
mainfrom
feat/auth-authorization-details-restrict

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds restrict(string $field, callable $resolver, ?string $wildcard = null): self and toArray(): list<array> to Utopia\Auth\OAuth2\AuthorizationDetails.

An authorization server that stores an RFC 9396 grant at consent time needs to reconcile it with the subject's current access whenever it issues or introspects a token. The list shaping for that is protocol-level and now lives next to grants(). The access lookup itself stays with the caller as a resolver callable.

Usage

The resolver receives an entry's type and the string values in $field, and returns the allowed subset. null means the resolver does not govern that type and the entry passes through untouched. An empty result drops the entry. The result can only narrow: values the entry did not list are discarded, so a resolver cannot widen a grant. The exception is an entry that lists $wildcard (for example '*', named by the caller as with grants()), whose resolver result is taken as the expansion. Other fields on the entry are kept, order is kept, and the receiver is never modified.

use Utopia\Auth\OAuth2\AuthorizationDetails;

$granted = new AuthorizationDetails([
    ['type' => 'organization', 'identifiers' => ['org-a', 'org-b']],
    ['type' => 'project', 'identifiers' => ['p1', 'p2'], 'actions' => ['read']],
    ['type' => 'payment', 'actions' => ['initiate']],
]);

$issued = $granted->restrict('identifiers', fn (string $type, array $values): ?array => match ($type) {
    'organization' => $user->organizations(), // intersected with $values, or the full list when $values is ['*']
    'project' => $user->projects(),
    default => null,
}, '*');

$issued->toArray();
// [
//     ['type' => 'organization', 'identifiers' => ['org-a']],         // narrowed
//     ['type' => 'project', 'identifiers' => ['p1'], 'actions' => ['read']], // other fields kept
//     ['type' => 'payment', 'actions' => ['initiate']],               // ungoverned, untouched
// ]
// An entry whose allowed subset is empty is dropped.

At a token endpoint, the result feeds both the JWT claim and the response member:

$accessTokenClaims['authorization_details'] = $issued->toArray();
$responsePayload['authorization_details'] = $issued->toArray();

At an introspection endpoint, the same call on the stored grant keeps the two answers consistent:

$response['authorization_details'] = (new AuthorizationDetails($token->getAttribute('authorizationDetails')))
    ->restrict('identifiers', $resolver)
    ->toArray();

Appwrite Cloud will use this to intersect organization and project grants with live memberships at both endpoints, replacing a hand-rolled loop (appwrite-labs/cloud follow-up).

Changes

  • src/Auth/OAuth2/AuthorizationDetails.php: restrict() and toArray().
  • tests/Auth/OAuth2/AuthorizationDetailsTest.php: data-provider cases for narrowing, dropping, pass-through with order, malformed fields, non-string values, plus immutability, no-widening and wildcard-expansion checks.
  • docs/oauth2.md: one paragraph and an example under the RAR section.

Verification

Full auth suite passes locally (269 tests). Pint clean against the repo pint.json. PHPStan level 5 clean on the touched files.

An authorization server that stores a RAR grant at consent has to reconcile it with the subject's current access every time it issues or introspects a token. The list shaping for that is protocol-level (RFC 9396 entries, one array-valued field, pass-through for ungoverned types) and belongs next to grants(); the access lookup itself stays with the caller as a resolver callable.
@greptile-apps

greptile-apps Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with the previous grant-expansion defect addressed and no new actionable failures identified.

Summary

  • Introduces restrict() with resolver-driven filtering, no-widening behavior, entry removal, and caller-defined wildcard expansion.
  • Adds toArray() for emitting the resulting authorization details.
  • Documents the API and tests narrowing, pass-through behavior, malformed values, immutability, wildcard expansion, and prevention of grant widening.

Reviews (3) · Last reviewed commit: "docs(auth): reword the wildcard note so ..."

A resolver's output replaced the field wholesale, so a live-access resolver returning everything the subject can reach today could add values the token was never granted. Intersect with the granted values so restrict() only ever narrows.
@ChiragAgg5k

Copy link
Copy Markdown
Member Author

@greptile review

Intersecting with the granted values dropped every entry that listed a wildcard, since the resolver's concrete expansion never matches the sentinel. Callers name the wildcard, as with grants(), and only those entries take the resolver's result as the expansion; explicit grants still cannot widen.
@ChiragAgg5k
ChiragAgg5k merged commit c1155c7 into main Sep 10, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants