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 ;
@@ -51,14 +51,14 @@ public static function validate(array $vapid): array
5151 throw new \ErrorException ('Failed to convert VAPID public key from hexadecimal to binary ' );
5252 }
5353 $ vapid ['publicKey ' ] = base64_encode ($ binaryPublicKey );
54- $ vapid ['privateKey ' ] = base64_encode (str_pad (Base64Url ::decode ($ jwk ->get ('d ' )), self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
54+ $ vapid ['privateKey ' ] = base64_encode (str_pad (Base64UrlSafe ::decode ($ jwk ->get ('d ' )), self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
5555 }
5656
5757 if (!isset ($ vapid ['publicKey ' ])) {
5858 throw new \ErrorException ('[VAPID] You must provide a public key. ' );
5959 }
6060
61- $ publicKey = Base64Url ::decode ($ vapid ['publicKey ' ]);
61+ $ publicKey = Base64UrlSafe ::decode ($ vapid ['publicKey ' ]);
6262
6363 if (Utils::safeStrlen ($ publicKey ) !== self ::PUBLIC_KEY_LENGTH ) {
6464 throw new \ErrorException ('[VAPID] Public key should be 65 bytes long when decoded. ' );
@@ -68,7 +68,7 @@ public static function validate(array $vapid): array
6868 throw new \ErrorException ('[VAPID] You must provide a private key. ' );
6969 }
7070
71- $ privateKey = Base64Url ::decode ($ vapid ['privateKey ' ]);
71+ $ privateKey = Base64UrlSafe ::decode ($ vapid ['privateKey ' ]);
7272
7373 if (Utils::safeStrlen ($ privateKey ) !== self ::PRIVATE_KEY_LENGTH ) {
7474 throw new \ErrorException ('[VAPID] Private key should be 32 bytes long when decoded. ' );
@@ -126,9 +126,9 @@ public static function getVapidHeaders(
126126 $ jwk = new JWK ([
127127 'kty ' => 'EC ' ,
128128 'crv ' => 'P-256 ' ,
129- 'x ' => Base64Url:: encode ($ x ),
130- 'y ' => Base64Url:: encode ($ y ),
131- 'd ' => Base64Url:: encode ($ privateKey ),
129+ 'x ' => Base64UrlSafe:: encodeUnpadded ($ x ),
130+ 'y ' => Base64UrlSafe:: encodeUnpadded ($ y ),
131+ 'd ' => Base64UrlSafe:: encodeUnpadded ($ privateKey ),
132132 ]);
133133
134134 $ jwsCompactSerializer = new CompactSerializer ();
@@ -140,7 +140,7 @@ public static function getVapidHeaders(
140140 ->build ();
141141
142142 $ jwt = $ jwsCompactSerializer ->serialize ($ jws , 0 );
143- $ encodedPublicKey = Base64Url:: encode ($ publicKey );
143+ $ encodedPublicKey = Base64UrlSafe:: encodeUnpadded ($ publicKey );
144144
145145 if ($ contentEncoding === ContentEncoding::aesgcm) {
146146 return [
@@ -174,14 +174,14 @@ public static function createVapidKeys(): array
174174 throw new \ErrorException ('Failed to convert VAPID public key from hexadecimal to binary ' );
175175 }
176176
177- $ binaryPrivateKey = hex2bin (str_pad (bin2hex (Base64Url ::decode ($ jwk ->get ('d ' ))), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
177+ $ binaryPrivateKey = hex2bin (str_pad (bin2hex (Base64UrlSafe ::decode ($ jwk ->get ('d ' ))), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
178178 if (!$ binaryPrivateKey ) {
179179 throw new \ErrorException ('Failed to convert VAPID private key from hexadecimal to binary ' );
180180 }
181181
182182 return [
183- 'publicKey ' => Base64Url ::encode ($ binaryPublicKey ),
184- 'privateKey ' => Base64Url ::encode ($ binaryPrivateKey ),
183+ 'publicKey ' => Base64UrlSafe ::encode ($ binaryPublicKey ),
184+ 'privateKey ' => Base64UrlSafe ::encode ($ binaryPrivateKey ),
185185 ];
186186 }
187187}
0 commit comments