Skip to content

Commit e85095c

Browse files
committed
Start with JWK
1 parent 7631f77 commit e85095c

9 files changed

Lines changed: 155 additions & 11 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\OpenID\Jwk\Factories;
6+
7+
use Jose\Component\Core\JWK;
8+
use Jose\Component\KeyManagement\JWKFactory;
9+
use SimpleSAML\OpenID\Jwk\JwkDecorator;
10+
11+
class JwkDecoratorFactory
12+
{
13+
/**
14+
* @param mixed[] $data
15+
*/
16+
public function fromData(array $data): JwkDecorator
17+
{
18+
return new JwkDecorator(
19+
new JWK($data),
20+
);
21+
}
22+
23+
/**
24+
* @param non-empty-string $path
25+
* @param mixed[] $additionalData
26+
*/
27+
public function fromPkcs1Or8KeyFile(
28+
string $path,
29+
?string $password = null,
30+
array $additionalData = [],
31+
): JwkDecorator {
32+
return new JwkDecorator(
33+
JWKFactory::createFromKeyFile($path, $password, $additionalData),
34+
);
35+
}
36+
37+
/**
38+
* @param non-empty-string $key
39+
* @param mixed[] $additionalData
40+
*/
41+
public function fromPkcs1Or8Key(
42+
string $key,
43+
?string $password = null,
44+
array $additionalData = [],
45+
): JwkDecorator {
46+
return new JwkDecorator(
47+
JWKFactory::createFromKey($key, $password, $additionalData),
48+
);
49+
}
50+
51+
/**
52+
* @param non-empty-string $path
53+
* @param mixed[] $additionalData
54+
*/
55+
public function fromPkcs12CertificateFile(
56+
string $path,
57+
string $password = '',
58+
array $additionalData = [],
59+
): JwkDecorator {
60+
return new JwkDecorator(
61+
JWKFactory::createFromPKCS12CertificateFile($path, $password, $additionalData),
62+
);
63+
}
64+
65+
/**
66+
* @param non-empty-string $path
67+
* @param mixed[] $additionalData
68+
*/
69+
public function fromX509CertificateFile(
70+
string $path,
71+
array $additionalData = [],
72+
): JwkDecorator {
73+
return new JwkDecorator(
74+
JWKFactory::createFromCertificateFile($path, $additionalData),
75+
);
76+
}
77+
78+
/**
79+
* @param non-empty-string $certificate
80+
* @param mixed[] $additionalData
81+
*/
82+
public function fromX509Certificate(
83+
string $certificate,
84+
array $additionalData = [],
85+
): JwkDecorator {
86+
return new JwkDecorator(
87+
JWKFactory::createFromCertificate($certificate, $additionalData),
88+
);
89+
}
90+
}

src/Jwk/JwkDecorator.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\OpenID\Jwk;
6+
7+
use Jose\Component\Core\JWK;
8+
9+
class JwkDecorator
10+
{
11+
public function __construct(
12+
protected readonly JWK $jwk,
13+
) {
14+
}
15+
16+
public function jwk(): JWK
17+
{
18+
return $this->jwk;
19+
}
20+
}

src/Jwks.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ public function jwsDecoratorBuilder(): JwsDecoratorBuilder
202202
{
203203
return $this->jwsDecoratorBuilder ??= $this->jwsDecoratorBuilderFactory()->build(
204204
$this->jwsSerializerManagerDecorator(),
205+
$this->algorithmManagerDecorator(),
206+
$this->helpers(),
205207
);
206208
}
207209

