8000 Merge branch 'release/1.0.1' · sunaoka/push-notifications-php@e6558e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6558e0

Browse files
committed
Merge branch 'release/1.0.1'
2 parents c79c1df + feb22fd commit e6558e0

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Release Notes
22

3-
## v1.0.0 (2021-10-12)
3+
## [Unreleased](https://github.com/sunaoka/push-notifications-php/compare/1.0.1...develop)
4+
5+
## [v1.0.1 (2021-10-13)](https://github.com/sunaoka/push-notifications-php/compare/1.0.0...1.0.1)
6+
7+
### Changed
8+
9+
- No longer need `file://` prefix for APNs\Token\Option::authKey
10+
11+
## [v1.0.0 (2021-10-12)](https://github.com/sunaoka/push-notifications-php/compare/bbc5601...1.0.0)
412

513
- first release.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Push notifications Library for PHP
22

3-
[![Latest](https://poser.pugx.org/sunaoka/lpush-notifications-php/v)](https://packagist.org/packages/sunaoka/lpush-notifications-php)
3+
[![Latest](https://poser.pugx.org/sunaoka/push-notifications-php/v)](https://packagist.org/packages/sunaoka/push-notifications-php)
44
[![License](https://poser.pugx.org/sunaoka/push-notifications-php/license)](https://packagist.org/packages/sunaoka/push-notifications-php)
55
[![PHP](https://img.shields.io/packagist/php-v/sunaoka/push-notifications-php)](composer.json)
66
[![Test](https://github.com/sunaoka/push-notifications-php/actions/workflows/test.yml/badge.svg)](https://github.com/sunaoka/push-notifications-php/actions/workflows/test.yml)
@@ -71,7 +71,7 @@ There are two ways to specify the option.
7171
```php
7272
$options = new APNs\Token\Option();
7373
$options->payload = $payload;
74-
$options->authKey = 'file:///path/to/key.p8';
74+
$options->authKey = '/path/to/key.p8';
7575
$options->keyId = 'ABCDE12345';
7676
$options->teamId = 'ABCDE12345';
7777
$options->topic = 'com.example.app';
@@ -82,7 +82,7 @@ or
8282
```php
8383
$options = new APNs\Token\Option([
8484
'payload' => $payload,
85-
'authKey' => 'file:///path/to/key.p8',
85+
'authKey' => '/path/to/key.p8',
8686
'keyId' => 'ABCDE12345',
8787
'teamId' => 'ABCDE12345',
8888
'topic' => 'com.example.app',

src/Drivers/APNs/Token.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use GuzzleHttp;
7+
use RuntimeException;
78
use Sunaoka\PushNotifications\Drivers\Driver;
89
use Sunaoka\PushNotifications\Drivers\Feedback;
910
use Sunaoka\PushNotifications\Exceptions\OptionTypeError;
@@ -111,23 +112,36 @@ private function _send($device)
111112
*/
112113
private function bearerToken($authKey, $keyId, $teamId)
113114
{
115+
if (file_exists($authKey)) {
116+
$authKey = "file://{$authKey}";
117+
}
118+
114119
$key = openssl_pkey_get_private($authKey);
120+
if ($key === false) {
121+
throw new RuntimeException(openssl_error_string()); // @codeCoverageIgnore
122+
}
115123

116-
$header = $this->jwtEncode(['alg' => 'ES256', 'kid' => $keyId]);
117-
$claims = $this->jwtEncode(['iss' => $teamId, 'iat' => time()]);
124+
$segments = [];
125+
$segments[] = $this->encodeB64URLSafe(json_encode(['alg' => 'ES256', 'kid' => $keyId]));
126+
$segments[] = $this->encodeB64URLSafe(json_encode(['iss' => $teamId, 'iat' => time()]));
127+
128+
$success = openssl_sign(implode('.', $segments), $signature, $key, 'sha256');
129+
if ($success === false) {
130+
throw new RuntimeException(openssl_error_string()); // @codeCoverageIgnore
131+
}
118132

119-
openssl_sign("{$header}.{$claims}", $signature, $key, 'sha256');
133+
$segments[] = $this->encodeB64URLSafe($signature);
120134

121-
return sprintf('bearer %s.%s.%s', $header, $claims, base64_encode($signature));
135+
return 'bearer ' . implode('.', $segments);
122136
}
123137

124138
/**
125-
* @param array $input
139+
* @param string $input
126140
*
127141
* @return string
128142
*/
129-
private function jwtEncode($input)
143+
private function encodeB64URLSafe($input)
130144
{
131-
return str_replace('=', '', strtr(base64_encode(json_encode($input)), '+/', '-_'));
145+
return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
132146
}
133147
}

tests/Drivers/APNs/TokenTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testAuthKeyIsFile()
9292

9393
$options = new APNs\Token\Option();
9494
$options->payload = $payload;
95-
$options->authKey = "file://{$this->certs('/fake.p8')}";
95+
$options->authKey = $this->certs('/fake.p8');
9696
$options->keyId = 'ABCDE12345';
9797
$options->teamId = 'ABCDE12345';
9898
$options->topic = 'com.example.app';
@@ -122,7 +122,7 @@ public function testSingleFailure()
122122

123123
$options = new APNs\Token\Option();
124124
$options->payload = $payload;
125-
$options->authKey = "file://{$this->certs('/fake.p8')}";
125+
$options->authKey = $this->certs('/fake.p8');
126126
$options->keyId = 'ABCDE12345';
127127
$options->teamId = 'ABCDE12345';
128128
$options->topic = 'com.example.app';
@@ -152,7 +152,7 @@ public function testMultipleFailure()
152152

153153
$options = new APNs\Token\Option();
154154
$options->payload = $payload;
155-
$options->authKey = "file://{$this->certs('/fake.p8')}";
155+
$options->authKey = $this->certs('/fake.p8');
156156
$options->keyId = 'ABCDE12345';
157157
$options->teamId = 'ABCDE12345';
158158
$options->topic = 'com.example.app';
@@ -188,14 +188,14 @@ public function testMakeOption()
188188

189189
$options = new APNs\Token\Option([
190190
'payload' => $payload,
191-
'authKey' => "file://{$this->certs('/fake.p8')}",
191+
'authKey' => $this->certs('/fake.p8'),
192192
'keyId' => 'ABCDE12345',
193193
'teamId' => 'ABCDE12345',
194194
'topic' => 'com.example.app',
195195
]);
196196

197197
self::assertSame($payload, $options->payload);
198-
self::assertSame("file://{$this->certs('/fake.p8')}", $options->authKey);
198+
self::assertSame($this->certs('/fake.p8'), $options->authKey);
199199
self::assertSame('ABCDE12345', $options->keyId);
200200
self::assertSame('ABCDE12345', $options->teamId);
201201
self::assertSame('com.example.app', $options->topic);

tests/Fake/Certs/fake.p8

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
-----BEGIN PRIVATE KEY-----
2-
MIGTAgEAMA0GCSqGSIb3DQEBAQUABH8wfQIBAAIXAM2ikj0CTh0vyIu5/zLk+fPF
3-
4K2g7xECAwEAAQIWXt1zDYY0ty5GHH78Up+IrvvbkC7XKQIMAOcX2sPRaFcqXAPH
4-
AgwA48xLmgs6aQPVRmcCC3IcFFkHCTepYgA3AgwA18HzRWcnHrojsjcCCzx4xaxZ
5-
N+kN9ocw
2+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg+K4GtiSGthyWghna
3+
pMC+R1chj8fvydobNaOl/UBoCGqhRANCAATneGLhyb/UZd0HBBQhSqsMGN4ede8N
4+
eEvlJmgZ8z7NYbQpUZehHwm4M64oW90uzmUgj0r+CSMfyksBK/2CZK6j
65
-----END PRIVATE KEY-----

0 commit comments

Comments
 (0)
0