Skip to content

Commit 68b189d

Browse files
committed
WIP JwtVcJson
1 parent be52cdd commit 68b189d

9 files changed

Lines changed: 249 additions & 75 deletions

File tree

src/Codebooks/ClaimsEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ enum ClaimsEnum: string
4444
case CredentialEndpoint = 'credential_endpoint';
4545
case CredentialIssuer = 'credential_issuer';
4646
case CredentialResponseEncryption = 'credential_response_encryption';
47+
case Credential_Schema = 'credentialSchema';
4748
// CredentialSigningAlgorithmValuesSupported
4849
case CredentialSigningAlgValuesSupported = 'credential_signing_alg_values_supported';
4950
case Credential_Status = 'credentialStatus';
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims;
6+
7+
use SimpleSAML\OpenID\Claims\ClaimInterface;
8+
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
9+
10+
abstract class AbstractIdentifiedTypedClaimValue implements ClaimInterface
11+
{
12+
/** @var non-empty-array<mixed> */
13+
protected array $data;
14+
15+
/**
16+
* @param non-empty-string $id,
17+
* @param non-empty-string $type
18+
* @param mixed[] $otherClaims
19+
*/
20+
public function __construct(
21+
protected string $id,
22+
protected string $type,
23+
array $otherClaims = [],
24+
) {
25+
$this->data = array_merge(
26+
$otherClaims,
27+
[ClaimsEnum::Id->value => $this->$id],
28+
[ClaimsEnum::Type->value => $this->type],
29+
);
30+
}
31+
32+
/**
33+
* @return non-empty-string
34+
*/
35+
public function getId(): string
36+
{
37+
return $this->id;
38+
}
39+
40+
/**
41+
* @return non-empty-string
42+
*/
43+
public function getType(): string
44+
{
45+
return $this->type;
46+
}
47+
48+
public function getKey(int|string $key): mixed
49+
{
50+
return $this->data[$key] ?? null;
51+
}
52+
53+
abstract public function getName(): string;
54+
55+
/**
56+
* @return non-empty-array<mixed>
57+
*/
58+
public function getValue(): array
59+
{
60+
return $this->data;
61+
}
62+
63+
/**
64+
* @return non-empty-array<mixed>
65+
*/
66+
public function jsonSerialize(): array
67+
{
68+
return $this->getValue();
69+
}
70+
}

src/VerifiableCredentials/VcDataModel/Claims/VcClaimValue.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626
protected readonly ?VcProofClaimValue $proofClaimValue,
2727
protected readonly ?DateTimeImmutable $expirationDate,
2828
protected readonly ?VcCredentialStatusClaimValue $credentialStatusClaimValue,
29+
protected readonly ?VcCredentialSchemaClaimBag $credentialSchemaClaimBag,
2930
) {
3031
}
3132

@@ -88,6 +89,11 @@ public function getCredentialStatus(): ?VcCredentialStatusClaimValue
8889
return $this->credentialStatusClaimValue;
8990
}
9091