src/Jws/Factories/JwsDecoratorBuilderFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public function build(
1616
JwsSerializerManagerDecorator $jwsSerializerManagerDecorator,
1717
AlgorithmManagerDecorator $algorithmManagerDecorator,
1818
Helpers $helpers,
19-
): JwsDecoratorBuilder
20-
{
19+
): JwsDecoratorBuilder {
2120
return new JwsDecoratorBuilder(
2221
$jwsSerializerManagerDecorator,
2322
new JWSBuilder($algorithmManagerDecorator->algorithmManager()),

src/Jws/JwsDecoratorBuilder.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
namespace SimpleSAML\OpenID\Jws;
66

77
use Jose\Component\Signature\JWSBuilder;
8+
use SimpleSAML\OpenID\Algorithms\SignatureAlgorithmEnum;
9+
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
810
use SimpleSAML\OpenID\Exceptions\JwsException;
911
use SimpleSAML\OpenID\Helpers;
12+
use SimpleSAML\OpenID\Jwk\JwkDecorator;
1013
use SimpleSAML\OpenID\Serializers\JwsSerializerManagerDecorator;
1114
use Throwable;
1215

@@ -30,4 +33,34 @@ public function fromToken(string $token): JwsDecorator
3033
throw new JwsException('Unable to parse token.', (int)$throwable->getCode(), $throwable);
3134
}
3235
}
36+
37+
/**
38+
* @param array<non-empty-string,mixed> $payload
39+
* @param array<non-empty-string,mixed> $header
40+
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
41+
*/
42+
public function fromData(
43+
JwkDecorator $signatureJwkDecorator,
44+
SignatureAlgorithmEnum $signatureAlgorithm,
45+
array $payload,
46+
array $header,
47+
): JwsDecorator {
48+
$header = array_merge(
49+
$header,
50+
[ClaimsEnum::Alg->value => $signatureAlgorithm->value],
51+
);
52+
53+
try {
54+
return new JwsDecorator(
55+
$this->jwsBuilder->create()->withPayload(
56+
$this->helpers->json()->encode($payload),
57+
)->addSignature(
58+
$signatureJwkDecorator->jwk(),
59+
$header,
60+
)->build(),
61+
);
62+
} catch (Throwable $throwable) {
63+
throw new JwsException('Unable to build JWS.', (int)$throwable->getCode(), $throwable);
64+
}
65+
}
3366
}

src/VerifiableCredentials/Factories/JwtVcJsonFactory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
namespace SimpleSAML\OpenID\VerifiableCredentials\Factories;
66

77
use SimpleSAML\OpenID\Jws\Factories\ParsedJwsFactory;
8-
use SimpleSAML\OpenID\VerifiableCredentials\JwtVcJson;
98

109
class JwtVcJsonFactory extends ParsedJwsFactory
1110
{
12-
public function fromData(): JwtVcJson
13-
{
14-
}
15-
}
11+
// TODO mivanci Continue
12+
// public function fromData(): JwtVcJson
13+
// {
14+
// return new JwtVcJson();
15+
// }
16+
}

src/VerifiableCredentials/JwtVcJson.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ public function getCredentialFormatIdentifier(): CredentialFormatIdentifiersEnum
1313
{
1414
return CredentialFormatIdentifiersEnum::JwtVcJson;
1515
}
16-
}
16+
}

src/VerifiableCredentials/VerifiableCredentialInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
interface VerifiableCredentialInterface
88
{
9-
10-
}
9+
}

tests/src/FederationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
use SimpleSAML\OpenID\Jws\Factories\JwsDecoratorBuilderFactory;
3939
use SimpleSAML\OpenID\Jws\Factories\JwsVerifierDecoratorFactory;
4040
use SimpleSAML\OpenID\Jws\Factories\ParsedJwsFactory;
41-
use SimpleSAML\OpenID\Jws\JwsFetcher;
4241
use SimpleSAML\OpenID\Jws\JwsDecoratorBuilder;
42+
use SimpleSAML\OpenID\Jws\JwsFetcher;
4343
use SimpleSAML\OpenID\Jws\JwsVerifierDecorator;
4444
use SimpleSAML\OpenID\Serializers\JwsSerializerManagerDecorator;
4545
use SimpleSAML\OpenID\SupportedAlgorithms;

0 commit comments

Comments
 (0)