1010
1111namespace Minishlink \WebPush ;
1212
13- use Base64Url \Base64Url ;
1413use Jose \Component \Core \AlgorithmManager ;
1514use Jose \Component \Core \JWK ;
15+ use Jose \Component \Core \Util \Base64UrlSafe ;
1616use Jose \Component \KeyManagement \JWKFactory ;
1717use Jose \Component \Signature \Algorithm \ES256 ;
1818use Jose \Component \Signature \JWSBuilder ;
@@ -69,14 +69,14 @@ public static function validate(array $vapid): array
6969 throw new \ErrorException ('[VAPID] Failed to convert VAPID public key from hexadecimal to binary. ' );
7070 }
7171 $ vapid ['publicKey ' ] = base64_encode ($ binaryPublicKey );
72- $ vapid ['privateKey ' ] = base64_encode (str_pad (Base64Url ::decode ($ jwk ->get ('d ' )), self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
72+ $ vapid ['privateKey ' ] = base64_encode (str_pad (Base64UrlSafe ::decode ($ jwk ->get ('d ' )), self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
7373 }
7474
7575 if (!isset ($ vapid ['publicKey ' ])) {
7676 throw new \ErrorException ('[VAPID] You must provide a public key. ' );
7777 }
7878
79- $ publicKey = Base64Url ::decode ($ vapid ['publicKey ' ]);
79+ $ publicKey = Base64UrlSafe ::decode ($ vapid ['publicKey ' ]);
8080
8181 if (Utils::safeStrlen ($ publicKey ) !== self ::PUBLIC_KEY_LENGTH ) {
8282 throw new \ErrorException ('[VAPID] Public key should be 65 bytes long when decoded. ' );
@@ -86,7 +86,7 @@ public static function validate(array $vapid): array
8686 throw new \ErrorException ('[VAPID] You must provide a private key. ' );
8787 }
8888
89- $ privateKey = Base64Url ::decode ($ vapid ['privateKey ' ]);
89+ $ privateKey = Base64UrlSafe ::decode ($ vapid ['privateKey ' ]);
9090
9191 if (Utils::safeStrlen ($ privateKey ) !== self ::PRIVATE_KEY_LENGTH ) {
9292 throw new \ErrorException ('[VAPID] Private key should be 32 bytes long when decoded. ' );
@@ -148,9 +148,9 @@ public static function getVapidHeaders(
148148 $ jwk = new JWK ([
149149 'kty ' => 'EC ' ,
150150 'crv ' => 'P-256 ' ,
151- 'x ' => Base64Url:: encode ($ x ),
152- 'y ' => Base64Url:: encode ($ y ),
153- 'd ' => Base64Url:: encode ($ privateKey ),
151+ 'x ' => Base64UrlSafe:: encodeUnpadded ($ x ),
152+ 'y ' => Base64UrlSafe:: encodeUnpadded ($ y ),
153+ 'd ' => Base64UrlSafe:: encodeUnpadded ($ privateKey ),
154154 ]);
155155
156156 $ jwsCompactSerializer = new CompactSerializer ();
@@ -162,7 +162,7 @@ public static function getVapidHeaders(
162162 ->build ();
163163
164164 $ jwt = $ jwsCompactSerializer ->serialize ($ jws , 0 );
165- $ encodedPublicKey = Base64Url:: encode ($ publicKey );
165+ $ encodedPublicKey = Base64UrlSafe:: encodeUnpadded ($ publicKey );
166166
167167 if ($ contentEncoding === ContentEncoding::aesgcm) {
168168 return [
@@ -196,14 +196,14 @@ public static function createVapidKeys(): array
196196 throw new \ErrorException ('Failed to convert VAPID public key from hexadecimal to binary. ' );
197197 }
198198
199- $ binaryPrivateKey = hex2bin (str_pad (bin2hex (Base64Url ::decode ($ jwk ->get ('d ' ))), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
199+ $ binaryPrivateKey = hex2bin (str_pad (bin2hex (Base64UrlSafe ::decode ($ jwk ->get ('d ' ))), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
200200 if (!$ binaryPrivateKey ) {
201201 throw new \ErrorException ('Failed to convert VAPID private key from hexadecimal to binary. ' );
202202 }
203203
204204 return [
205- 'publicKey ' => Base64Url ::encode ($ binaryPublicKey ),
206- 'privateKey ' => Base64Url ::encode ($ binaryPrivateKey ),
205+ 'publicKey ' => Base64UrlSafe ::encode ($ binaryPublicKey ),
206+ 'privateKey ' => Base64UrlSafe ::encode ($ binaryPrivateKey ),
207207 ];
208208 }
209209}
0 commit comments