Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ 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
- 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

Expand Down
7 changes: 5 additions & 2 deletions lib/Models/Grant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions lib/Models/GrantContinue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions lib/Models/Subject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace OpenPayments\Models;

class Subject
{
/** @var SubjectId[] */
public readonly array $sub_ids;

/**
* @param SubjectId[] $sub_ids
*/
public function __construct(array $sub_ids)
{
$this->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),
];
}
}
29 changes: 29 additions & 0 deletions lib/Models/SubjectId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace OpenPayments\Models;

class SubjectId
{
public readonly string $id;

public readonly string $format;

public function __construct(string $id, string $format)
{
$this->id = $id;
$this->format = $format;
}

/**
* Convert the model to an associative array.
*/
public function toArray(): array
{
return [
'id' => $this->id,
'format' => $this->format,
];
}
}
16 changes: 15 additions & 1 deletion lib/Services/GrantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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) !== '/') {
Expand Down
36 changes: 35 additions & 1 deletion lib/Transformers/GrantTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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];
Expand Down Expand Up @@ -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);
}
}
6 changes: 1 addition & 5 deletions lib/Transformers/PendingGrantTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
116 changes: 113 additions & 3 deletions lib/Validators/Grant/RequestSchema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
{
"type": "object",
"required": ["access_token", "client"],
"required": ["client"],
"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": {
"type": "object",
Expand Down Expand Up @@ -119,8 +140,97 @@
"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",
"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": {
Expand Down
Loading
Loading