Repository navigation
feat(auth): add AuthorizationDetails reader for RFC 9396 grants - #242
Conversation
Adds a value object over a token's `authorization_details` (RFC 9396 Rich Authorization Requests) and an enum of the RFC 9396 §2 common field names. `AuthorizationDetails::grants(type, value, field, wildcard)` answers whether an entry of a given type lists a value in one of its array-valued fields — the RFC common fields (locations, actions, datatypes, privileges) or a type-defined one. The wildcard is an opt-in caller parameter because RFC 9396 defines none, so no sentinel is baked into the library. The `AuthorizationDetail` enum keeps the field names (type, locations, actions, datatypes, identifier, privileges) out of scattered string literals, matching the existing Claim/Header/Prompt enums.
| { | ||
| $entries = []; | ||
| if (\is_array($value)) { | ||
| foreach ($value as $entry) { |
There was a problem hiding this comment.
Associative arrays can grant access
A JSON object used where RFC 9396 requires a list is still traversed because both the outer claim and each field are checked only with is_array(). For example, {"named":{"type":"project","identifiers":["p1"]}} is decoded as an associative array, accepted as an entry collection, and grants p1. Likewise, an associative field such as {"identifiers":{"alias":"p1"}} matches through in_array(). This breaks the documented promise that non-list input contributes nothing and can turn a malformed authorization claim into an access grant. Both levels should require list-shaped arrays, with regression coverage for associative inputs.
How this was verified: JWT claims decode JSON objects into associative PHP arrays, and these checks iterate their values without validating list shape.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/auth/src/Auth/OAuth2/AuthorizationDetails.php
Line: 30
Comment:
**Associative arrays can grant access**
A JSON object used where RFC 9396 requires a list is still traversed because both the outer claim and each field are checked only with `is_array()`. For example, `{"named":{"type":"project","identifiers":["p1"]}}` is decoded as an associative array, accepted as an entry collection, and grants `p1`. Likewise, an associative field such as `{"identifiers":{"alias":"p1"}}` matches through `in_array()`. This breaks the documented promise that non-list input contributes nothing and can turn a malformed authorization claim into an access grant. Both levels should require list-shaped arrays, with regression coverage for associative inputs.
**How this was verified:** JWT claims decode JSON objects into associative PHP arrays, and these checks iterate their values without validating list shape.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| * given type lists a value in one of its array-valued fields. Malformed entries | ||
| * and fields are ignored, never trusted. | ||
| */ | ||
| class AuthorizationDetails |
There was a problem hiding this comment.
OAuth2 helper lacks documentation
This adds a public OAuth2 helper without documenting it in docs/oauth2.md, despite the auth package's contributor guidance requiring new OAuth2 or OpenID Connect helpers to update that document. Add usage and wildcard-semantics documentation so consumers can discover and correctly use the new API.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/auth/src/Auth/OAuth2/AuthorizationDetails.php
Line: 15
Comment:
**OAuth2 helper lacks documentation**
This adds a public OAuth2 helper without documenting it in `docs/oauth2.md`, despite the auth package's contributor guidance requiring new OAuth2 or OpenID Connect helpers to update that document. Add usage and wildcard-semantics documentation so consumers can discover and correctly use the new API.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Adds a small, reusable reader for a token's
authorization_details(RFC 9396 Rich Authorization Requests) to theauthpackage, alongside an enum of the RFC 9396 §2 common field names.What
Utopia\Auth\OAuth2\AuthorizationDetails— an immutable value object over a rawauthorization_detailsvalue, sitting next to the existingResourceIndicators,PAR, andRedirectUris. Its constructor tolerates any malformed input (non-array, scalar entries, non-list fields) by contributing nothing, so callers need not pre-validate.grants(type, value, field, wildcard)answers whether an entry of the given type lists a value in one of its array-valued fields. It is field-parameterized so it works for the RFC common array fields (locations,actions,datatypes,privileges) and any field a type defines itself. The wildcard is an opt-in caller parameter, because RFC 9396 defines no wildcard — no sentinel is baked into the library.Utopia\Auth\Enums\AuthorizationDetailenumerates the RFC 9396 §2 common field names (type,locations,actions,datatypes,identifier,privileges), each RFC-cited, keeping them out of scattered string literals — the same pattern as the existingClaim,Header, andPromptenums.Why
Appwrite Cloud resolves an OAuth2 token to an effective role and scopes by asking, repeatedly, whether the token's
authorization_detailsgrant a given project or organization (honoring a*wildcard). That matching is protocol-level RAR logic, duplicated across several call sites today. This gives it one home with its own tests, so consumers delegate to it instead of re-implementing the array walk.Tests
bin/monorepo test authpasses (258 tests). A single data-provider-driventestGrantscovers exact match, absent value, type and field mismatch, another RFC field, the wildcard opted in and left out, the empty-argument guards, malformed inputs, and type-strict matching.bin/monorepo check auth(pint, phpstan, rector) andbin/monorepo validateare clean.