Repository navigation
Add OAuth2 resource indicator helpers #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
loks0n
merged 13 commits into
utopia-php:main
from
ChiragAgg5k:codex/oauth2-resource-indicators
Jun 19, 2026
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7f7d507
Add OAuth2 resource indicator helpers
ChiragAgg5k ee7563a
Fix single resource audience shape
ChiragAgg5k 53948b2
Refine resource indicators API
ChiragAgg5k 0de3667
Refine access token audience handling
ChiragAgg5k 751b4f2
Use spread operator for audience list
ChiragAgg5k dd8f8ba
Honor requested resource audiences
ChiragAgg5k 5f39878
Remove duplicate resource validation
ChiragAgg5k fe454ed
Surface invalid resource error code
ChiragAgg5k 85b0330
Document resource downscope check
ChiragAgg5k 886d8b2
Clarify resource URI validation message
ChiragAgg5k ac8709d
Simplify resource indicators README example
ChiragAgg5k 9a15057
Fix resource indicator PHPStan type
ChiragAgg5k 9ee4a93
Consolidate CI workflow
ChiragAgg5k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Auth\OAuth2; | ||
|
|
||
| class InvalidResourceException extends \InvalidArgumentException | ||
| { | ||
| /** | ||
| * RFC 8707 Section 2. | ||
| */ | ||
| public const ERROR_CODE = 'invalid_target'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Auth\OAuth2; | ||
|
|
||
| class ResourceIndicators | ||
| { | ||
| /** | ||
| * @var array<int, string> | ||
| */ | ||
| private array $resources; | ||
|
|
||
| /** | ||
| * @param array<int, string> $resources | ||
| */ | ||
| private function __construct(array $resources) | ||
| { | ||
| if ($resources !== \array_values($resources)) { | ||
| throw new InvalidResourceException('resources must be a list of absolute URIs.'); | ||
| } | ||
|
|
||
| $seen = []; | ||
|
|
||
| foreach ($resources as $resource) { | ||
| switch (true) { | ||
| case !\is_string($resource) || $resource === '': | ||
| throw new InvalidResourceException('resource must be a non-empty absolute URI.'); | ||
|
|
||
| case \is_string($resource) && !self::isValid($resource): | ||
| throw new InvalidResourceException('resource must be an absolute URI without a fragment.'); | ||
|
|
||
| case \in_array($resource, $seen, true): | ||
| throw new InvalidResourceException('resources must not contain duplicates.'); | ||
| } | ||
|
|
||
| $seen[] = $resource; | ||
| } | ||
|
|
||
| $this->resources = $resources; | ||
|
ChiragAgg5k marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * @param string|array<int, mixed>|null $value | ||
| * | ||
| * @throws InvalidResourceException | ||
| */ | ||
| public static function from(string|array|null $value): self | ||
| { | ||
| if ($value === null || $value === '') { | ||
| return new self([]); | ||
| } | ||
|
|
||
| $resources = \is_array($value) ? $value : [$value]; | ||
| $normalized = []; | ||
|
|
||
| foreach ($resources as $resource) { | ||
| if (!\in_array($resource, $normalized, true)) { | ||
| $normalized[] = $resource; | ||
| } | ||
| } | ||
|
|
||
| return new self($normalized); | ||
| } | ||
|
|
||
| public function isSubsetOf(self $granted): bool | ||
| { | ||
| return empty(\array_diff($this->resources, $granted->resources)); | ||
| } | ||
|
|
||
| public function equals(self $resources): bool | ||
| { | ||
| $left = $this->resources; | ||
| $right = $resources->resources; | ||
| \sort($left, \SORT_STRING); | ||
| \sort($right, \SORT_STRING); | ||
|
|
||
| return $left === $right; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<int, string> | ||
| */ | ||
| public function audience(string $defaultAudience): array | ||
| { | ||
| if ($this->resources === []) { | ||
| return [$defaultAudience]; | ||
| } | ||
|
|
||
| return $this->resources; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<int, string> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return $this->resources; | ||
| } | ||
|
|
||
| private static function isValid(string $resource): bool | ||
| { | ||
| $parts = \parse_url($resource); | ||
|
|
||
| return \is_array($parts) | ||
| && !empty($parts['scheme']) | ||
| && !isset($parts['fragment']) | ||
| && (!empty($parts['host']) || !empty($parts['path'])); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.