Skip to content

Commit 8c52880

Browse files
authored
chore: simplify if-statements; strict checks (#392)
More specific exceptions
1 parent bf93dc0 commit 8c52880

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

src/Encryption.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ public static function padPayload(string $payload, int $maxLengthToPad, string $
3636

3737
if ($contentEncoding === "aesgcm") {
3838
return pack('n*', $padLen).str_pad($payload, $padLen + $payloadLen, chr(0), STR_PAD_LEFT);
39-
} elseif ($contentEncoding === "aes128gcm") {
39+
}
40+
if ($contentEncoding === "aes128gcm") {
4041
return str_pad($payload.chr(2), $padLen + $payloadLen, chr(0), STR_PAD_RIGHT);
41-
} else {
42-
throw new \ErrorException("This content encoding is not supported");
4342
}
43+
44+
throw new \ErrorException("This content encoding is not supported");
4445
}
4546

4647
/**
@@ -63,7 +64,7 @@ public static function encrypt(string $payload, string $userPublicKey, string $u
6364
}
6465

6566
/**
66-
* @throws \ErrorException
67+
* @throws \RuntimeException
6768
*/
6869
public static function deterministicEncrypt(string $payload, string $userPublicKey, string $userAuthToken, string $contentEncoding, array $localKeyObject, string $salt): array
6970
{
@@ -88,7 +89,7 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
8889
]);
8990
}
9091
if (!$localPublicKey) {
91-
throw new \ErrorException('Failed to convert local public key from hexadecimal to binary');
92+
throw new \RuntimeException('Failed to convert local public key from hexadecimal to binary.');
9293
}
9394

9495
// get user public key object
@@ -225,7 +226,9 @@ private static function createInfo(string $type, ?string $context, string $conte
225226
}
226227

227228
return 'Content-Encoding: '.$type.chr(0).'P-256'.$context;
228-
} elseif ($contentEncoding === "aes128gcm") {
229+
}
230+
231+
if ($contentEncoding === "aes128gcm") {
229232
return 'Content-Encoding: '.$type.chr(0);
230233
}
231234

@@ -299,23 +302,22 @@ private static function createLocalKeyObjectUsingOpenSSL(): array
299302
}
300303

301304
/**
302-
* @throws \ErrorException
305+
* @throws \ValueError
303306
*/
304307
private static function getIKM(string $userAuthToken, string $userPublicKey, string $localPublicKey, string $sharedSecret, string $contentEncoding): string
305308
{
306-
if (!empty($userAuthToken)) {
307-
if ($contentEncoding === "aesgcm") {
308-
$info = 'Content-Encoding: auth'.chr(0);
309-
} elseif ($contentEncoding === "aes128gcm") {
310-
$info = "WebPush: info".chr(0).$userPublicKey.$localPublicKey;
311-
} else {
312-
throw new \ErrorException("This content encoding is not supported");
313-
}
314-
315-
return self::hkdf($userAuthToken, $sharedSecret, $info, 32);
309+
if (empty($userAuthToken)) {
310+
return $sharedSecret;
311+
}
312+
if($contentEncoding === "aesgcm") {
313+
$info = 'Content-Encoding: auth'.chr(0);
314+
} elseif($contentEncoding === "aes128gcm") {
315+
$info = "WebPush: info".chr(0).$userPublicKey.$localPublicKey;
316+
} else {
317+
throw new \ValueError("This content encoding is not supported.");
316318
}
317319

318-
return $sharedSecret;
320+
return self::hkdf($userAuthToken, $sharedSecret, $info, 32);
319321
}
320322

321323
private static function calculateAgreementKey(JWK $private_key, JWK $public_key): string
@@ -325,7 +327,7 @@ private static function calculateAgreementKey(JWK $private_key, JWK $public_key)
325327

326328
$result = openssl_pkey_derive($publicPem, $privatePem, 256);
327329
if ($result === false) {
328-
throw new \Exception('Unable to compute the agreement key');
330+
throw new \RuntimeException('Unable to compute the agreement key.');
329331
}
330332
return $result;
331333
}

src/Subscription.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
) {
2828
if($publicKey || $authToken || $contentEncoding) {
2929
$supportedContentEncodings = ['aesgcm', 'aes128gcm'];
30-
if ($contentEncoding && !in_array($contentEncoding, $supportedContentEncodings)) {
30+
if ($contentEncoding && !in_array($contentEncoding, $supportedContentEncodings, true)) {
3131
throw new \ErrorException('This content encoding ('.$contentEncoding.') is not supported.');
3232
}
3333
$this->contentEncoding = $contentEncoding ?: "aesgcm";

src/WebPush.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function sendOneNotification(SubscriptionInterface $subscription, ?string
123123
*
124124
* @param null|int $batchSize Defaults the value defined in defaultOptions during instantiation (which defaults to 1000).
125125
*
126-
* @return \Generator|MessageSentReport[]
126+
* @return \Generator
127127
* @throws \ErrorException
128128
*/
129129
public function flush(?int $batchSize = null): \Generator
@@ -176,7 +176,7 @@ public function flush(?int $batchSize = null): \Generator
176176
}
177177

178178
/**
179-
* @throws \ErrorException
179+
* @throws \ErrorException|\Random\RandomException
180180
*/
181181
protected function prepare(array $notifications): array
182182
{
@@ -272,21 +272,24 @@ public function getAutomaticPadding(): int
272272
/**
273273
* @param bool|int $automaticPadding Max padding length
274274
*
275-
* @throws \Exception
275+
* @throws \ValueError
276276
*/
277277
public function setAutomaticPadding(bool|int $automaticPadding): WebPush
278278
{
279-
if ($automaticPadding > Encryption::MAX_PAYLOAD_LENGTH) {
280-
throw new \Exception('Automatic padding is too large. Max is '.Encryption::MAX_PAYLOAD_LENGTH.'. Recommended max is '.Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH.' for compatibility reasons (see README).');
281-
} elseif ($automaticPadding < 0) {
282-
throw new \Exception('Padding length should be positive or zero.');
283-
} elseif ($automaticPadding === true) {
284-
$this->automaticPadding = Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH;
279+
if ($automaticPadding === true) {
280+
$automaticPadding = Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH;
285281
} elseif ($automaticPadding === false) {
286-
$this->automaticPadding = 0;
287-
} else {
288-
$this->automaticPadding = $automaticPadding;
282+
$automaticPadding = 0;
283+
}
284+
285+
if($automaticPadding > Encryption::MAX_PAYLOAD_LENGTH) {
286+
throw new \ValueError('Automatic padding is too large. Max is '.Encryption::MAX_PAYLOAD_LENGTH.'. Recommended max is '.Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH.' for compatibility reasons (see README).');
289287
}
288+
if($automaticPadding < 0) {
289+
throw new \ValueError('Padding length should be positive or zero.');
290+
}
291+
292+
$this->automaticPadding = $automaticPadding;
290293

291294
return $this;
292295
}

tests/PushServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected function createClosureTest($browserId, $options): callable
119119
$messageIndex = 0;
120120

121121
foreach ($supportedContentEncodings as $contentEncoding) {
122-
if (!in_array($contentEncoding, ['aesgcm', 'aes128gcm'])) {
122+
if (!in_array($contentEncoding, ['aesgcm', 'aes128gcm'], true)) {
123123
$this->expectException(ErrorException::class);
124124
$this->expectExceptionMessage('This content encoding ('.$contentEncoding.') is not supported.');
125125
$this->markTestIncomplete('Unsupported content encoding: '.$contentEncoding);

0 commit comments

Comments
 (0)