Skip to content

Commit

Permalink
Upgrade to Webtokens 2.0 (#88)
Browse files Browse the repository at this point in the history
* Upgrade to webtokens2-library
* Upgrade phpunit
* Fix tests and syntax
  • Loading branch information
rasmusbe authored and edamov committed Nov 18, 2019
1 parent a84a754 commit a58ae87
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
composer.lock
docs
vendor
.phpunit.result.cache
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"ext-json": "*",
"ext-xml": "*",
"ext-intl": "*",
"web-token/jwt-signature-algorithm-ecdsa": "^1.3",
"web-token/jwt-key-mgmt": "^1.3"
"web-token/jwt-signature-algorithm-ecdsa": "^2.0",
"web-token/jwt-key-mgmt": "^2.0"
},
"require-dev": {
"ext-xdebug": "*",
"phpunit/phpunit" : "~7.5",
"squizlabs/php_codesniffer": "^3.4",
"php-coveralls/php-coveralls": "^2.0"
"php-coveralls/php-coveralls": "^2.0",
"phpunit/phpunit": "^8.4",
"squizlabs/php_codesniffer": "^3.4"
},
"autoload": {
"psr-4": {
Expand All @@ -51,14 +51,17 @@
"dev-master": "0.1.x-dev"
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true,
"scripts": {
"test": "phpunit",
"check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests",
"fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests"
},
"config": {
"sort-packages": true
"sort-packages": true,
"platform": {
"php": "7.2"
}
}
}
2 changes: 1 addition & 1 deletion src/AuthProvider/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ public function authenticateClient(Request $request)
"apns-topic" => $this->appBundleId
]);
}
}
}
10 changes: 4 additions & 6 deletions src/AuthProvider/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Jose\Component\Core\JWK;
use Jose\Component\KeyManagement\JWKFactory;
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\Converter\StandardConverter;
use Jose\Component\Signature\JWSBuilder;
use Jose\Component\Signature\Algorithm\ES512;
use Jose\Component\Signature\Serializer\CompactSerializer;
Expand Down Expand Up @@ -208,13 +207,12 @@ private function getProtectedHeader(JWK $privateECKey): array
*/
private function generate(): string
{
$jsonConverter = new StandardConverter();
$algorithmManager = AlgorithmManager::create([
$algorithmManager = new AlgorithmManager([
new ES512(),
]);

$jwsBuilder = new JWSBuilder($jsonConverter, $algorithmManager);
$payload = $jsonConverter->encode($this->getClaimsPayload());
$jwsBuilder = new JWSBuilder($algorithmManager);
$payload = json_encode($this->getClaimsPayload(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

$privateECKey = $this->generatePrivateECKey();

Expand All @@ -224,7 +222,7 @@ private function generate(): string
->addSignature($privateECKey, $this->getProtectedHeader($privateECKey))
->build();

$serializer = new CompactSerializer($jsonConverter);
$serializer = new CompactSerializer();
$this->token = $serializer->serialize($jws);

return $this->token;
Expand Down
86 changes: 53 additions & 33 deletions src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@

namespace Pushok;

use Countable;
use Pushok\Payload\Alert;

// Polyfill for PHP 7.2
if (!function_exists('is_countable')) {
function is_countable($var)
{
return (is_array($var) || $var instanceof Countable);
}
}

/**
* Class Payload
*
* @package Pushok
*
* @see http://bit.ly/payload-key-reference
* @see http://bit.ly/payload-key-reference
*/
class Payload implements \JsonSerializable
{
Expand Down Expand Up @@ -103,10 +113,21 @@ public static function create(): Payload
return new self();
}

/**
* Get Alert.
*
* @return Alert|null
*/
public function getAlert()
{
return $this->alert;
}

/**
* Set Alert.
*
* @param Alert|string $alert
*
* @return Payload
*/
public function setAlert($alert): Payload
Expand All @@ -119,19 +140,20 @@ public function setAlert($alert): Payload
}

/**
* Get Alert.
* Get badge.
*
* @return Alert|null
* @return int|null
*/
public function getAlert()
public function getBadge()
{
return $this->alert;
return $this->badge;
}

/**
* Set badge.
*
* @param int $value
*
* @return Payload
*/
public function setBadge(int $value): Payload
Expand All @@ -142,19 +164,20 @@ public function setBadge(int $value): Payload
}

/**
* Get badge.
* Get sound.
*
* @return int|null
* @return string|null
*/
public function getBadge()
public function getSound()
{
return $this->badge;
return $this->sound;
}

/**
* Set sound.
*
* @param string $value
*
* @return Payload
*/
public function setSound(string $value): Payload
Expand All @@ -164,20 +187,11 @@ public function setSound(string $value): Payload
return $this;
}

/**
* Get sound.
*
* @return string|null
*/
public function getSound()
{
return $this->sound;
}

/**
* Set content availability.
*
* @param bool $value
*
* @return Payload
*/
public function setContentAvailability(bool $value): Payload
Expand All @@ -199,9 +213,11 @@ public function isContentAvailable()

/**
* Set the mutable-content key for Notification Service Extensions on iOS10.
*
* @see http://bit.ly/mutable-content
*
* @param bool $value
*
* @return Payload
*/
public function setMutableContent(bool $value): Payload
Expand All @@ -221,10 +237,21 @@ public function hasMutableContent()
return $this->mutableContent;
}

/**
* Get category.
*
* @return string|null
*/
public function getCategory()
{
return $this->category;
}

/**
* Set category.
*
* @param string $value
*
* @return Payload
*/
public function setCategory(string $value): Payload
Expand All @@ -235,19 +262,20 @@ public function setCategory(string $value): Payload
}

