From d2d8f336d732c42c21572f81475a3a2c4be775ef Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Wed, 17 Apr 2024 13:55:51 +0200 Subject: [PATCH 1/3] Push: Adapt JWT generation to be compatible with more versions of lcobucci/jwt We could create the `Builder` directly, but the (albeit useless) roundtrip through `Configuration` makes this easier to test. Functionally nothing changes, since what's passed to `forSymmetricSigner()` isn't used for anything. Only what's passed to `getToken()` matters. --- ApnsPHP/Push.php | 17 ++++++++++------- ApnsPHP/Tests/PushHttpInitTest.php | 8 ++++---- composer.json | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/ApnsPHP/Push.php b/ApnsPHP/Push.php index ed9045db..ea798420 100644 --- a/ApnsPHP/Push.php +++ b/ApnsPHP/Push.php @@ -13,6 +13,7 @@ use ApnsPHP\Push\Exception; use Psr\Log\LoggerInterface; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter; use Lcobucci\JWT\Signer\Ecdsa\Sha256; use Lcobucci\JWT\Configuration; @@ -475,13 +476,15 @@ protected function httpInit(): bool */ protected function getJsonWebToken(): string { - $key = InMemory::file($this->providerCertFile); - return Configuration::forUnsecuredSigner()->builder() - ->issuedBy($this->providerTeamId) - ->issuedAt(new DateTimeImmutable()) - ->withHeader('kid', $this->providerKeyId) - ->getToken(Sha256::create(), $key) - ->toString(); + $key = InMemory::file($this->providerCertFile); + $signer = new Sha256(new MultibyteStringConverter()); + $tokenBuilder = Configuration::forSymmetricSigner($signer, $key)->builder(); + + return $tokenBuilder->issuedBy($this->providerTeamId) + ->issuedAt(new DateTimeImmutable()) + ->withHeader('kid', $this->providerKeyId) + ->getToken($signer, $key) + ->toString(); } /** diff --git a/ApnsPHP/Tests/PushHttpInitTest.php b/ApnsPHP/Tests/PushHttpInitTest.php index c210ea89..9f87043c 100644 --- a/ApnsPHP/Tests/PushHttpInitTest.php +++ b/ApnsPHP/Tests/PushHttpInitTest.php @@ -68,8 +68,8 @@ public function testHttpInitSucceedsWithKey(): void ->getMock(); $token = new Plain( - new DataSet([ 'headers' ], 'eHeaders'), - new DataSet([ 'claims' ], 'eClaims'), + new DataSet([ 'headers' => 'foo' ], 'eHeaders'), + new DataSet([ 'claims' => 'bar' ], 'eClaims'), new Signature('signature', 'eSignature'), ); @@ -148,8 +148,8 @@ public function testHttpInitThrowsExceptionOnCurlSetoptFail(): void ->getMock(); $token = new Plain( - new DataSet([ 'headers' ], 'eHeaders'), - new DataSet([ 'claims' ], 'eClaims'), + new DataSet([ 'headers' => 'foo' ], 'eHeaders'), + new DataSet([ 'claims' => 'bar' ], 'eClaims'), new Signature('signature', 'eSignature'), ); diff --git a/composer.json b/composer.json index 2920f398..af8a5fff 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "lib-curl": ">=7.33.0", "lib-openssl": "*", "psr/log": ">=1.1", - "lcobucci/jwt": "~4.1.5" + "lcobucci/jwt": "~4.1 || ~5.0" }, "require-dev": { "phpunit/phpunit": ">=9.0 <9.6", From 5d4e384ff167a6274fc71d5a78a02c9f66ea4c5d Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Thu, 18 Apr 2024 08:37:06 +0200 Subject: [PATCH 2/3] Samples: Fix codestyle issues --- sample_push.php | 23 +++++++++++++---------- sample_push_custom.php | 26 +++++++++++++++----------- sample_push_http.php | 23 +++++++++++++---------- sample_push_many.php | 37 ++++++++++++++++++++----------------- sample_push_safari.php | 24 ++++++++++++++---------- 5 files changed, 75 insertions(+), 58 deletions(-) diff --git a/sample_push.php b/sample_push.php index 96475f7e..c3f96aeb 100644 --- a/sample_push.php +++ b/sample_push.php @@ -1,7 +1,10 @@ setSound(); // Set a custom property -$message->setCustomProperty('acme2', array('bang', 'whiz')); +$message->setCustomProperty('acme2', ['bang', 'whiz']); // Set another custom property -$message->setCustomProperty('acme3', array('bing', 'bong')); +$message->setCustomProperty('acme3', ['bing', 'bong']); // Set the expiry value to 30 seconds $message->setExpiry(30); @@ -75,5 +78,5 @@ public function log($level, $message, array $context = array()): void // Examine the error message container $aErrorQueue = $push->getErrors(); if (!empty($aErrorQueue)) { - var_dump($aErrorQueue); + var_dump($aErrorQueue); } diff --git a/sample_push_custom.php b/sample_push_custom.php index 422b9149..1e9b40e7 100644 --- a/sample_push_custom.php +++ b/sample_push_custom.php @@ -1,7 +1,10 @@ setSound(); // Set a custom property -$message->setCustomProperty('acme2', array('bang', 'whiz')); +$message->setCustomProperty('acme2', ['bang', 'whiz']); // Set the expiry value to 30 seconds $message->setExpiry(30); @@ -61,8 +64,9 @@ public function log($level, $message, array $context = array()): void $message->setActionLocKey('Show me!'); // Set the alert-message string and variable string values to appear in place of the format specifiers. -$message->setLocKey('Hello %1$@, you have %2$@ new messages!'); // This will overwrite the text specified with setText() method. -$message->setLocArgs(array('Steve', 5)); +// This will overwrite the text specified with setText() method. +$message->setLocKey('Hello %1$@, you have %2$@ new messages!'); +$message->setLocArgs(['Steve', 5]); // Set the filename of an image file in the application bundle. $message->setLaunchImage('DefaultAlert.png'); @@ -79,5 +83,5 @@ public function log($level, $message, array $context = array()): void // Examine the error message container $aErrorQueue = $push->getErrors(); if (!empty($aErrorQueue)) { - var_dump($aErrorQueue); + var_dump($aErrorQueue); } diff --git a/sample_push_http.php b/sample_push_http.php index a14fed84..0f866f4d 100644 --- a/sample_push_http.php +++ b/sample_push_http.php @@ -1,7 +1,10 @@ setTeamId('sgfdgfdfgd'); @@ -64,10 +67,10 @@ public function log($level, $message, array $context = array()): void $message->setSound(); // Set a custom property -$message->setCustomProperty('acme2', array('bang', 'whiz')); +$message->setCustomProperty('acme2', ['bang', 'whiz']); // Set another custom property -$message->setCustomProperty('acme3', array('bing', 'bong')); +$message->setCustomProperty('acme3', ['bing', 'bong']); // Set the expiry value to 30 seconds $message->setExpiry(30); @@ -84,5 +87,5 @@ public function log($level, $message, array $context = array()): void // Examine the error message container $aErrorQueue = $push->getErrors(); if (!empty($aErrorQueue)) { - var_dump($aErrorQueue); + var_dump($aErrorQueue); } diff --git a/sample_push_many.php b/sample_push_many.php index 48876e0e..653f515b 100644 --- a/sample_push_many.php +++ b/sample_push_many.php @@ -1,7 +1,10 @@ connect(); for ($i = 1; $i <= 10; $i++) { - // Instantiate a new Message with a single recipient - $message = new \ApnsPHP\Message($i == 5 ? INVALID_TOKEN : VALID_TOKEN); + // Instantiate a new Message with a single recipient + $message = new \ApnsPHP\Message($i == 5 ? INVALID_TOKEN : VALID_TOKEN); - // Set a custom identifier. To get back this identifier use the getCustomIdentifier() method - // over a ApnsPHP_Message object retrieved with the getErrors() message. - $message->setCustomIdentifier(sprintf("Message-Badge-%03d", $i)); + // Set a custom identifier. To get back this identifier use the getCustomIdentifier() method + // over a ApnsPHP_Message object retrieved with the getErrors() message. + $message->setCustomIdentifier(sprintf("Message-Badge-%03d", $i)); - // Set badge icon to "3" - $message->setBadge($i); + // Set badge icon to "3" + $message->setBadge($i); - // Add the message to the message queue - $push->add($message); + // Add the message to the message queue + $push->add($message); } // Send all messages in the message queue @@ -68,5 +71,5 @@ public function log($level, $message, array $context = array()): void // Examine the error message container $aErrorQueue = $push->getErrors(); if (!empty($aErrorQueue)) { - var_dump($aErrorQueue); + var_dump($aErrorQueue); } diff --git a/sample_push_safari.php b/sample_push_safari.php index 0e41bcbc..fad42a75 100644 --- a/sample_push_safari.php +++ b/sample_push_safari.php @@ -1,7 +1,10 @@ setAction('View'); -// Set an array of values that are paired with the placeholders inside the urlFormatString value of your website.json file -$message->setUrlArgs(array('boarding', 'A998')); +// Set an array of values that are paired with the placeholders inside the urlFormatString value +// of your website.json file +$message->setUrlArgs(['boarding', 'A998']); // Add the message to the message queue $push->add($message); @@ -66,5 +70,5 @@ public function log($level, $message, array $context = array()): void // Examine the error message container $aErrorQueue = $push->getErrors(); if (!empty($aErrorQueue)) { - var_dump($aErrorQueue); + var_dump($aErrorQueue); } From 64a7616c9a67211074cb20128a13ce9c92cd9931 Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Thu, 25 Apr 2024 15:03:38 +0200 Subject: [PATCH 3/3] CI: Enforce passing phpcs and phpstan --- .github/workflows/php-tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php-tests.yml b/.github/workflows/php-tests.yml index a55b74fa..1f9591a2 100644 --- a/.github/workflows/php-tests.yml +++ b/.github/workflows/php-tests.yml @@ -89,7 +89,7 @@ jobs: phpcs: runs-on: ubuntu-latest - continue-on-error: true + continue-on-error: false name: "PHPCS" steps: - name: Checkout @@ -107,7 +107,7 @@ jobs: phpstan: runs-on: ubuntu-latest - continue-on-error: true + continue-on-error: false name: "PHPStan" steps: - name: Checkout