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 3 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,95 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Auth\OAuth2; | ||
|
|
||
| class ResourceIndicators | ||
| { | ||
| /** | ||
| * @var array<int, string> | ||
| */ | ||
| private array $resources; | ||
|
|
||
| /** | ||
| * @param array<int, string> $resources | ||
| */ | ||
| private function __construct(array $resources) | ||
| { | ||
| $this->resources = $resources; | ||
|
ChiragAgg5k marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * @param string|array<int, mixed>|null $value | ||
| */ | ||
| 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 (!\is_string($resource) || $resource === '') { | ||
| throw new \InvalidArgumentException('resource must be a non-empty absolute URI.'); | ||
| } | ||
|
|
||
| if (!self::isValid($resource)) { | ||
| throw new \InvalidArgumentException('resource must be an absolute URI without a fragment.'); | ||
| } | ||
|
|
||
| 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 string|array<int, string> | ||
| */ | ||
| public function audience(string $defaultAudience): string|array | ||
| { | ||
| $resourcesWithoutDefault = \array_values( | ||
| \array_filter($this->resources, fn ($resource) => $resource !== $defaultAudience) | ||
| ); | ||
|
|
||
| $audience = \array_values(\array_merge([$defaultAudience], $resourcesWithoutDefault)); | ||
|
|
||
| return \count($audience) === 1 ? $audience[0] : $audience; | ||
| } | ||
|
|
||
| /** | ||
| * @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
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,105 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Auth\OAuth2; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Auth\OAuth2\ResourceIndicators; | ||
|
|
||
| class ResourceIndicatorsTest extends TestCase | ||
| { | ||
| public function testEmptyValueNormalizesToEmptyArray(): void | ||
| { | ||
| $this->assertSame([], ResourceIndicators::from(null)->toArray()); | ||
| $this->assertSame([], ResourceIndicators::from('')->toArray()); | ||
| } | ||
|
|
||
| public function testStringNormalizesToArray(): void | ||
| { | ||
| $this->assertSame(['https://api.example.com/'], ResourceIndicators::from('https://api.example.com/')->toArray()); | ||
| } | ||
|
|
||
| public function testArrayIsNormalizedAndDeduplicated(): void | ||
| { | ||
| $this->assertSame( | ||
| ['https://api.example.com/', 'urn:example:resource'], | ||
| ResourceIndicators::from([ | ||
| 'https://api.example.com/', | ||
| 'urn:example:resource', | ||
| 'https://api.example.com/', | ||
| ])->toArray() | ||
| ); | ||
| } | ||
|
|
||
| public function testFragmentIsRejected(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('resource must be an absolute URI without a fragment.'); | ||
|
|
||
| ResourceIndicators::from('https://api.example.com/#section'); | ||
| } | ||
|
|
||
| public function testRelativeUriIsRejected(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('resource must be an absolute URI without a fragment.'); | ||
|
|
||
| ResourceIndicators::from('/relative'); | ||
| } | ||
|
|
||
| public function testNonStringIsRejected(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('resource must be a non-empty absolute URI.'); | ||
|
|
||
| ResourceIndicators::from(['https://api.example.com/', 42]); | ||
| } | ||
|
|
||
| public function testSubset(): void | ||
| { | ||
| $this->assertTrue( | ||
| ResourceIndicators::from(['https://api.example.com/']) | ||
| ->isSubsetOf(ResourceIndicators::from(['https://api.example.com/', 'https://files.example.com/'])) | ||
| ); | ||
| $this->assertFalse( | ||
| ResourceIndicators::from(['https://api.example.com/']) | ||
| ->isSubsetOf(ResourceIndicators::from(['https://files.example.com/'])) | ||
| ); | ||
| } | ||
|
|
||
| public function testSameSetIgnoresOrder(): void | ||
| { | ||
| $this->assertTrue( | ||
| ResourceIndicators::from(['https://api.example.com/', 'https://files.example.com/']) | ||
| ->equals(ResourceIndicators::from(['https://files.example.com/', 'https://api.example.com/'])) | ||
| ); | ||
| } | ||
|
|
||
| public function testAudienceUsesDefaultWhenNoResourcesRequested(): void | ||
| { | ||
| $this->assertSame( | ||
| 'https://cloud.appwrite.io/v1/project1', | ||
| ResourceIndicators::from(null)->audience('https://cloud.appwrite.io/v1/project1') | ||
| ); | ||
| } | ||
|
|
||
| public function testAudiencePrependsDefaultAndDeduplicatesIt(): void | ||
| { | ||
| $this->assertSame( | ||
| ['https://cloud.appwrite.io/v1/project1', 'https://mcp.example.com/'], | ||
| ResourceIndicators::from([ | ||
| 'https://mcp.example.com/', | ||
| 'https://cloud.appwrite.io/v1/project1', | ||
| ])->audience('https://cloud.appwrite.io/v1/project1') | ||
| ); | ||
| } | ||
|
|
||
| public function testAudienceReturnsStringWhenOnlyDefaultAudienceRemains(): void | ||
| { | ||
| $this->assertSame( | ||
| 'https://cloud.appwrite.io/v1/project1', | ||
| ResourceIndicators::from([ | ||
| 'https://cloud.appwrite.io/v1/project1', | ||
| ])->audience('https://cloud.appwrite.io/v1/project1') | ||
| ); | ||
| } | ||
| } |
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.