Skip to content

Commit 2a322ef

Browse files
committed
WIP
1 parent bcb0c46 commit 2a322ef

32 files changed

Lines changed: 226 additions & 289 deletions

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@
2626
"laminas/laminas-diactoros": "^3",
2727
"laminas/laminas-httphandlerrunner": "^2",
2828
"lcobucci/jwt": "^5.3",
29-
"league/oauth2-server": "^8.5.3",
29+
"league/oauth2-server": "^9.4",
3030
"nette/forms": "^3",
3131
"psr/container": "^2.0",
3232
"psr/log": "^3",
33+
"psr/simple-cache": "^3",
3334
"simplesamlphp/composer-module-installer": "^1.3",
3435
"simplesamlphp/openid": "~0.3.8",
3536
"spomky-labs/base64url": "^2.0",
37+
"symfony/cache": "^7.4",
3638
"symfony/expression-language": "^7.4",
3739
"symfony/psr-http-message-bridge": "^7.4",
38-
"web-token/jwt-framework": "^3.4.10",
39-
"symfony/cache": "^7.4",
40-
"psr/simple-cache": "^3"
40+
"web-token/jwt-framework": "^3.4.10"
4141
},
4242
"require-dev": {
4343
"friendsofphp/php-cs-fixer": "^3",

src/Entities/AccessTokenEntity.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,22 @@ public function __construct(
7575
protected readonly ?string $boundRedirectUri = null,
7676
protected readonly ?string $issuerState = null,
7777
) {
78+
if ($id === '') {
79+
throw new \InvalidArgumentException('Access token identifier cannot be empty.');
80+
}
81+
7882
$this->setIdentifier($id);
7983
$this->setClient($clientEntity);
8084
foreach ($scopes as $scope) {
8185
$this->addScope($scope);
8286
}
8387
$this->setExpiryDateTime($expiryDateTime);
84-
$this->setUserIdentifier($userIdentifier);
88+
if (!is_null($userIdentifier)) {
89+
$userIdentifier = (string)$userIdentifier;
90+
}
91+
if (!empty($userIdentifier)) {
92+
$this->setUserIdentifier($userIdentifier);
93+
}
8594
$this->setAuthCodeId($authCodeId);
8695
$this->setRequestedClaims($requestedClaims ?? []);
8796
if ($isRevoked) {
@@ -134,16 +143,16 @@ public function getState(): array
134143
*/
135144
public function __toString(): string
136145
{
137-
return $this->stringRepresentation = $this->convertToJWT()->getToken();
146+
return $this->toString();
138147
}
139148

140149
/**
141150
* Get string representation of access token at the moment of casting it to string.
142-
* @return string|null String representation or null if it was not cast to string yet.
151+
* @return string String representation of the access token.
143152
*/
144-
public function toString(): ?string
153+
public function toString(): string
145154
{
146-
return $this->stringRepresentation;
155+
return $this->stringRepresentation ??= $this->convertToJWT()->getToken();
147156
}
148157

149158
/**
@@ -162,7 +171,7 @@ protected function convertToJWT(): ParsedJws
162171
$payload = array_filter([
163172
ClaimsEnum::Iss->value => $this->moduleConfig->getIssuer(),
164173
ClaimsEnum::Iat->value => $currentTimestamp,
165-
ClaimsEnum::Jti->value => (string)$this->getIdentifier(),
174+
ClaimsEnum::Jti->value => $this->getIdentifier(),
166175
ClaimsEnum::Aud->value => $this->getClient()->getIdentifier(),
167176
ClaimsEnum::Nbf->value => $currentTimestamp,
168177
ClaimsEnum::Exp->value => $this->expiryDateTime->getTimestamp(),

src/Entities/AuthCodeEntity.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ public function __construct(
5151
protected readonly ?string $boundRedirectUri = null,
5252
protected readonly ?string $issuerState = null,
5353
) {
54+
if ($id === '') {
55+
throw new \InvalidArgumentException('Authorization code identifier cannot be empty.');
56+
}
57+
5458
$this->identifier = $id;
5559
$this->client = $client;
5660
$this->scopes = $scopes;
5761
$this->expiryDateTime = $expiryDateTime;
58-
$this->userIdentifier = $userIdentifier;
62+
$this->userIdentifier = $userIdentifier === '' ? null : $userIdentifier;
5963
$this->redirectUri = $redirectUri;
6064
$this->nonce = $nonce;
6165
$this->isRevoked = $isRevoked;

src/Entities/ClientEntity.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public function __construct(
162162
?array $extraMetadata = null,
163163
?string $registrationAccessToken = null,
164164
) {
165+
if ($identifier === '') {
166+
throw new \InvalidArgumentException('Client identifier cannot be empty.');
167+
}
168+
165169
$this->identifier = $identifier;
166170
$this->secret = $secret;
167171
$this->name = $name;

src/Entities/Interfaces/EntityStringRepresentationInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ interface EntityStringRepresentationInterface
99
/**
1010
* Generate string representation of entity.
1111
*/
12-
public function toString(): ?string;
12+
public function toString(): string;
1313
}

src/Entities/RefreshTokenEntity.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public function __construct(
3838
?string $authCodeId = null,
3939
bool $isRevoked = false,
4040
) {
41+
if ($id === '') {
42+
throw new \InvalidArgumentException('Refresh token identifier cannot be empty.');
43+
}
44+
4145
$this->setIdentifier($id);
4246
$this->setExpiryDateTime($expiryDateTime);
4347
$this->setAccessToken($accessTokenEntity);

src/Entities/ScopeEntity.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function __construct(
3535
protected ?string $icon = null,
3636
protected array $claims = [],
3737
) {
38+
if ($identifier === '') {
39+
throw new \InvalidArgumentException('Scope identifier cannot be empty.');
40+
}
41+
3842
$this->identifier = $identifier;
3943
}
4044

@@ -58,6 +62,6 @@ public function getClaims(): array
5862

5963
public function jsonSerialize(): string
6064
{
61-
return (string) $this->getIdentifier();
65+
return $this->getIdentifier();
6266
}
6367
}

src/Entities/UserEntity.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@
2626
*/
2727
class UserEntity implements UserEntityInterface, MementoInterface, ClaimSetInterface
2828
{
29+
/** @var non-empty-string */
30+
private readonly string $identifier;
31+
2932
public function __construct(
30-
private readonly string $identifier,
33+
string $identifier,
3134
private readonly DateTimeImmutable $createdAt,
3235
private DateTimeImmutable $updatedAt,
3336
private array $claims = [],
3437
) {
38+
if ($identifier === '') {
39+
throw new \InvalidArgumentException('User identifier cannot be empty.');
40+
}
41+
42+
$this->identifier = $identifier;
3543
}
3644

3745
/**

src/Factories/Entities/ClientEntityFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ public function fromRegistrationData(
168168
$isDcrUpdate = $existingClient !== null && $registrationType === RegistrationTypeEnum::Dynamic;
169169
$metadataFallbackClient = $isDcrUpdate ? null : $existingClient;
170170

171-
$id = $clientIdentifier ?? $existingClient?->getIdentifier() ??
172-
$this->sspBridge->utils()->random()->generateID();
171+
$id = $clientIdentifier ?: $existingClient?->getIdentifier();
172+
if (empty($id)) {
173+
$id = $this->sspBridge->utils()->random()->generateID();
174+
}
173175

174176
$secret = $existingClient?->getSecret() ?? $this->sspBridge->utils()->random()->generateID();
175177

src/Repositories/AccessTokenRepository.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,12 @@ public function getTableName(): string
6161
public function getNewToken(
6262
OAuth2ClientEntityInterface $clientEntity,
6363
array $scopes,
64-
$userIdentifier = null,
64+
?string $userIdentifier = null,
6565
?string $authCodeId = null,
6666
?array $requestedClaims = null,
6767
?string $id = null,
6868
?DateTimeImmutable $expiryDateTime = null,
6969
): AccessTokenEntityInterface {
70-
if (!is_null($userIdentifier)) {
71-
$userIdentifier = (string)$userIdentifier;
72-
}
7370
if (empty($userIdentifier)) {
7471
$userIdentifier = null;
7572
}
@@ -145,7 +142,7 @@ public function persistNewAccessToken(OAuth2AccessTokenEntityInterface $accessTo
145142
$this->helpers->dateTime()->getSecondsToExpirationTime(
146143
$accessTokenEntity->getExpiryDateTime()->getTimestamp(),
147144
),
148-
$this->getCacheKey((string)$accessTokenEntity->getIdentifier()),
145+
$this->getCacheKey($accessTokenEntity->getIdentifier()),
149146
);
150147
}
151148

@@ -184,7 +181,7 @@ public function findById(string $tokenId): ?AccessTokenEntity
184181
$this->helpers->dateTime()->getSecondsToExpirationTime(
185182
$accessTokenEntity->getExpiryDateTime()->getTimestamp(),
186183
),
187-
$this->getCacheKey((string)$accessTokenEntity->getIdentifier()),
184+
$this->getCacheKey($accessTokenEntity->getIdentifier()),
188185
);
189186

190187
return $accessTokenEntity;
@@ -195,7 +192,7 @@ public function findById(string $tokenId): ?AccessTokenEntity
195192
* @throws \JsonException
196193
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
197194
*/
198-
public function revokeAccessToken($tokenId): void
195+
public function revokeAccessToken(string $tokenId): void
199196
{
200197
$accessToken = $this->findById($tokenId);
201198

@@ -227,7 +224,7 @@ public function revokeByAuthCodeId(string $authCodeId): void
227224
* {@inheritdoc}
228225
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
229226
*/
230-
public function isAccessTokenRevoked($tokenId): bool
227+
public function isAccessTokenRevoked(string $tokenId): bool
231228
{
232229
$accessToken = $this->findById($tokenId);
233230

@@ -286,7 +283,7 @@ private function update(AccessTokenEntity $accessTokenEntity): void
286283
$this->helpers->dateTime()->getSecondsToExpirationTime(
287284
$accessTokenEntity->getExpiryDateTime()->getTimestamp(),
288285
),
289-
$this->getCacheKey((string)$accessTokenEntity->getIdentifier()),
286+
$this->getCacheKey($accessTokenEntity->getIdentifier()),
290287
);
291288
}
292289

0 commit comments

Comments
 (0)