Skip to content

Commit 5e8e190

Browse files
authored
Merge pull request #38 from Staffbase/code-cleanup-pt2
refactor: Code cleanup Pt2
2 parents 8e10f11 + 35250f0 commit 5e8e190

17 files changed

Lines changed: 203 additions & 167 deletions

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@ phpcs.xml
4040
/vendor/*
4141
/wpcs/*
4242

43-
!/.gitignore

.php-cs-fixer.dist.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
return (new PhpCsFixer\Config())
88
->setRules([
9-
'@PHP8x4Migration' => true,
109
'@PER-CS' => true,
1110
'array_syntax' => ['syntax' => 'short'],
1211
'declare_strict_types' => true,

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 5
2+
level: 7
33
paths:
44
- ./src
55
- ./test

src/AbstractToken.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ abstract class AbstractToken
2525

2626
private Configuration $config;
2727

28+
/**
29+
* @var Constraint[]
30+
*/
2831
private array $constraints;
2932

3033
/**
@@ -92,6 +95,9 @@ protected function validateToken(): void
9295
*/
9396
protected function hasClaim(string $claim): bool
9497
{
98+
if (empty($claim)) {
99+
return false;
100+
}
95101
return $this->token->claims()->has($claim);
96102
}
97103

@@ -102,13 +108,25 @@ protected function hasClaim(string $claim): bool
102108
*
103109
* @return mixed
104110
*/
105-
protected function getClaim(string $claim)
111+
/**
112+
* Get a claim without checking for existence.
113+
*
114+
* @param string $claim name.
115+
*
116+
* @return mixed
117+
*/
118+
protected function getClaim(string $claim): mixed
106119
{
120+
if (empty($claim)) {
121+
return null;
122+
}
107123
return $this->token->claims()->get($claim);
108124
}
109125

110126
/**
111127
* Get an array of all available claims and their values.
128+
*
129+
* @return array<string, mixed>
112130
*/
113131
protected function getAllClaims(): array
114132
{
@@ -125,12 +143,19 @@ protected function getAllClaims(): array
125143
*/
126144
public static function base64ToPEMPublicKey(string $data): string
127145
{
146+
if (empty($data)) {
147+
throw new SSOException('Empty base64 data provided for PEM conversion.');
148+
}
128149

129150
$data = strtr($data, [
130151
"\r" => "",
131152
"\n" => "",
132153
]);
133154

155+
if (empty($data)) {
156+
throw new SSOException('Base64 data is empty after cleanup.');
157+
}
158+
134159
return
135160
"-----BEGIN PUBLIC KEY-----\n"
136161
. chunk_split($data, 64)
@@ -179,12 +204,26 @@ public function getSignerKey(): Key
179204
*/
180205
private function getKey(string $appSecret): Key
181206
{
207+
// Ensure the app secret is not empty to satisfy strict non-empty-string requirements
208+
if (!trim($appSecret)) {
209+
throw new SSOException('Empty appSecret provided when creating signer key.');
210+
}
211+
182212
if (strpos($appSecret, '-----') === 0) {
213+
if (empty($appSecret)) {
214+
throw new SSOException('Empty PEM key provided.');
215+
}
183216
$key = InMemory::plainText($appSecret);
184217
} elseif (strpos($appSecret, 'file://') === 0) {
218+
if (empty($appSecret)) {
219+
throw new SSOException('Empty file path provided.');
220+
}
185221
$key = InMemory::file($appSecret);
186222
} else {
187-
$key = InMemory::plainText(self::base64ToPEMPublicKey($appSecret));
223+
$pem = self::base64ToPEMPublicKey($appSecret);
224+
// After our validation in base64ToPEMPublicKey, we know $pem is non-empty
225+
/** @var non-empty-string $pem */
226+
$key = InMemory::plainText($pem);
188227
}
189228
return $key;
190229
}

src/RemoteCall/AbstractRemoteCallHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract class AbstractRemoteCallHandler implements RemoteCallInterface
3030
*
3131
* This will tell Staffbase that everything went OK.
3232
*/
33-
public function exitSuccess()
33+
public function exitSuccess(): void
3434
{
3535
header("HTTP/1.1 200 OK");
3636
exit;
@@ -41,7 +41,7 @@ public function exitSuccess()
4141
*
4242
* This will tell Staffbase that it should try again later.
4343
*/
44-
public function exitFailure()
44+
public function exitFailure(): void
4545
{
4646
header('HTTP/1.1 500 Internal Server Error');
4747
exit;

src/RemoteCall/RemoteCallInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ interface RemoteCallInterface
2929
*
3030
* This will tell Staffbase that everything went OK.
3131
*/
32-
public function exitSuccess();
32+
public function exitSuccess(): void;
3333

3434
/**
3535
* Stop the execution by providing a non 2XX HTTP response
3636
*
3737
* This will tell Staffbase that it should try again later.
3838
*/
39-
public function exitFailure();
39+
public function exitFailure(): void;
4040
}

src/SSOData/ClaimAccessTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract protected function getClaim(string $claim);
4040
/**
4141
* Get an array of all available claims and their values.
4242
*
43-
* @return array
43+
* @return array<string, mixed>
4444
*/
4545
abstract protected function getAllClaims(): array;
4646

src/SSOData/SSODataTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function getLocale(): string
198198
/**
199199
* Get the user tags.
200200
*
201-
* @return array|null
201+
* @return array<mixed>|null
202202
*/
203203
public function getTags(): ?array
204204
{

src/SSOData/SharedDataTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ trait SharedDataTrait
3636
*/
3737
public function getAudience(): ?string
3838
{
39-
/** @var array|string|null $audience */
39+
/** @var array<string>|string|null $audience */
4040
$audience = $this->getClaimSafe(SharedClaimsInterface::CLAIM_AUDIENCE);
4141

4242
if (is_array($audience)) {
@@ -127,7 +127,7 @@ public function getRole(): ?string
127127
/**
128128
* Get all stored data.
129129
*
130-
* @return array
130+
* @return array<string,mixed>
131131
*/
132132
public function getData(): array
133133
{

src/SSOToken.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class SSOToken extends AbstractToken implements SharedClaimsInterface, SSODataCl
4545
*/
4646
public function __construct(string $appSecret, string $tokenData, ?int $leeway = 0)
4747
{
48+
if (empty($tokenData)) {
49+
throw new SSOException('Parameter tokenData for SSOToken is empty.');
50+
}
51+
4852
$constrains = [
4953
new StrictValidAt(SystemClock::fromUTC(), $this->getLeewayInterval((int) $leeway)),
5054
new HasInstanceId(),

0 commit comments

Comments
 (0)