-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityStatementFetcher.php
More file actions
168 lines (140 loc) · 5.73 KB
/
EntityStatementFetcher.php
File metadata and controls
168 lines (140 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
namespace SimpleSAML\OpenID\Federation;
use Psr\Log\LoggerInterface;
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
use SimpleSAML\OpenID\Codebooks\ContentTypesEnum;
use SimpleSAML\OpenID\Codebooks\HttpMethodsEnum;
use SimpleSAML\OpenID\Codebooks\WellKnownEnum;
use SimpleSAML\OpenID\Decorators\DateIntervalDecorator;
use SimpleSAML\OpenID\Exceptions\EntityStatementException;
use SimpleSAML\OpenID\Exceptions\FetchException;
use SimpleSAML\OpenID\Federation\Factories\EntityStatementFactory;
use SimpleSAML\OpenID\Helpers;
use SimpleSAML\OpenID\Jws\JwsFetcher;
use SimpleSAML\OpenID\Utils\ArtifactFetcher;
class EntityStatementFetcher extends JwsFetcher
{
public function __construct(
private readonly EntityStatementFactory $parsedJwsFactory,
ArtifactFetcher $artifactFetcher,
DateIntervalDecorator $maxCacheDuration,
Helpers $helpers,
?LoggerInterface $logger = null,
) {
parent::__construct($parsedJwsFactory, $artifactFetcher, $maxCacheDuration, $helpers, $logger);
}
protected function buildJwsInstance(string $token): EntityStatement
{
return $this->parsedJwsFactory->fromToken($token);
}
public function getExpectedContentTypeHttpHeader(): string
{
return ContentTypesEnum::ApplicationEntityStatementJwt->value;
}
/**
* Fetch entity statement from a well-known URI. By default, this will be openid-federation (entity configuration).
* Fetch will first check if the entity statement is available in cache. If not, it will do a network fetch.
*
* @param non-empty-string $entityId
* @throws \SimpleSAML\OpenID\Exceptions\FetchException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*/
public function fromCacheOrWellKnownEndpoint(
string $entityId,
WellKnownEnum $wellKnownEnum = WellKnownEnum::OpenIdFederation,
): EntityStatement {
$wellKnownUri = $wellKnownEnum->uriFor($entityId);
$this->logger?->debug(
'Entity statement fetch from cache or well-known endpoint.',
['entityId' => $entityId, 'wellKnownUri' => $wellKnownUri, 'wellKnownEnum' => $wellKnownEnum],
);
return $this->fromCacheOrNetwork($wellKnownUri);
}
/**
* @param \SimpleSAML\OpenID\Federation\EntityStatement $entityConfiguration Entity from which to use the fetch
* endpoint (issuer).
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\FetchException
*/
public function fromCacheOrFetchEndpoint(
string $subjectId,
EntityStatement $entityConfiguration,
): EntityStatement {
$fetchEndpointUri = $entityConfiguration->getFederationFetchEndpoint() ??
throw new EntityStatementException('No fetch endpoint found in entity configuration.');
$this->logger?->debug(
'Entity statement fetch from cache or fetch endpoint.',
['subjectId' => $subjectId, 'fetchEndpointUri' => $fetchEndpointUri],
);
return $this->fromCacheOrNetwork(
$this->helpers->url()->withParams(
$fetchEndpointUri,
[
ClaimsEnum::Sub->value => $subjectId,
],
),
);
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\FetchException
*/
public function fromCacheOrNetwork(string $uri): EntityStatement
{
return $this->fromCache($uri) ?? $this->fromNetwork($uri);
}
/**
* Fetch entity statement from cache, if available. URI is used as cache key.
*
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\FetchException
*/
public function fromCache(string $uri): ?EntityStatement
{
$entityStatement = parent::fromCache($uri);
if (is_null($entityStatement)) {
return null;
}
if ($entityStatement instanceof \SimpleSAML\OpenID\Federation\EntityStatement) {
return $entityStatement;
}
// @codeCoverageIgnoreStart
$message = 'Unexpected entity statement instance encountered for cache fetch.';
$this->logger?->error(
$message,
['uri' => $uri, 'entityStatement' => $entityStatement],
);
throw new FetchException($message);
// @codeCoverageIgnoreEnd
}
/**
* Fetch entity statement from network.
*
* @param array<string, mixed> $options See https://docs.guzzlephp.org/en/stable/request-options.html
* @param bool $shouldCache If true, each successful fetch will be cached, with URI being used as a cache key.
* @param string ...$additionalCacheKeyElements Additional string elements to be used as cache key.
* @throws \SimpleSAML\OpenID\Exceptions\FetchException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*/
public function fromNetwork(
string $uri,
HttpMethodsEnum $httpMethodsEnum = HttpMethodsEnum::GET,
array $options = [],
bool $shouldCache = true,
string ...$additionalCacheKeyElements,
): EntityStatement {
$entityStatement = parent::fromNetwork($uri, $httpMethodsEnum, $options, $shouldCache);
if ($entityStatement instanceof \SimpleSAML\OpenID\Federation\EntityStatement) {
return $entityStatement;
}
// @codeCoverageIgnoreStart
$message = 'Unexpected entity statement instance encountered for network fetch.';
$this->logger?->error(
$message,
['uri' => $uri, 'entityStatement' => $entityStatement],
);
throw new FetchException($message);
// @codeCoverageIgnoreEnd
}
}