From a985baadb333804e8531ecfec55f601d31eea153 Mon Sep 17 00:00:00 2001 From: Adrian Boros Date: Wed, 7 Oct 2026 11:18:28 +0300 Subject: [PATCH 1/2] feat(grants): support subject grant requests and responses --- .github/workflows/ci.yml | 2 +- CHANGELOG.md | 8 ++ lib/Models/Grant.php | 7 +- lib/Models/GrantContinue.php | 4 +- lib/Models/Subject.php | 29 +++++ lib/Models/SubjectId.php | 29 +++++ lib/Transformers/GrantTransformer.php | 36 +++++- lib/Validators/Grant/RequestSchema.json | 60 +++++++++- tests/Services/GrantServiceTest.php | 56 ++++++++++ tests/Transformers/GrantTransformerTest.php | 97 ++++++++++++++++ tests/Validators/GrantValidatorTest.php | 116 ++++++++++++++++++++ 11 files changed, 437 insertions(+), 7 deletions(-) create mode 100644 lib/Models/Subject.php create mode 100644 lib/Models/SubjectId.php create mode 100644 tests/Transformers/GrantTransformerTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 93d7219..a2c47d6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,4 +27,4 @@ jobs: run: vendor/bin/phpstan analyse lib/Utils --level=1 --no-progress - name: Code style - run: vendor/bin/pint --test lib tests/Http tests/DTO tests/Validators + run: vendor/bin/pint --test lib tests/Http tests/DTO tests/Validators tests/Transformers diff --git a/CHANGELOG.md b/CHANGELOG.md index df1ed8c..38e8fb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - GitHub Actions CI: PHPUnit, PHPStan and Pint +- Grant requests for subject information (spec v1.3 wallet address ownership). A request needs `access_token`, or `interact` and `subject` +- `Subject` and `SubjectId` models. `Grant::$subject` holds the `subject` returned by the auth server + +### Changed +- `Grant::$access_token` is now nullable. It is `null` for a grant that only returns subject information. In that case `Grant::$subject` is set +- `GrantContinue::$access_token` is now `AccessToken|SimpleAccessToken`. It is a `SimpleAccessToken` when the grant has no access token. Only `->value` is set for both types +- These two property types changed, so code that uses static analysis may need a null check or an `instanceof` check +- `GrantTransformer` throws `UnexpectedValueException` when a grant response has no valid `continue` field, or has neither `access_token` nor `subject` (grant still pending). Before, it threw an `InvalidArgumentException` or a `TypeError` ## [1.1.0] - 2026-06-13 diff --git a/lib/Models/Grant.php b/lib/Models/Grant.php index 1ec25de..6a7dbf8 100644 --- a/lib/Models/Grant.php +++ b/lib/Models/Grant.php @@ -6,13 +6,16 @@ class Grant { - public readonly AccessToken $access_token; + public readonly ?AccessToken $access_token; public readonly GrantContinue $continue; - public function __construct(AccessToken $access_token, GrantContinue $continue) + public readonly ?Subject $subject; + + public function __construct(?AccessToken $access_token, GrantContinue $continue, ?Subject $subject = null) { $this->access_token = $access_token; $this->continue = $continue; + $this->subject = $subject; } } diff --git a/lib/Models/GrantContinue.php b/lib/Models/GrantContinue.php index 8ea5b89..70983a6 100644 --- a/lib/Models/GrantContinue.php +++ b/lib/Models/GrantContinue.php @@ -10,13 +10,13 @@ */ class GrantContinue { - public readonly AccessToken $access_token; + public readonly AccessToken|SimpleAccessToken $access_token; public readonly string $uri; public readonly ?int $wait; - public function __construct(AccessToken $access_token, string $uri, ?int $wait = null) + public function __construct(AccessToken|SimpleAccessToken $access_token, string $uri, ?int $wait = null) { $this->access_token = $access_token; $this->uri = $uri; diff --git a/lib/Models/Subject.php b/lib/Models/Subject.php new file mode 100644 index 0000000..08a1ecf --- /dev/null +++ b/lib/Models/Subject.php @@ -0,0 +1,29 @@ +sub_ids = $sub_ids; + } + + /** + * Convert the model to an associative array. + */ + public function toArray(): array + { + return [ + 'sub_ids' => array_map(fn (SubjectId $subId) => $subId->toArray(), $this->sub_ids), + ]; + } +} diff --git a/lib/Models/SubjectId.php b/lib/Models/SubjectId.php new file mode 100644 index 0000000..f79dd39 --- /dev/null +++ b/lib/Models/SubjectId.php @@ -0,0 +1,29 @@ +id = $id; + $this->format = $format; + } + + /** + * Convert the model to an associative array. + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'format' => $this->format, + ]; + } +} diff --git a/lib/Transformers/GrantTransformer.php b/lib/Transformers/GrantTransformer.php index 683c7da..2f038b2 100644 --- a/lib/Transformers/GrantTransformer.php +++ b/lib/Transformers/GrantTransformer.php @@ -8,6 +8,9 @@ use OpenPayments\Models\IncomingPaymentAccess; use OpenPayments\Models\OutgoingPaymentAccess; use OpenPayments\Models\QuoteAccess; +use OpenPayments\Models\SimpleAccessToken; +use OpenPayments\Models\Subject; +use OpenPayments\Models\SubjectId; use Psr\Http\Message\ResponseInterface; use stdClass; @@ -18,6 +21,27 @@ public function createFromResponse(array|stdClass|ResponseInterface $response): if ($response instanceof stdClass || $response instanceof ResponseInterface) { $response = json_decode(json_encode($response), true); } + if (! isset($response['continue']['access_token']['value'], $response['continue']['uri'])) { + throw new \UnexpectedValueException('Grant response has no valid continue field'); + } + if (! isset($response['access_token']) && ! isset($response['subject'])) { + throw new \UnexpectedValueException('Grant is not finalized: response has no access_token or subject'); + } + + $subject = isset($response['subject']) ? $this->createSubject($response['subject']) : null; + + // A grant for subject information only has no access token. + if (! isset($response['access_token'])) { + $continueData = $response['continue']; + $grantContinue = new GrantContinue( + new SimpleAccessToken($continueData['access_token']['value']), + $continueData['uri'], + $continueData['wait'] ?? null + ); + + return new Grant(null, $grantContinue, $subject); + } + $accessTokenData = $response['access_token']; $accessData = $accessTokenData['access'][0]; @@ -72,6 +96,16 @@ public function createFromResponse(array|stdClass|ResponseInterface $response): ); // Return the complete Grant instance - return new Grant($accessToken, $grantContinue); + return new Grant($accessToken, $grantContinue, $subject); + } + + private function createSubject(array $subjectData): Subject + { + $subIds = array_map( + fn (array $subId) => new SubjectId($subId['id'], $subId['format']), + $subjectData['sub_ids'] ?? [] + ); + + return new Subject($subIds); } } diff --git a/lib/Validators/Grant/RequestSchema.json b/lib/Validators/Grant/RequestSchema.json index dcee998..95ed4b3 100644 --- a/lib/Validators/Grant/RequestSchema.json +++ b/lib/Validators/Grant/RequestSchema.json @@ -1,6 +1,10 @@ { "type": "object", - "required": ["access_token", "client"], + "required": ["client"], + "anyOf": [ + { "required": ["access_token"] }, + { "required": ["interact", "subject"] } + ], "properties": { "access_token": { "type": "object", @@ -121,6 +125,60 @@ "client": { "type": "string", "format": "uri" + }, + "interact": { + "type": "object", + "required": ["start"], + "properties": { + "start": { + "type": "array", + "items": { + "type": "string", + "enum": ["redirect"] + } + }, + "finish": { + "type": ["object", "null"], + "required": ["method", "uri", "nonce"], + "properties": { + "method": { + "type": "string", + "enum": ["redirect"] + }, + "uri": { + "type": "string", + "format": "uri" + }, + "nonce": { + "type": "string" + } + } + } + } + }, + "subject": { + "type": "object", + "required": ["sub_ids"], + "properties": { + "sub_ids": { + "type": "array", + "minItems": 1, + "maxItems": 1, + "items": { + "type": "object", + "required": ["id", "format"], + "properties": { + "id": { + "type": "string" + }, + "format": { + "type": "string", + "enum": ["uri"] + } + } + } + } + } } }, "definitions": { diff --git a/tests/Services/GrantServiceTest.php b/tests/Services/GrantServiceTest.php index daef546..d2ee595 100644 --- a/tests/Services/GrantServiceTest.php +++ b/tests/Services/GrantServiceTest.php @@ -99,6 +99,62 @@ public function test_request_returns_grant(): void $this->assertInstanceOf(Grant::class, $result); } + public function test_request_with_subject_returns_pending_grant(): void + { + $response = [ + 'interact' => ['redirect' => 'https://ilp.interledger-test.dev', 'finish' => 'abc'], + 'continue' => ['access_token' => ['value' => 'EAB18A53A6F67F9F1247'], 'uri' => 'https://ilp.interledger-test.dev/continue/abc'], + ]; + $this->apiClient->method('request')->willReturn($response); + + $this->pendingTransformer + ->expects($this->once()) + ->method('createFromResponse') + ->with($response) + ->willReturn($this->createMock(PendingGrant::class)); + + $result = $this->service->request(['url' => 'https://ilp.interledger-test.dev'], [ + 'subject' => [ + 'sub_ids' => [ + ['id' => 'https://ilp.interledger-test.dev/alice', 'format' => 'uri'], + ], + ], + 'interact' => [ + 'start' => ['redirect'], + 'finish' => [ + 'method' => 'redirect', + 'uri' => 'https://example.com/finish', + 'nonce' => 'nonce-123', + ], + ], + ]); + $this->assertInstanceOf(PendingGrant::class, $result); + } + + public function test_continue_returns_subject_grant(): void + { + $this->apiClient->method('request')->willReturn([ + 'subject' => [ + 'sub_ids' => [ + ['id' => 'https://ilp.interledger-test.dev/alice', 'format' => 'uri'], + ], + ], + 'continue' => [ + 'access_token' => ['value' => '33OMUKMKSKU80UPRY5NM'], + 'uri' => 'https://ilp.interledger-test.dev/continue/abc', + ], + ]); + $service = new GrantService($this->apiClient, 'https://ilp.interledger-test.dev/wallet'); + + $result = $service->continue([ + 'url' => 'https://ilp.interledger-test.dev/continue/abc', + 'access_token' => 'EAB18A53A6F67F9F1247', + ], ['interact_ref' => 'xyz']); + + $this->assertNull($result->access_token); + $this->assertSame('https://ilp.interledger-test.dev/alice', $result->subject->sub_ids[0]->id); + } + public function test_continue_throws_if_missing_url_or_token(): void { $this->expectException(\InvalidArgumentException::class); diff --git a/tests/Transformers/GrantTransformerTest.php b/tests/Transformers/GrantTransformerTest.php new file mode 100644 index 0000000..b0d29c3 --- /dev/null +++ b/tests/Transformers/GrantTransformerTest.php @@ -0,0 +1,97 @@ + ['value' => '33OMUKMKSKU80UPRY5NM'], + 'uri' => 'https://auth.interledger-test.dev/continue/4CF492MLVMSW9MKMXKHQ', + 'wait' => 30, + ]; + + private const SUBJECT = [ + 'sub_ids' => [ + ['id' => 'https://ilp.interledger-test.dev/alice', 'format' => 'uri'], + ], + ]; + + private const ACCESS_TOKEN = [ + 'value' => 'OS9M2PMHKUR64TB8N6BW7OZB8CDFONP219RP1LT0', + 'manage' => 'https://auth.interledger-test.dev/token/dd17a202-9982-4ed9-ae31-564947fb6379', + 'expires_in' => 3600, + 'access' => [ + [ + 'type' => 'incoming-payment', + 'actions' => ['create', 'read'], + 'identifier' => 'https://ilp.interledger-test.dev/alice', + ], + ], + ]; + + public function test_subject_only_response(): void + { + $grant = (new GrantTransformer)->createFromResponse([ + 'subject' => self::SUBJECT, + 'continue' => self::CONTINUE, + ]); + + $this->assertNull($grant->access_token); + $this->assertNotNull($grant->subject); + $this->assertCount(1, $grant->subject->sub_ids); + $this->assertSame('https://ilp.interledger-test.dev/alice', $grant->subject->sub_ids[0]->id); + $this->assertSame('uri', $grant->subject->sub_ids[0]->format); + $this->assertSame(self::SUBJECT, $grant->subject->toArray()); + $this->assertInstanceOf(SimpleAccessToken::class, $grant->continue->access_token); + $this->assertSame('33OMUKMKSKU80UPRY5NM', $grant->continue->access_token->value); + $this->assertSame(self::CONTINUE['uri'], $grant->continue->uri); + $this->assertSame(30, $grant->continue->wait); + } + + public function test_access_token_response(): void + { + $grant = (new GrantTransformer)->createFromResponse([ + 'access_token' => self::ACCESS_TOKEN, + 'continue' => self::CONTINUE, + ]); + + $this->assertNull($grant->subject); + $this->assertInstanceOf(AccessToken::class, $grant->access_token); + $this->assertSame(self::ACCESS_TOKEN['value'], $grant->access_token->value); + $this->assertSame(self::ACCESS_TOKEN['manage'], $grant->access_token->manage); + $this->assertInstanceOf(AccessToken::class, $grant->continue->access_token); + $this->assertSame('33OMUKMKSKU80UPRY5NM', $grant->continue->access_token->value); + } + + public function test_access_token_and_subject_response(): void + { + $grant = (new GrantTransformer)->createFromResponse([ + 'access_token' => self::ACCESS_TOKEN, + 'subject' => self::SUBJECT, + 'continue' => self::CONTINUE, + ]); + + $this->assertNotNull($grant->access_token); + $this->assertNotNull($grant->subject); + $this->assertSame('https://ilp.interledger-test.dev/alice', $grant->subject->sub_ids[0]->id); + } + + public function test_pending_continue_response_throws(): void + { + $this->expectException(\UnexpectedValueException::class); + (new GrantTransformer)->createFromResponse(['continue' => self::CONTINUE]); + } + + public function test_response_without_continue_throws(): void + { + $this->expectException(\UnexpectedValueException::class); + (new GrantTransformer)->createFromResponse(['message' => 'No content returned', 'status_code' => 204]); + } +} diff --git a/tests/Validators/GrantValidatorTest.php b/tests/Validators/GrantValidatorTest.php index 2588c78..e41a498 100644 --- a/tests/Validators/GrantValidatorTest.php +++ b/tests/Validators/GrantValidatorTest.php @@ -2,6 +2,7 @@ namespace Tests\Validators; +use OpenPayments\DTO\GrantInteraction; use OpenPayments\Exceptions\ValidationException; use OpenPayments\Validators\GrantValidator; use PHPUnit\Framework\TestCase; @@ -47,4 +48,119 @@ public function test_invalid_request(): void $this->expectException(ValidationException::class); $validator->validateRequest($invalidData); } + + public function test_valid_subject_request(): void + { + $validator = new GrantValidator; + + $this->expectNotToPerformAssertions(); + $validator->validateRequest($this->subjectRequest()); + } + + public function test_subject_request_without_interact_is_invalid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + unset($data['interact']); + + $this->expectException(ValidationException::class); + $validator->validateRequest($data); + } + + public function test_request_without_access_token_or_subject_is_invalid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + unset($data['subject']); + + $this->expectException(ValidationException::class); + $validator->validateRequest($data); + } + + public function test_subject_request_with_empty_sub_ids_is_invalid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + $data['subject']['sub_ids'] = []; + + $this->expectException(ValidationException::class); + $validator->validateRequest($data); + } + + public function test_subject_request_with_unknown_format_is_invalid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + $data['subject']['sub_ids'][0]['format'] = 'email'; + + $this->expectException(ValidationException::class); + $validator->validateRequest($data); + } + + public function test_request_with_access_token_and_subject_is_valid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + $data['access_token'] = [ + 'access' => [ + [ + 'type' => 'quote', + 'actions' => ['create', 'read'], + ], + ], + ]; + + $this->expectNotToPerformAssertions(); + $validator->validateRequest($data); + } + + public function test_subject_request_with_invalid_interact_is_invalid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + $data['interact'] = ['finish' => $data['interact']['finish']]; + + $this->expectException(ValidationException::class); + $validator->validateRequest($data); + } + + public function test_request_with_interaction_dto_without_finish_is_valid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + $data['interact'] = (new GrantInteraction(['redirect']))->toArray(); + + $this->expectNotToPerformAssertions(); + $validator->validateRequest($data); + } + + private function subjectRequest(): array + { + return [ + 'subject' => [ + 'sub_ids' => [ + [ + 'id' => 'https://ilp.interledger-test.dev/alice', + 'format' => 'uri', + ], + ], + ], + 'client' => 'https://ilp.interledger-test.dev/interledger', + 'interact' => [ + 'start' => ['redirect'], + 'finish' => [ + 'method' => 'redirect', + 'uri' => 'https://example.com/finish', + 'nonce' => 'nonce-123', + ], + ], + ]; + } } From 3a9a12308f7a5fb436875845678d8f7e101afeb7 Mon Sep 17 00:00:00 2001 From: Adrian Boros Date: Wed, 7 Oct 2026 12:17:26 +0300 Subject: [PATCH 2/2] feat(grants): accept jwk client in grant requests --- CHANGELOG.md | 3 + lib/Services/GrantService.php | 16 +++- lib/Transformers/PendingGrantTransformer.php | 6 +- lib/Validators/Grant/RequestSchema.json | 62 ++++++++++++- tests/Services/GrantServiceTest.php | 91 ++++++++++++++++++ .../PendingGrantTransformerTest.php | 30 ++++++ tests/Validators/GrantValidatorTest.php | 92 +++++++++++++++++++ 7 files changed, 289 insertions(+), 11 deletions(-) create mode 100644 tests/Transformers/PendingGrantTransformerTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 38e8fb9..923c17b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,11 +21,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - GitHub Actions CI: PHPUnit, PHPStan and Pint - Grant requests for subject information (spec v1.3 wallet address ownership). A request needs `access_token`, or `interact` and `subject` - `Subject` and `SubjectId` models. `Grant::$subject` holds the `subject` returned by the auth server +- Grant requests can use a directed identity client: pass `'client' => ['jwk' => ...]` with the public key of the configured key pair. The spec allows this only for non-interactive grants (for example incoming payments), so a request with `jwk` and `interact` fails validation. A `jwk` with a private key (`d`) also fails validation +- The request schema accepts all spec v1.3 `client` forms: wallet address string, `['walletAddress' => ...]` and `['jwk' => ...]` ### Changed - `Grant::$access_token` is now nullable. It is `null` for a grant that only returns subject information. In that case `Grant::$subject` is set - `GrantContinue::$access_token` is now `AccessToken|SimpleAccessToken`. It is a `SimpleAccessToken` when the grant has no access token. Only `->value` is set for both types - These two property types changed, so code that uses static analysis may need a null check or an `instanceof` check +- `GrantService::request()` keeps a caller `client` only when it is a `jwk` object. Any other value is replaced with the configured wallet address, as before. `continue()` always sends the configured wallet address, as before - `GrantTransformer` throws `UnexpectedValueException` when a grant response has no valid `continue` field, or has neither `access_token` nor `subject` (grant still pending). Before, it threw an `InvalidArgumentException` or a `TypeError` ## [1.1.0] - 2026-06-13 diff --git a/lib/Services/GrantService.php b/lib/Services/GrantService.php index 215ae32..2a3998d 100644 --- a/lib/Services/GrantService.php +++ b/lib/Services/GrantService.php @@ -59,6 +59,20 @@ private function injectClientUrl(array &$grantStructure): void $grantStructure['client'] = $this->clientUrl; } + /** + * Injects the client URL into a grant request, + * unless the caller set a directed identity client (`['jwk' => ...]`). + * + * @param array $grantRequest The grant request to modify. + */ + private function injectClient(array &$grantRequest): void + { + if (is_array($grantRequest['client'] ?? null) && isset($grantRequest['client']['jwk'])) { + return; + } + $this->injectClientUrl($grantRequest); + } + /** * Requests a new grant or pending grant. * @@ -73,7 +87,7 @@ public function request(array $requestParams, array $grantRequest): Grant|Pendin if (! isset($requestParams['url'])) { throw new \InvalidArgumentException('Missing required data'); } - $this->injectClientUrl($grantRequest); + $this->injectClient($grantRequest); $this->validator->validateRequest($grantRequest); $url = $requestParams['url']; if (substr($url, -1) !== '/') { diff --git a/lib/Transformers/PendingGrantTransformer.php b/lib/Transformers/PendingGrantTransformer.php index b314664..3716f1c 100644 --- a/lib/Transformers/PendingGrantTransformer.php +++ b/lib/Transformers/PendingGrantTransformer.php @@ -25,11 +25,7 @@ public function createFromResponse(array|stdClass|ResponseInterface $response): // Create AccessToken instance for the continue field $continueData = $response['continue']; - $accessToken = new SimpleAccessToken( - $continueData['access_token']['value'], - '', // The 'manage' field is not applicable here. - null - ); + $accessToken = new SimpleAccessToken($continueData['access_token']['value']); // Create PendingGrantContinue instance $pendingGrantContinue = new PendingGrantContinue( diff --git a/lib/Validators/Grant/RequestSchema.json b/lib/Validators/Grant/RequestSchema.json index 95ed4b3..ad2c168 100644 --- a/lib/Validators/Grant/RequestSchema.json +++ b/lib/Validators/Grant/RequestSchema.json @@ -1,9 +1,26 @@ { "type": "object", "required": ["client"], - "anyOf": [ - { "required": ["access_token"] }, - { "required": ["interact", "subject"] } + "allOf": [ + { + "anyOf": [ + { "required": ["access_token"] }, + { "required": ["interact", "subject"] } + ] + }, + { + "description": "A jwk client can only be used for non-interactive grant requests", + "anyOf": [ + { "not": { "required": ["interact"] } }, + { + "properties": { + "client": { + "not": { "type": "object", "required": ["jwk"] } + } + } + } + ] + } ], "properties": { "access_token": { @@ -123,8 +140,43 @@ "additionalProperties": false }, "client": { - "type": "string", - "format": "uri" + "oneOf": [ + { + "type": "string", + "format": "uri" + }, + { + "type": "object", + "required": ["walletAddress"], + "properties": { + "walletAddress": { + "type": "string", + "format": "uri" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": ["jwk"], + "properties": { + "jwk": { + "type": "object", + "required": ["kid", "alg", "kty", "crv", "x"], + "properties": { + "kid": { "type": "string" }, + "alg": { "type": "string", "enum": ["EdDSA"] }, + "use": { "type": "string", "enum": ["sig"] }, + "kty": { "type": "string", "enum": ["OKP"] }, + "crv": { "type": "string", "enum": ["Ed25519"] }, + "x": { "type": "string", "pattern": "^[a-zA-Z0-9_-]{43}\\z" } + }, + "not": { "required": ["d"] } + } + }, + "additionalProperties": false + } + ] }, "interact": { "type": "object", diff --git a/tests/Services/GrantServiceTest.php b/tests/Services/GrantServiceTest.php index d2ee595..bf957a8 100644 --- a/tests/Services/GrantServiceTest.php +++ b/tests/Services/GrantServiceTest.php @@ -155,6 +155,97 @@ public function test_continue_returns_subject_grant(): void $this->assertSame('https://ilp.interledger-test.dev/alice', $result->subject->sub_ids[0]->id); } + public function test_request_injects_client_url_when_client_is_missing(): void + { + $this->apiClient + ->expects($this->once()) + ->method('request') + ->with('POST', 'https://ilp.interledger-test.dev/', $this->callback( + fn (array $body) => $body['client'] === 'https://ilp.interledger-test.dev/wallet' + )) + ->willReturn(['access_token' => 'xyz', 'continue' => '']); + $this->grantTransformer->method('createFromResponse')->willReturn($this->createMock(Grant::class)); + + $this->service->request(['url' => 'https://ilp.interledger-test.dev'], [ + 'access_token' => ['access' => [['type' => 'quote', 'actions' => ['create']]]], + ]); + } + + public function test_request_keeps_client_set_by_caller(): void + { + $client = ['jwk' => [ + 'kid' => 'key-1', + 'alg' => 'EdDSA', + 'kty' => 'OKP', + 'crv' => 'Ed25519', + 'x' => '11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo', + ]]; + $this->apiClient + ->expects($this->once()) + ->method('request') + ->with('POST', 'https://ilp.interledger-test.dev/', $this->callback( + fn (array $body) => $body['client'] === $client + )) + ->willReturn(['access_token' => 'xyz', 'continue' => '']); + $this->grantTransformer->method('createFromResponse')->willReturn($this->createMock(Grant::class)); + + $this->service->request(['url' => 'https://ilp.interledger-test.dev'], [ + 'access_token' => ['access' => [['type' => 'incoming-payment', 'actions' => ['create']]]], + 'client' => $client, + ]); + } + + public function test_request_replaces_client_that_is_not_a_jwk(): void + { + $this->apiClient + ->expects($this->once()) + ->method('request') + ->with('POST', 'https://ilp.interledger-test.dev/', $this->callback( + fn (array $body) => $body['client'] === 'https://ilp.interledger-test.dev/wallet' + )) + ->willReturn(['access_token' => 'xyz', 'continue' => '']); + $this->grantTransformer->method('createFromResponse')->willReturn($this->createMock(Grant::class)); + + $this->service->request(['url' => 'https://ilp.interledger-test.dev'], [ + 'access_token' => ['access' => [['type' => 'quote', 'actions' => ['create']]]], + 'client' => ['walletAddress' => 'https://ilp.interledger-test.dev/other'], + ]); + } + + public function test_request_injects_client_url_when_client_is_null(): void + { + $this->apiClient + ->expects($this->once()) + ->method('request') + ->with('POST', 'https://ilp.interledger-test.dev/', $this->callback( + fn (array $body) => $body['client'] === 'https://ilp.interledger-test.dev/wallet' + )) + ->willReturn(['access_token' => 'xyz', 'continue' => '']); + $this->grantTransformer->method('createFromResponse')->willReturn($this->createMock(Grant::class)); + + $this->service->request(['url' => 'https://ilp.interledger-test.dev'], [ + 'access_token' => ['access' => [['type' => 'quote', 'actions' => ['create']]]], + 'client' => null, + ]); + } + + public function test_continue_always_sends_configured_client(): void + { + $this->apiClient + ->expects($this->once()) + ->method('request') + ->with('POST', 'https://ilp.interledger-test.dev/continue/abc/', $this->callback( + fn (array $body) => $body['client'] === 'https://ilp.interledger-test.dev/wallet' + )) + ->willReturn(['access_token' => 'xyz']); + $this->grantTransformer->method('createFromResponse')->willReturn($this->createMock(Grant::class)); + + $this->service->continue([ + 'url' => 'https://ilp.interledger-test.dev/continue/abc', + 'access_token' => 'EAB18A53A6F67F9F1247', + ], ['interact_ref' => 'xyz', 'client' => ['jwk' => ['kid' => 'key-1']]]); + } + public function test_continue_throws_if_missing_url_or_token(): void { $this->expectException(\InvalidArgumentException::class); diff --git a/tests/Transformers/PendingGrantTransformerTest.php b/tests/Transformers/PendingGrantTransformerTest.php new file mode 100644 index 0000000..29df863 --- /dev/null +++ b/tests/Transformers/PendingGrantTransformerTest.php @@ -0,0 +1,30 @@ +createFromResponse([ + 'interact' => [ + 'redirect' => 'https://auth.interledger-test.dev/4CF492MLVMSW9MKMXKHQ', + 'finish' => '4105340a-05eb-4290-8739-f9e2b463bfa7', + ], + 'continue' => [ + 'access_token' => ['value' => '33OMUKMKSKU80UPRY5NM'], + 'uri' => 'https://auth.interledger-test.dev/continue/4CF492MLVMSW9MKMXKHQ', + 'wait' => 30, + ], + ]); + + $this->assertSame('33OMUKMKSKU80UPRY5NM', $pendingGrant->continue->access_token->value); + $this->assertSame('https://auth.interledger-test.dev/continue/4CF492MLVMSW9MKMXKHQ', $pendingGrant->continue->uri); + $this->assertSame(30, $pendingGrant->continue->wait); + } +} diff --git a/tests/Validators/GrantValidatorTest.php b/tests/Validators/GrantValidatorTest.php index e41a498..ea008e9 100644 --- a/tests/Validators/GrantValidatorTest.php +++ b/tests/Validators/GrantValidatorTest.php @@ -9,6 +9,15 @@ class GrantValidatorTest extends TestCase { + private const JWK = [ + 'kid' => 'key-1', + 'alg' => 'EdDSA', + 'use' => 'sig', + 'kty' => 'OKP', + 'crv' => 'Ed25519', + 'x' => '11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo', + ]; + public function test_valid_request(): void { $validator = new GrantValidator; @@ -141,6 +150,89 @@ public function test_request_with_interaction_dto_without_finish_is_valid(): voi $validator->validateRequest($data); } + public function test_client_wallet_address_object_is_valid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + $data['client'] = ['walletAddress' => 'https://ilp.interledger-test.dev/interledger']; + + $this->expectNotToPerformAssertions(); + $validator->validateRequest($data); + } + + public function test_client_jwk_object_is_valid(): void + { + $validator = new GrantValidator; + + $this->expectNotToPerformAssertions(); + $validator->validateRequest($this->jwkRequest(self::JWK)); + } + + public function test_client_jwk_with_wrong_alg_is_invalid(): void + { + $validator = new GrantValidator; + + $this->expectException(ValidationException::class); + $validator->validateRequest($this->jwkRequest(['alg' => 'RS256'] + self::JWK)); + } + + public function test_client_jwk_with_private_key_is_invalid(): void + { + $validator = new GrantValidator; + + $this->expectException(ValidationException::class); + $validator->validateRequest($this->jwkRequest(self::JWK + ['d' => 'private-key-value'])); + } + + public function test_client_jwk_with_trailing_newline_in_x_is_invalid(): void + { + $validator = new GrantValidator; + + $this->expectException(ValidationException::class); + $validator->validateRequest($this->jwkRequest(['x' => substr(self::JWK['x'], 0, 42)."\n"] + self::JWK)); + } + + public function test_client_jwk_with_interact_is_invalid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + $data['client'] = ['jwk' => self::JWK]; + + $this->expectException(ValidationException::class); + $validator->validateRequest($data); + } + + public function test_client_with_jwk_and_wallet_address_is_invalid(): void + { + $validator = new GrantValidator; + + $data = $this->subjectRequest(); + $data['client'] = [ + 'jwk' => self::JWK, + 'walletAddress' => 'https://ilp.interledger-test.dev/interledger', + ]; + + $this->expectException(ValidationException::class); + $validator->validateRequest($data); + } + + private function jwkRequest(array $jwk): array + { + return [ + 'access_token' => [ + 'access' => [ + [ + 'type' => 'incoming-payment', + 'actions' => ['create', 'read'], + ], + ], + ], + 'client' => ['jwk' => $jwk], + ]; + } + private function subjectRequest(): array { return [