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. ' );
@@ -131,9 +131,9 @@ public static function getVapidHeaders(
131131 $ jwk = new JWK ([
132132 'kty ' => 'EC ' ,
133133 'crv ' => 'P-256 ' ,
134- 'x ' => Base64Url:: encode ($ x ),
135- 'y ' => Base64Url:: encode ($ y ),
136- 'd ' => Base64Url:: encode ($ privateKey ),
134+ 'x ' => Base64UrlSafe:: encodeUnpadded ($ x ),
135+ 'y ' => Base64UrlSafe:: encodeUnpadded ($ y ),
136+ 'd ' => Base64UrlSafe:: encodeUnpadded ($ privateKey ),
137137 ]);
138138
139139 $ jwsCompactSerializer = new CompactSerializer ();
@@ -145,7 +145,7 @@ public static function getVapidHeaders(
145145 ->build ();
146146
147147 $ jwt = $ jwsCompactSerializer ->serialize ($ jws , 0 );
148- $ encodedPublicKey = Base64Url:: encode ($ publicKey );
148+ $ encodedPublicKey = Base64UrlSafe:: encodeUnpadded ($ publicKey );
149149
150150 if ($ contentEncoding === ContentEncoding::aesgcm) {
151151 return [
@@ -179,14 +179,14 @@ public static function createVapidKeys(): array
179179 throw new \ErrorException ('Failed to convert VAPID public key from hexadecimal to binary ' );
180180 }
181181
182- $ binaryPrivateKey = hex2bin (str_pad (bin2hex (Base64Url ::decode ($ jwk ->get ('d ' ))), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
182+ $ binaryPrivateKey = hex2bin (str_pad (bin2hex (Base64UrlSafe ::decode ($ jwk ->get ('d ' ))), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
183183 if (!$ binaryPrivateKey ) {
184184 throw new \ErrorException ('Failed to convert VAPID private key from hexadecimal to binary ' );
185185 }
186186
187187 return [
188- 'publicKey ' => Base64Url ::encode ($ binaryPublicKey ),
189- 'privateKey ' => Base64Url ::encode ($ binaryPrivateKey ),
188+ 'publicKey ' => Base64UrlSafe ::encode ($ binaryPublicKey ),
189+ 'privateKey ' => Base64UrlSafe ::encode ($ binaryPrivateKey ),
190190 ];
191191 }
192192}
0 commit comments