Skip to content

feat(auth): add AuthorizationDetails reader for RFC 9396 grants - #242

Merged
ChiragAgg5k merged 1 commit into
mainfrom
feat/auth-authorization-details
Sep 9, 2026
Merged

ChiragAgg5k merged 1 commit into
mainfrom
feat/auth-authorization-details

Conversation

@ChiragAgg5k

Copy link
Copy Markdown
Member

Adds a small, reusable reader for a token's authorization_details (RFC 9396 Rich Authorization Requests) to the auth package, alongside an enum of the RFC 9396 §2 common field names.

What

Utopia\Auth\OAuth2\AuthorizationDetails — an immutable value object over a raw authorization_details value, sitting next to the existing ResourceIndicators, PAR, and RedirectUris. Its constructor tolerates any malformed input (non-array, scalar entries, non-list fields) by contributing nothing, so callers need not pre-validate.

$details = new AuthorizationDetails($token->getAttribute('authorizationDetails'));
$details->grants('project', $projectId, 'identifiers', '*'); // wildcard opted in
$details->grants('organization', $teamId, 'identifiers');    // concrete only

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\AuthorizationDetail enumerates 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 existing Claim, Header, and Prompt enums.

Why

Appwrite Cloud resolves an OAuth2 token to an effective role and scopes by asking, repeatedly, whether the token's authorization_details grant 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 auth passes (258 tests). A single data-provider-driven testGrants covers 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) and bin/monorepo validate are clean.

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.
@greptile-apps

greptile-apps Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 3/5

This PR is not safe to merge until malformed associative authorization details are prevented from producing grants.

Fix All in Claude CodeFindings

  1. P1 Security Associative arrays can grant access ▶
  2. P2 OAuth2 helper lacks documentation ▶

Summary

  • Supports strict matching across caller-selected array-valued fields.
  • Allows callers to opt into a custom wildcard sentinel.
  • Needs list-shape validation so malformed JSON objects cannot produce authorization grants.
  • Needs consumer documentation alongside the existing OAuth2 helpers.

{
$entries = [];
if (\is_array($value)) {
foreach ($value as $entry) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security 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.

Fix in Claude Code Fix in Codex

* given type lists a value in one of its array-valued fields. Malformed entries
* and fields are ignored, never trusted.
*/
class AuthorizationDetails

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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!

Fix in Claude Code Fix in Codex

@ChiragAgg5k
ChiragAgg5k merged commit f2da009 into main Sep 9, 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