1 | <?php |
||
2 | |||
3 | if (!function_exists('isEmptyOrNull')) { |
||
4 | function isEmptyOrNull($value): bool |
||
5 | { |
||
6 | 14 | return is_null($value) || empty($value) || '' === $value; |
|
7 | } |
||
8 | } |
||
9 | |||
10 | if (!function_exists('base64URLEncode')) { |
||
11 | /** |
||
12 | * base64URLEncode |
||
13 | * |
||
14 | * @source https://base64.guru/developers/php/examples/base64url |
||
15 | */ |
||
16 | function base64URLEncode(string $string): string |
||
17 | { |
||
18 | // First of all you should encode $data to Base64 string |
||
19 | 1 | $b64 = base64_encode($string); |
|
20 | |||
21 | // Make sure you get a valid result, otherwise, return FALSE, as the base64_encode() function do |
||
22 | 1 | if ($b64 === false) { |
|
23 | return false; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
24 | } |
||
25 | |||
26 | // Convert Base64 to Base64URL by replacing “+” with “-” and “/” with “_” |
||
27 | 1 | $url = strtr($b64, '+/', '-_'); |
|
28 | |||
29 | // Remove padding character from the end of line and return the Base64URL result |
||
30 | 1 | return rtrim($url, '='); |
|
31 | } |
||
32 | } |
||
33 | |||
34 | if (!function_exists('createVerifier')) { |
||
35 | function createVerifier(): string |
||
36 | { |
||
37 | $random = bin2hex(openssl_random_pseudo_bytes(32)); |
||
38 | |||
39 | return \base64URLEncode(pack('H*', $random)); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | if (!function_exists('createChallenge')) { |
||
44 | function createChallenge(string $verifier): string |
||
45 | { |
||
46 | return \base64URLEncode(pack('H*', hash('sha256', $verifier))); |
||
47 | } |
||
48 | } |
||
49 |