Skip to content

Commit c9e6b39

Browse files
committed
Update VcSdJwtFactory
1 parent 3654f95 commit c9e6b39

3 files changed

Lines changed: 67 additions & 5 deletions

File tree

src/VerifiableCredentials.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ public function vcSdJwtFactory(): VcSdJwtFactory
251251
$this->timestampValidationLeewayDecorator,
252252
$this->helpers(),
253253
$this->claimFactory(),
254+
$this->disclosureFactory(),
254255
);
255256
}
256257

src/VerifiableCredentials/VcDataModel2/Factories/VcSdJwtFactory.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
use SimpleSAML\OpenID\Algorithms\SignatureAlgorithmEnum;
88
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
9+
use SimpleSAML\OpenID\Codebooks\HashAlgorithmsEnum;
910
use SimpleSAML\OpenID\Codebooks\JwtTypesEnum;
1011
use SimpleSAML\OpenID\Jwk\JwkDecorator;
11-
use SimpleSAML\OpenID\Jws\Factories\ParsedJwsFactory;
12+
use SimpleSAML\OpenID\SdJwt\DisclosureBag;
13+
use SimpleSAML\OpenID\SdJwt\Factories\SdJwtFactory;
14+
use SimpleSAML\OpenID\SdJwt\KbJwt;
1215
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel2\VcSdJwt;
1316

14-
class VcSdJwtFactory extends ParsedJwsFactory
17+
class VcSdJwtFactory extends SdJwtFactory
1518
{
1619
public function fromToken(string $token): VcSdJwt
1720
{
@@ -31,14 +34,25 @@ public function fromToken(string $token): VcSdJwt
3134
* @param array<non-empty-string,mixed> $payload
3235
* @param array<non-empty-string,mixed> $header
3336
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
37+
* @throws \SimpleSAML\OpenID\Exceptions\OpenIdException
3438
*/
3539
public function fromData(
3640
JwkDecorator $signingKey,
3741
SignatureAlgorithmEnum $signatureAlgorithm,
3842
array $payload,
3943
array $header,
44+
?DisclosureBag $disclosureBag = null,
45+
?KbJwt $kbJwt = null,
46+
JwtTypesEnum $jwtTypesEnum = JwtTypesEnum::VcSdJwt,
47+
HashAlgorithmsEnum $hashAlgorithmsEnum = HashAlgorithmsEnum::SHA_256,
4048
): VcSdJwt {
41-
$header[ClaimsEnum::Typ->value] = JwtTypesEnum::VcSdJwt->value;
49+
$header[ClaimsEnum::Typ->value] = $jwtTypesEnum->value;
50+
51+
if ($disclosureBag instanceof DisclosureBag) {
52+
$payload = $this->updatePayloadWithDisclosures($payload, $disclosureBag, $hashAlgorithmsEnum);
53+
}
54+
55+
/** @var array<non-empty-string,mixed> $payload */
4256

4357
return new VcSdJwt(
4458
$this->jwsDecoratorBuilder->fromData(
@@ -53,6 +67,8 @@ public function fromData(
5367
$this->timestampValidationLeeway,
5468
$this->helpers,
5569
$this->claimFactory,
70+
$disclosureBag,
71+
$kbJwt,
5672
);
5773
}
5874
}

tests/src/VerifiableCredentials/VcDataModel2/Factories/VcSdJwtFactoryTest.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616
use SimpleSAML\OpenID\Helpers;
1717
use SimpleSAML\OpenID\Jwk\JwkDecorator;
1818
use SimpleSAML\OpenID\Jwks\Factories\JwksDecoratorFactory;
19-
use SimpleSAML\OpenID\Jws\Factories\ParsedJwsFactory;
2019
use SimpleSAML\OpenID\Jws\JwsDecorator;
2120
use SimpleSAML\OpenID\Jws\JwsDecoratorBuilder;
2221
use SimpleSAML\OpenID\Jws\JwsVerifierDecorator;
2322
use SimpleSAML\OpenID\Jws\ParsedJws;
23+
use SimpleSAML\OpenID\SdJwt\DisclosureBag;
24+
use SimpleSAML\OpenID\SdJwt\Factories\DisclosureFactory;
25+
use SimpleSAML\OpenID\SdJwt\Factories\SdJwtFactory;
2426
use SimpleSAML\OpenID\Serializers\JwsSerializerManagerDecorator;
2527
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel2\Factories\VcSdJwtFactory;
2628
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel2\VcSdJwt;
2729

2830
#[CoversClass(VcSdJwtFactory::class)]
29-
#[UsesClass(ParsedJwsFactory::class)]
31+
#[UsesClass(SdJwtFactory::class)]
3032
#[UsesClass(VcSdJwt::class)]
3133
#[UsesClass(ParsedJws::class)]
3234
#[UsesClass(DateIntervalDecorator::class)]
@@ -50,6 +52,8 @@ final class VcSdJwtFactoryTest extends TestCase
5052

5153
protected \PHPUnit\Framework\MockObject\Stub&ClaimFactory $claimFactoryMock;
5254

55+
protected \PHPUnit\Framework\MockObject\Stub&DisclosureFactory $disclosureFactoryMock;
56+
5357

5458
protected function setUp(): void
5559
{
@@ -60,6 +64,7 @@ protected function setUp(): void
6064
$this->dateIntervalDecorator = new DateIntervalDecorator(new \DateInterval('PT0S'));
6165
$this->helpers = new Helpers();
6266
$this->claimFactoryMock = $this->createStub(ClaimFactory::class);
67+
$this->disclosureFactoryMock = $this->createStub(DisclosureFactory::class);
6368
}
6469

6570

@@ -73,6 +78,7 @@ protected function sut(): VcSdJwtFactory
7378
$this->dateIntervalDecorator,
7479
$this->helpers,
7580
$this->claimFactoryMock,
81+
$this->disclosureFactoryMock,
7682
);
7783
}
7884

@@ -147,4 +153,43 @@ public function testCanBuildFromData(): void
147153
),
148154
);
149155
}
156+
157+
158+
public function testCanBuildFromDataWithDisclosureBag(): void
159+
{
160+
$signingKey = $this->createStub(JwkDecorator::class);
161+
$signatureAlgorithm = SignatureAlgorithmEnum::RS256;
162+
$payload = ['foo' => 'bar'];
163+
$header = ['alg' => 'RS256'];
164+
$disclosureBag = $this->createStub(DisclosureBag::class);
165+
$disclosureBag->method('all')->willReturn([]);
166+
167+
$jwsDecoratorMock = $this->createJwsDecoratorMock($payload);
168+
169+
$this->jwsDecoratorBuilderMock
170+
->expects($this->once())
171+
->method('fromData')
172+
->with(
173+
$signingKey,
174+
$signatureAlgorithm,
175+
$payload,
176+
$this->callback(function (array $header): true {
177+
$this->assertArrayHasKey(ClaimsEnum::Typ->value, $header);
178+
$this->assertSame(JwtTypesEnum::VcSdJwt->value, $header[ClaimsEnum::Typ->value]);
179+
return true;
180+
}),
181+
)
182+
->willReturn($jwsDecoratorMock);
183+
184+
$vcSdJwt = $this->sut()->fromData(
185+
$signingKey,
186+
$signatureAlgorithm,
187+
$payload,
188+
$header,
189+
$disclosureBag,
190+
);
191+
192+
$this->assertInstanceOf(VcSdJwt::class, $vcSdJwt);
193+
$this->assertSame($disclosureBag, $vcSdJwt->getDisclosureBag());
194+
}
150195
}

0 commit comments

Comments
 (0)