/**
* Get category.
* Get thread-id.
*
* @return string|null
*/
public function getCategory()
public function getThreadId()
{
return $this->category;
return $this->threadId;
}

/**
* Set thread-id.
*
* @param string $value
*
* @return Payload
*/
public function setThreadId(string $value): Payload
Expand All @@ -257,21 +285,12 @@ public function setThreadId(string $value): Payload
return $this;
}

/**
* Get thread-id.
*
* @return string|null
*/
public function getThreadId()
{
return $this->threadId;
}

/**
* Set custom value for Payload.
*
* @param string $key
* @param mixed $value
*
* @return Payload
* @throws InvalidPayloadException
*/
Expand All @@ -290,6 +309,7 @@ public function setCustomValue(string $key, $value): Payload
* Get custom value.
*
* @param $key
*
* @return mixed
* @throws InvalidPayloadException
*/
Expand Down Expand Up @@ -350,7 +370,7 @@ public function jsonSerialize()
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_THREAD_ID_KEY} = $this->threadId;
}

if ((is_array($this->customValues) || $this->customValues instanceof Countable) && count($this->customValues)) {
if (is_countable($this->customValues) && count($this->customValues)) {
$payload = array_merge($payload, $this->customValues);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private function prepareApnsHeaders(Notification $notification)

if (is_int($notification->getPriority())) {
$this->headers[self::HEADER_APNS_PRIORITY] = $notification->getPriority();
} else if ($notification->getPayload()->isContentAvailable()) {
} elseif ($notification->getPayload()->isContentAvailable()) {
$this->headers[self::HEADER_APNS_PRIORITY] = Notification::PRIORITY_LOW;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private static function fetchErrorReason(string $body): string
*/
private static function fetch410Timestamp(int $statusCode, string $body): string
{
if($statusCode === 410) {
if ($statusCode === 410) {
return (string)(json_decode($body, true)['timestamp'] ?: '');
}
return '';
Expand Down
28 changes: 14 additions & 14 deletions tests/AuthProvider/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@ public function testCreatingTokenAuthProvider()
$this->assertTrue(is_string($authProvider->get()));
}

private function getOptions()
{
return [
'key_id' => '1234567890',
'team_id' => '1234567890',
'app_bundle_id' => 'com.app.Test',
];
}

public function testCreatingTokenAuthProviderWithKeyContent()
{
$options = $this->getOptions();
$options['private_key_content'] = <<<EOT
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg13n3isfsEktzl+CtH5ECpRrKk+40prVuCbldkP77gamgCgYIKoZIzj0DAQehRANCAARhwgxSRqXBt54BWRQXoU/doFWULOWrER3uLS43/iugDW1PMDliQZEzWetYAdf+Mafq/PrlbEAfA+l7JfmijAsv
-----END PRIVATE KEY-----
EOT;

$options['private_key_content'] = file_get_contents(
implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'files', 'private_key.p8'])
);

$authProvider = AuthProvider\Token::create($options);

$this->assertInstanceOf(AuthProviderInterface::class, $authProvider);
Expand All @@ -60,13 +69,4 @@ public function testUseExistingToken()
$this->assertInstanceOf(AuthProviderInterface::class, $authProvider);
$this->assertEquals($token, $authProvider->get());
}

private function getOptions()
{
return [
'key_id' => '1234567890',
'team_id' => '1234567890',
'app_bundle_id' => 'com.app.Test',
];
}
}
4 changes: 2 additions & 2 deletions tests/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public function testConvertToJSon()
' "thread-id": "tread-id", "mutable-content": 1, "content-available": 1}, "key": "value"}',
$payload->toJson()
);

}

public function testSetCustomArrayType() {
public function testSetCustomArrayType()
{
$alert = Alert::create()->setTitle('title');
$payload = Payload::create()
->setAlert($alert)
Expand Down
5 changes: 4 additions & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public function testGetErrorDescription()
{
$response = new Response(400, 'headers', '{"reason": "BadCollapseId"}');

$this->assertEquals('The collapse identifier exceeds the maximum allowed size', $response->getErrorDescription());
$this->assertEquals(
'The collapse identifier exceeds the maximum allowed size',
$response->getErrorDescription()
);
}

public function testGetError410Timestamp()
Expand Down

0 comments on commit a58ae87

Please sign in to comment.