92+
public function getCredentialSchema(): ?VcCredentialSchemaClaimBag
93+
{
94+
return $this->credentialSchemaClaimBag;
95+
}
96+
9197
public function getName(): string
9298
{
9399
return ClaimsEnum::Vc->value;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims;
6+
7+
use SimpleSAML\OpenID\Claims\ClaimInterface;
8+
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
9+
10+
class VcCredentialSchemaClaimBag implements ClaimInterface
11+
{
12+
/** @var \SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims\VcCredentialSchemaClaimValue[] */
13+
protected array $vcCredentialSchemaClaimValueValues;
14+
15+
public function __construct(
16+
VcCredentialSchemaClaimValue $vcCredentialSchemaClaimValue,
17+
VcCredentialSchemaClaimValue ...$vcCredentialSchemaClaimValueValues,
18+
) {
19+
$this->vcCredentialSchemaClaimValueValues = [
20+
$vcCredentialSchemaClaimValue,
21+
...$vcCredentialSchemaClaimValueValues,
22+
];
23+
}
24+
25+
/**
26+
* @return mixed[]
27+
*/
28+
public function jsonSerialize(): array
29+
{
30+
return $this->getValue();
31+
}
32+
33+
public function getName(): string
34+
{
35+
return ClaimsEnum::Credential_Schema->value;
36+
}
37+
38+
/**
39+
* @return mixed[]
40+
*/
41+
public function getValue(): array
42+
{
43+
return array_map(
44+
fn(
45+
VcCredentialSchemaClaimValue $vcCredentialSchemaClaimValue,
46+
): array => $vcCredentialSchemaClaimValue->jsonSerialize(),
47+
$this->vcCredentialSchemaClaimValueValues,
48+
);
49+
}
50+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims;
6+
7+
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
8+
9+
class VcCredentialSchemaClaimValue extends AbstractIdentifiedTypedClaimValue
10+
{
11+
public function getName(): string
12+
{
13+
return ClaimsEnum::Credential_Schema->value;
14+
}
15+
}

src/VerifiableCredentials/VcDataModel/Claims/VcCredentialStatusClaimValue.php

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,12 @@
44

55
namespace SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims;
66

7-
use SimpleSAML\OpenID\Claims\ClaimInterface;
87
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
98

10-
class VcCredentialStatusClaimValue implements ClaimInterface
9+
class VcCredentialStatusClaimValue extends AbstractIdentifiedTypedClaimValue
1110
{
12-
/** @var non-empty-array<mixed> */
13-
protected array $data;
14-
15-
/**
16-
* @param non-empty-string $id,
17-
* @param non-empty-string $type
18-
* @param mixed[] $otherClaims
19-
*/
20-
public function __construct(
21-
protected string $id,
22-
protected string $type,
23-
array $otherClaims = [],
24-
) {
25-
$this->data = array_merge(
26-
$otherClaims,
27-
[ClaimsEnum::Id->value => $this->$id],
28-
[ClaimsEnum::Type->value => $this->type],
29-
);
30-
}
31-
32-
/**
33-
* @return non-empty-string
34-
*/
35-
public function getId(): string
36-
{
37-
return $this->id;
38-
}
39-
40-
/**
41-
* @return non-empty-string
42-
*/
43-
public function getType(): string
44-
{
45-
return $this->type;
46-
}
47-
48-
public function getKey(int|string $key): mixed
49-
{
50-
return $this->data[$key] ?? null;
51-
}
52-
5311
public function getName(): string
5412
{
5513
return ClaimsEnum::Credential_Status->value;
5614
}
57-
58-
/**
59-
* @return non-empty-array<mixed>
60-
*/
61-
public function getValue(): array
62-
{
63-
return $this->data;
64-
}
65-
66-
/**
67-
* @return non-empty-array<mixed>
68-
*/
69-
public function jsonSerialize(): array
70-
{
71-
return $this->getValue();
72-
}
7315
}

src/VerifiableCredentials/VcDataModel/Factories/VcDataModelClaimFactory.php

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use SimpleSAML\OpenID\Helpers;
1212
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims\VcAtContextClaimValue;
1313
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims\VcClaimValue;
14+
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims\VcCredentialSchemaClaimBag;
15+
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims\VcCredentialSchemaClaimValue;
1416
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims\VcCredentialStatusClaimValue;
1517
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims\VcCredentialSubjectClaimBag;
1618
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Claims\VcCredentialSubjectClaimValue;
@@ -39,6 +41,7 @@ public function buildVcClaimValue(
3941
?VcProofClaimValue $vcProofClaimValue,
4042
?DateTimeImmutable $vcExpirationDate,
4143
?VcCredentialStatusClaimValue $vcCredentialStatusClaimValue,
44+
?VcCredentialSchemaClaimBag $vcCredentialSchemaClaimBag,
4245
): VcClaimValue {
4346
return new VcClaimValue(
4447
$vcAtContextClaimValue,
@@ -50,6 +53,7 @@ public function buildVcClaimValue(
5053
$vcProofClaimValue,
5154
$vcExpirationDate,
5255
$vcCredentialStatusClaimValue,
56+
$vcCredentialSchemaClaimBag,
5357
);
5458
}
5559

@@ -71,10 +75,17 @@ public function buildVcCredentialSubjectClaimValue(array $data): VcCredentialSub
7175
}
7276

7377
/**
74-
* @param non-empty-array<non-empty-array<mixed>> $data
78+
* @param mixed[] $data
79+
* @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException
7580
*/
7681
public function buildVcCredentialSubjectClaimBag(array $data): VcCredentialSubjectClaimBag
7782
{
83+
if ($this->helpers->arr()->isAssociative($data)) {
84+
$data = [$data];
85+
}
86+
87+
$data = $this->helpers->type()->enforceNonEmptyArrayOfNonEmptyArrays($data);
88+
7889
$vcCredentialSubjectClaimValueData = array_shift($data);
7990

8091
$vcCredentialSubjectClaimValue = $this->buildVcCredentialSubjectClaimValue($vcCredentialSubjectClaimValueData);
@@ -91,14 +102,14 @@ public function buildVcCredentialSubjectClaimBag(array $data): VcCredentialSubje
91102
}
92103

93104
/**
94-
* @param non-empty-array<mixed> $data
105+
* @param mixed[] $data
95106
* @throws \SimpleSAML\OpenID\Exceptions\VcDataModelException
96107
* @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException
97108
*/
98109
public function buildVcIssuerClaimValue(array $data): VcIssuerClaimValue
99110
{
100111
$id = $data[ClaimsEnum::Id->value] ?? throw new VcDataModelException(
101-
'No Issuer ID claim value available.',
112+
'No ID claim value available.',
102113
);
103114

104115
$id = $this->helpers->type()->enforceUri($id);
@@ -107,7 +118,7 @@ public function buildVcIssuerClaimValue(array $data): VcIssuerClaimValue
107118
}
108119

109120
/**
110-
* @param non-empty-array<mixed> $data
121+
* @param mixed[] $data
111122
* @throws \SimpleSAML\OpenID\Exceptions\VcDataModelException
112123
* @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException
113124
*/
@@ -123,14 +134,14 @@ public function buildVcProofClaimValue(array $data): VcProofClaimValue
123134
}
124135

125136
/**
126-
* @param non-empty-array<mixed> $data
137+
* @param mixed[] $data
127138
* @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException
128139
* @throws \SimpleSAML\OpenID\Exceptions\VcDataModelException
129140
*/
130141
public function buildVcCredentialStatusClaimValue(array $data): VcCredentialStatusClaimValue
131142
{
132143
$id = $data[ClaimsEnum::Id->value] ?? throw new VcDataModelException(
133-
'No Issuer ID claim value available.',
144+
'No ID claim value available.',
134145
);
135146
$id = $this->helpers->type()->enforceUri($id);
136147

@@ -141,4 +152,52 @@ public function buildVcCredentialStatusClaimValue(array $data): VcCredentialStat
141152

142153
return new VcCredentialStatusClaimValue($id, $type, $data);
143154
}
155+
156+
/**
157+
* @param non-empty-array<mixed> $data
158+
* @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException
159+
* @throws \SimpleSAML\OpenID\Exceptions\VcDataModelException
160+
*/
161+
public function buildVcCredentialSchemaClaimValue(array $data): VcCredentialSchemaClaimValue
162+
{
163+
$id = $data[ClaimsEnum::Id->value] ?? throw new VcDataModelException(
164+
'No ID claim value available.',
165+
);
166+
$id = $this->helpers->type()->enforceUri($id);
167+
168+
$type = $data[ClaimsEnum::Type->value] ?? throw new VcDataModelException(
169+
'No Type claim value available.',
170+
);
171+
$type = $this->helpers->type()->ensureNonEmptyString($type);
172+
173+
return new VcCredentialSchemaClaimValue($id, $type, $data);
174+
}
175+
176+
/**
177+
* @param mixed[] $data
178+
* @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException
179+
* @throws \SimpleSAML\OpenID\Exceptions\VcDataModelException
180+
*/
181+
public function buildVcCredentialSchemaClaimBag(array $data): VcCredentialSchemaClaimBag
182+
{
183+
if ($this->helpers->arr()->isAssociative($data)) {
184+
$data = [$data];
185+
}
186+
187+
$data = $this->helpers->type()->enforceNonEmptyArrayOfNonEmptyArrays($data);
188+
189+
$vcCredentialSchemaClaimValueData = array_shift($data);
190+
191+
$vcCredentialSchemaClaimValue = $this->buildVcCredentialSchemaClaimValue($vcCredentialSchemaClaimValueData);
192+
193+
$vcCredentialSchemaClaimValues = array_map(
194+
fn (array $data): VcCredentialSchemaClaimValue => $this->buildVcCredentialSchemaClaimValue($data),
195+
$data,
196+
);
197+
198+
return new VcCredentialSchemaClaimBag(
199+
$vcCredentialSchemaClaimValue,
200+
...$vcCredentialSchemaClaimValues,
201+
);
202+
}
144203
}

0 commit comments

Comments
 (0)