Skip to content

Commit c078f83

Browse files
committed
Consolidate Jwks factory methods
1 parent b7e6596 commit c078f83

File tree

2 files changed

+136
-82
lines changed

2 files changed

+136
-82
lines changed

src/Jwks.php

Lines changed: 108 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,39 @@
2727

2828
class Jwks
2929
{
30-
protected DateIntervalDecorator $maxCacheDuration;
31-
protected DateIntervalDecorator $timestampValidationLeeway;
30+
protected DateIntervalDecorator $maxCacheDurationDecorator;
31+
protected DateIntervalDecorator $timestampValidationLeewayDecorator;
3232
protected ?CacheDecorator $cacheDecorator;
3333
protected ?JwksFetcher $jwksFetcher = null;
3434
protected HttpClientDecorator $httpClientDecorator;
35-
protected JwsSerializerManager $jwsSerializerManager;
36-
protected JwsParser $jwsParser;
37-
protected JwsVerifier $jwsVerifier;
35+
protected ?JwsSerializerManager $jwsSerializerManager = null;
36+
protected ?JwsParser $jwsParser = null;
37+
protected ?JwsVerifier $jwsVerifier = null;
3838
protected ?JwksFactory $jwksFactory = null;
3939
protected ?SignedJwksFactory $signedJwksFactory = null;
40+
protected ?Helpers $helpers = null;
41+
protected ?AlgorithmManagerFactory $algorithmManagerFactory = null;
42+
protected ?JwsSerializerManagerFactory $jwsSerializerManagerFactory = null;
43+
protected ?JwsParserFactory $jwsParserFactory = null;
44+
protected ?JwsVerifierFactory $jwsVerifierFactory = null;
45+
protected ?DateIntervalDecoratorFactory $dateIntervalDecoratorFactory = null;
46+
protected ?CacheDecoratorFactory $cacheDecoratorFactory = null;
47+
protected ?HttpClientDecoratorFactory $httpClientDecoratorFactory = null;
4048

4149
public function __construct(
4250
protected readonly SupportedAlgorithms $supportedAlgorithms = new SupportedAlgorithms(),
4351
protected readonly SupportedSerializers $supportedSerializers = new SupportedSerializers(),
4452
DateInterval $maxCacheDuration = new DateInterval('PT1H'),
53+
DateInterval $timestampValidationLeeway = new DateInterval('PT1M'),
4554
?CacheInterface $cache = null,
46-
?Client $httpClient = null,
4755
protected readonly ?LoggerInterface $logger = null,
48-
protected readonly Helpers $helpers = new Helpers(),
49-
AlgorithmManagerFactory $algorithmManagerFactory = new AlgorithmManagerFactory(),
50-
JwsSerializerManagerFactory $jwsSerializerManagerFactory = new JwsSerializerManagerFactory(),
51-
JwsParserFactory $jwsParserFactory = new JwsParserFactory(),
52-
JwsVerifierFactory $jwsVerifierFactory = new JwsVerifierFactory(),
53-
DateInterval $timestampValidationLeeway = new DateInterval('PT1M'),
54-
DateIntervalDecoratorFactory $dateIntervalDecoratorFactory = new DateIntervalDecoratorFactory(),
55-
CacheDecoratorFactory $cacheDecoratorFactory = new CacheDecoratorFactory(),
56-
HttpClientDecoratorFactory $httpClientDecoratorFactory = new HttpClientDecoratorFactory(),
56+
?Client $httpClient = null,
5757
) {
58-
$this->maxCacheDuration = $dateIntervalDecoratorFactory->build($maxCacheDuration);
59-
$this->timestampValidationLeeway = $dateIntervalDecoratorFactory->build($timestampValidationLeeway);
60-
$this->cacheDecorator = is_null($cache) ? null : $cacheDecoratorFactory->build($cache);
61-
$this->httpClientDecorator = $httpClientDecoratorFactory->build($httpClient);
62-
63-
$this->jwsSerializerManager = $jwsSerializerManagerFactory->build($this->supportedSerializers);
64-
$this->jwsParser = $jwsParserFactory->build($this->jwsSerializerManager);
65-
$this->jwsVerifier = $jwsVerifierFactory->build($algorithmManagerFactory->build($this->supportedAlgorithms));
58+
$this->maxCacheDurationDecorator = $this->dateIntervalDecoratorFactory()->build($maxCacheDuration);
59+
$this->timestampValidationLeewayDecorator = $this->dateIntervalDecoratorFactory()
60+
->build($timestampValidationLeeway);
61+
$this->cacheDecorator = is_null($cache) ? null : $this->cacheDecoratorFactory()->build($cache);
62+
$this->httpClientDecorator = $this->httpClientDecoratorFactory()->build($httpClient);
6663
}
6764

6865
public function jwksFactory(): JwksFactory
@@ -73,12 +70,12 @@ public function jwksFactory(): JwksFactory
7370
public function signedJwksFactory(): SignedJwksFactory
7471
{
7572
return $this->signedJwksFactory ??= new SignedJwksFactory(
76-
$this->jwsParser,
77-
$this->jwsVerifier,
73+
$this->jwsParser(),
74+
$this->jwsVerifier(),
7875
$this->jwksFactory(),
79-
$this->jwsSerializerManager,
80-
$this->timestampValidationLeeway,
81-
$this->helpers,
76+
$this->jwsSerializerManager(),
77+
$this->timestampValidationLeewayDecorator,
78+
$this->helpers(),
8279
);
8380
}
8481

@@ -88,10 +85,91 @@ public function jwksFetcher(): JwksFetcher
8885
$this->httpClientDecorator,
8986
$this->jwksFactory(),
9087
$this->signedJwksFactory(),
91-
$this->maxCacheDuration,
88+
$this->maxCacheDurationDecorator,
9289
$this->cacheDecorator,
9390
$this->logger,
94-
$this->helpers,
91+
$this->helpers(),
9592
);
9693
}
94+
95+
public function helpers(): Helpers
96+
{
97+
return $this->helpers ??= new Helpers();
98+
}
99+
100+
public function algorithmManagerFactory(): AlgorithmManagerFactory
101+
{
102+
if (is_null($this->algorithmManagerFactory)) {
103+
$this->algorithmManagerFactory = new AlgorithmManagerFactory();
104+
}
105+
return $this->algorithmManagerFactory;
106+
}
107+
108+
public function jwsSerializerManagerFactory(): JwsSerializerManagerFactory
109+
{
110+
if (is_null($this->jwsSerializerManagerFactory)) {
111+
$this->jwsSerializerManagerFactory = new JwsSerializerManagerFactory();
112+
}
113+
return $this->jwsSerializerManagerFactory;
114+
}
115+
116+
public function jwsParserFactory(): JwsParserFactory
117+
{
118+
if (is_null($this->jwsParserFactory)) {
119+
$this->jwsParserFactory = new JwsParserFactory();
120+
}
121+
return $this->jwsParserFactory;
122+
}
123+
124+
public function jwsVerifierFactory(): JwsVerifierFactory
125+
{
126+
if (is_null($this->jwsVerifierFactory)) {
127+
$this->jwsVerifierFactory = new JwsVerifierFactory();
128+
}
129+
return $this->jwsVerifierFactory;
130+
}
131+
132+
public function dateIntervalDecoratorFactory(): DateIntervalDecoratorFactory
133+
{
134+
if (is_null($this->dateIntervalDecoratorFactory)) {
135+
$this->dateIntervalDecoratorFactory = new DateIntervalDecoratorFactory();
136+
}
137+
138+
return $this->dateIntervalDecoratorFactory;
139+
}
140+
141+
public function cacheDecoratorFactory(): CacheDecoratorFactory
142+
{
143+
if (is_null($this->cacheDecoratorFactory)) {
144+
$this->cacheDecoratorFactory = new CacheDecoratorFactory();
145+
}
146+
147+
return $this->cacheDecoratorFactory;
148+
}
149+
150+
public function httpClientDecoratorFactory(): HttpClientDecoratorFactory
151+
{
152+
if (is_null($this->httpClientDecoratorFactory)) {
153+
$this->httpClientDecoratorFactory = new HttpClientDecoratorFactory();
154+
}
155+
156+
return $this->httpClientDecoratorFactory;
157+
}
158+
159+
public function jwsVerifier(): JwsVerifier
160+
{
161+
return $this->jwsVerifier ??= $this->jwsVerifierFactory()->build(
162+
$this->algorithmManagerFactory()->build($this->supportedAlgorithms),
163+
);
164+
}
165+
166+
public function jwsParser(): JwsParser
167+
{
168+
return $this->jwsParser ??= $this->jwsParserFactory()->build($this->jwsSerializerManager());
169+
}
170+
171+
public function jwsSerializerManager(): JwsSerializerManager
172+
{
173+
return $this->jwsSerializerManager ??= $this->jwsSerializerManagerFactory()->build($this->supportedSerializers);
174+
}
97175
}

