|
4 | 4 |
|
5 | 5 | use Exception;
|
6 | 6 | use GuzzleHttp;
|
| 7 | +use RuntimeException; |
7 | 8 | use Sunaoka\PushNotifications\Drivers\Driver;
|
8 | 9 | use Sunaoka\PushNotifications\Drivers\Feedback;
|
9 | 10 | use Sunaoka\PushNotifications\Exceptions\OptionTypeError;
|
@@ -112,22 +113,31 @@ private function _send($device)
|
112 | 113 | private function bearerToken($authKey, $keyId, $teamId)
|
113 | 114 | {
|
114 | 115 | $key = openssl_pkey_get_private($authKey);
|
| 116 | + if ($key === false) { |
| 117 | + throw new RuntimeException(openssl_error_string()); // @codeCoverageIgnore |
| 118 | + } |
| 119 | + |
| 120 | + $segments = []; |
| 121 | + $segments[] = $this->encodeB64URLSafe(json_encode(['alg' => 'ES256', 'kid' => $keyId])); |
| 122 | + $segments[] = $this->encodeB64URLSafe(json_encode(['iss' => $teamId, 'iat' => time()])); |
115 | 123 |
|
116 |
| - $header = $this->jwtEncode(['alg' => 'ES256', 'kid' => $keyId]); |
117 |
| - $claims = $this->jwtEncode(['iss' => $teamId, 'iat' => time()]); |
| 124 | + $success = openssl_sign(implode('.', $segments), $signature, $key, 'sha256'); |
| 125 | + if ($success === false) { |
| 126 | + throw new RuntimeException(openssl_error_string()); // @codeCoverageIgnore |
| 127 | + } |
118 | 128 |
|
119 |
| - openssl_sign("{$header}.{$claims}", $signature, $key, 'sha256'); |
| 129 | + $segments[] = $this->encodeB64URLSafe($signature); |
120 | 130 |
|
121 |
| - return sprintf('bearer %s.%s.%s', $header, $claims, base64_encode($signature)); |
| 131 | + return 'bearer ' . implode('.', $segments); |
122 | 132 | }
|
123 | 133 |
|
124 | 134 | /**
|
125 |
| - * @param array $input |
| 135 | + * @param string $input |
126 | 136 | *
|
127 | 137 | * @return string
|
128 | 138 | */
|
129 |
| - private function jwtEncode($input) |
| 139 | + private function encodeB64URLSafe($input) |
130 | 140 | {
|
131 |
| - return str_replace('=', '', strtr(base64_encode(json_encode($input)), '+/', '-_')); |
| 141 | + return str_replace('=', '', strtr(base64_encode($input), '+/', '-_')); |
132 | 142 | }
|
133 | 143 | }
|
0 commit comments