friendship ended with social-app. php is my new best friend
at main 1.5 kB view raw
1<?php 2declare(strict_types=1); 3 4namespace Lcobucci\JWT\Encoding; 5 6use JsonException; 7use Lcobucci\JWT\Decoder; 8use Lcobucci\JWT\Encoder; 9use Lcobucci\JWT\SodiumBase64Polyfill; 10 11use function json_decode; 12use function json_encode; 13 14use const JSON_THROW_ON_ERROR; 15use const JSON_UNESCAPED_SLASHES; 16use const JSON_UNESCAPED_UNICODE; 17 18/** 19 * A utilitarian class that encodes and decodes data according to JOSE specifications 20 */ 21final class JoseEncoder implements Encoder, Decoder 22{ 23 public function jsonEncode(mixed $data): string 24 { 25 try { 26 return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR); 27 } catch (JsonException $exception) { 28 throw CannotEncodeContent::jsonIssues($exception); 29 } 30 } 31 32 public function jsonDecode(string $json): mixed 33 { 34 try { 35 return json_decode(json: $json, associative: true, flags: JSON_THROW_ON_ERROR); 36 } catch (JsonException $exception) { 37 throw CannotDecodeContent::jsonIssues($exception); 38 } 39 } 40 41 public function base64UrlEncode(string $data): string 42 { 43 return SodiumBase64Polyfill::bin2base64( 44 $data, 45 SodiumBase64Polyfill::SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING, 46 ); 47 } 48 49 public function base64UrlDecode(string $data): string 50 { 51 return SodiumBase64Polyfill::base642bin( 52 $data, 53 SodiumBase64Polyfill::SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING, 54 ); 55 } 56}