tests/src/JwksTest.php

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use PHPUnit\Framework\TestCase;
1313
use Psr\Log\LoggerInterface;
1414
use Psr\SimpleCache\CacheInterface;
15+
use SimpleSAML\OpenID\Decorators\CacheDecorator;
16+
use SimpleSAML\OpenID\Decorators\DateIntervalDecorator;
17+
use SimpleSAML\OpenID\Decorators\HttpClientDecorator;
1518
use SimpleSAML\OpenID\Factories\AlgorithmManagerFactory;
1619
use SimpleSAML\OpenID\Factories\CacheDecoratorFactory;
1720
use SimpleSAML\OpenID\Factories\DateIntervalDecoratorFactory;
@@ -25,6 +28,7 @@
2528
use SimpleSAML\OpenID\Jws\Factories\JwsParserFactory;
2629
use SimpleSAML\OpenID\Jws\Factories\JwsVerifierFactory;
2730
use SimpleSAML\OpenID\Jws\Factories\ParsedJwsFactory;
31+
use SimpleSAML\OpenID\Jws\JwsParser;
2832
use SimpleSAML\OpenID\SupportedAlgorithms;
2933
use SimpleSAML\OpenID\SupportedSerializers;
3034

@@ -33,91 +37,63 @@
3337
#[UsesClass(ParsedJwsFactory::class)]
3438
#[UsesClass(SignedJwksFactory::class)]
3539
#[UsesClass(JwksFetcher::class)]
40+
#[UsesClass(CacheDecorator::class)]
41+
#[UsesClass(DateIntervalDecorator::class)]
42+
#[UsesClass(HttpClientDecorator::class)]
43+
#[UsesClass(CacheDecoratorFactory::class)]
44+
#[UsesClass(DateIntervalDecoratorFactory::class)]
45+
#[UsesClass(HttpClientDecoratorFactory::class)]
46+
#[UsesClass(AlgorithmManagerFactory::class)]
47+
#[UsesClass(JwsSerializerManagerFactory::class)]
48+
#[UsesClass(JwsParserFactory::class)]
49+
#[UsesClass(JwsVerifierFactory::class)]
50+
#[UsesClass(JwsParser::class)]
3651
class JwksTest extends TestCase
3752
{
3853
protected MockObject $supportedAlgorithmsMock;
3954
protected MockObject $supportedSerializersMock;
40-
protected MockObject $maxCacheDurationMock;
55+
protected DateInterval $maxCacheDuration;
4156
protected MockObject $cacheMock;
4257
protected MockObject $httpClientMock;
4358
protected MockObject $loggerMock;
44-
protected MockObject $helpersMock;
45-
protected MockObject $algorithmManagerFactoryMock;
46-
protected MockObject $jwsSerializerManagerFactoryMock;
47-
protected MockObject $jwsParserFactoryMock;
48-
protected MockObject $jwsVerifierFactoryMock;
49-
protected MockObject $timestampValidationLeewayMock;
50-
protected MockObject $dateIntervalDecoratorFactoryMock;
51-
protected MockObject $cacheDecoratorFactoryMock;
52-
protected MockObject $httpClientDecoratorFactoryMock;
59+
protected DateInterval $timestampValidationLeeway;
60+
5361
protected function setUp(): void
5462
{
5563
$this->supportedAlgorithmsMock = $this->createMock(SupportedAlgorithms::class);
5664
$this->supportedSerializersMock = $this->createMock(SupportedSerializers::class);
57-
$this->maxCacheDurationMock = $this->createMock(DateInterval::class);
65+
$this->maxCacheDuration = new DateInterval('PT1M');
66+
$this->timestampValidationLeeway = new DateInterval('PT1M');
5867
$this->cacheMock = $this->createMock(CacheInterface::class);
59-
$this->httpClientMock = $this->createMock(Client::class);
6068
$this->loggerMock = $this->createMock(LoggerInterface::class);
61-
$this->helpersMock = $this->createMock(Helpers::class);
62-
$this->algorithmManagerFactoryMock = $this->createMock(AlgorithmManagerFactory::class);
63-
$this->jwsSerializerManagerFactoryMock = $this->createMock(JwsSerializerManagerFactory::class);
64-
$this->jwsParserFactoryMock = $this->createMock(JwsParserFactory::class);
65-
$this->jwsVerifierFactoryMock = $this->createMock(JwsVerifierFactory::class);
66-
$this->timestampValidationLeewayMock = $this->createMock(DateInterval::class);
67-
$this->dateIntervalDecoratorFactoryMock = $this->createMock(DateIntervalDecoratorFactory::class);
68-
$this->cacheDecoratorFactoryMock = $this->createMock(CacheDecoratorFactory::class);
69-
$this->httpClientDecoratorFactoryMock = $this->createMock(HttpClientDecoratorFactory::class);
69+
$this->httpClientMock = $this->createMock(Client::class);
7070
}
7171

7272
protected function sut(
7373
?SupportedAlgorithms $supportedAlgorithms = null,
7474
?SupportedSerializers $supportedSerializers = null,
7575
?DateInterval $maxCacheDuration = null,
76+
?DateInterval $timestampValidationLeeway = null,
7677
?CacheInterface $cache = null,
77-
?Client $httpClient = null,
7878
?LoggerInterface $logger = null,
79-
?Helpers $helpers = null,
80-
?AlgorithmManagerFactory $algorithmManagerFactory = null,
81-
?JwsSerializerManagerFactory $jwsSerializerManagerFactory = null,
82-
?JwsParserFactory $jwsParserFactory = null,
83-
?JwsVerifierFactory $jwsVerifierFactory = null,
84-
?DateInterval $timestampValidationLeeway = null,
85-
?DateIntervalDecoratorFactory $dateIntervalDecoratorFactory = null,
86-
?CacheDecoratorFactory $cacheDecoratorFactory = null,
87-
?HttpClientDecoratorFactory $httpClientDecoratorFactory = null,
79+
?Client $httpClient = null,
8880
): Jwks {
8981
$supportedAlgorithms ??= $this->supportedAlgorithmsMock;
9082
$supportedSerializers ??= $this->supportedSerializersMock;
91-
$maxCacheDuration ??= $this->maxCacheDurationMock;
83+
$maxCacheDuration ??= $this->maxCacheDuration;
84+
$timestampValidationLeeway ??= $this->timestampValidationLeeway;
9285
$cache ??= $this->cacheMock;
93-
$httpClient ??= $this->httpClientMock;
9486
$logger ??= $this->loggerMock;
95-
$helpers ??= $this->helpersMock;
96-
$algorithmManagerFactory ??= $this->algorithmManagerFactoryMock;
97-
$jwsSerializerManagerFactory ??= $this->jwsSerializerManagerFactoryMock;
98-
$jwsParserFactory ??= $this->jwsParserFactoryMock;
99-
$jwsVerifierFactory ??= $this->jwsVerifierFactoryMock;
100-
$timestampValidationLeeway ??= $this->timestampValidationLeewayMock;
101-
$dateIntervalDecoratorFactory ??= $this->dateIntervalDecoratorFactoryMock;
102-
$cacheDecoratorFactory ??= $this->cacheDecoratorFactoryMock;
103-
$httpClientDecoratorFactory ??= $this->httpClientDecoratorFactoryMock;
87+
$httpClient ??= $this->httpClientMock;
10488

10589
return new Jwks(
10690
$supportedAlgorithms,
10791
$supportedSerializers,
10892
$maxCacheDuration,
93+
$timestampValidationLeeway,
10994
$cache,
110-
$httpClient,
11195
$logger,
112-
$helpers,
113-
$algorithmManagerFactory,
114-
$jwsSerializerManagerFactory,
115-
$jwsParserFactory,
116-
$jwsVerifierFactory,
117-
$timestampValidationLeeway,
118-
$dateIntervalDecoratorFactory,
119-
$cacheDecoratorFactory,
120-
$httpClientDecoratorFactory,
96+
$httpClient,
12197
);
12298
}
12399

0 commit comments

Comments
 (0)