|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Alexandre |
|
5
|
|
|
* Date: 10/03/2018 |
|
6
|
|
|
* Time: 15:59 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OAuth2; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
abstract class Helper |
|
13
|
|
|
{ |
|
14
|
|
|
const CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
|
15
|
|
|
const LENGTH = 22; |
|
16
|
|
|
|
|
17
|
|
|
public static function containsNotAsciiChar(string $string) |
|
18
|
|
|
{ |
|
19
|
|
|
return preg_match('/[^\x20-\x7e]/', $string); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public static function generateToken($length = self::LENGTH) |
|
23
|
|
|
{ |
|
24
|
|
|
$token = ''; |
|
25
|
|
|
for ($i = 0; $i < $length; ++$i) { |
|
26
|
|
|
$token .= self::CHARS[random_int(0, strlen(self::CHARS) - 1)]; |
|
27
|
|
|
} |
|
28
|
|
|
return $token; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function pemToInline($pem) |
|
32
|
|
|
{ |
|
33
|
|
|
$pem = str_replace('-----BEGIN PUBLIC KEY-----', '', $pem); |
|
34
|
|
|
$pem = str_replace('-----END PUBLIC KEY-----', '', $pem); |
|
35
|
|
|
$pem = str_replace("\n", '', $pem); |
|
36
|
|
|
return $pem; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function certToArray($cert) |
|
40
|
|
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
'kty' => 'RSA', |
|
43
|
|
|
'alg' => 'RSA256', |
|
44
|
|
|
'use' => 'sig', |
|
45
|
|
|
'kid' => $cert->getKid(), |
|
46
|
|
|
'n' => $cert->getN(), |
|
47
|
|
|
'e' => $cert->getE(), |
|
48
|
|
|
'x5c' => self::pemToInline($cert->getPublicKey()) |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function generateRSAKeys() |
|
53
|
|
|
{ |
|
54
|
|
|
$config = array( |
|
55
|
|
|
"digest_alg" => "sha512", |
|
56
|
|
|
"private_key_bits" => 4096, |
|
57
|
|
|
"private_key_type" => OPENSSL_KEYTYPE_RSA, |
|
58
|
|
|
); |
|
59
|
|
|
// Create the private and public key |
|
60
|
|
|
$res = openssl_pkey_new($config); |
|
61
|
|
|
|
|
62
|
|
|
// Extract the private key from $res to $privKey |
|
63
|
|
|
openssl_pkey_export($res, $privKey); |
|
64
|
|
|
|
|
65
|
|
|
// Extract the public key from $res to $pubKey |
|
66
|
|
|
$details = openssl_pkey_get_details($res); |
|
67
|
|
|
|
|
68
|
|
|
$pubKey = $details["key"]; |
|
69
|
|
|
return ['privKey' => $privKey, 'pubKey' => $pubKey, 'rsa' => $details['rsa']]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $data |
|
74
|
|
|
* @return string |
|
75
|
|
|
* @src https://gist.github.com/nathggns/6652997 |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function base64url_encode($data) |
|
78
|
|
|
{ |
|
79
|
|
|
return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $data |
|
84
|
|
|
* @param bool $pad |
|
85
|
|
|
* @return bool|string |
|
86
|
|
|
* @src https://gist.github.com/nathggns/6652997 |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function base64url_decode($data, $pad = false) |
|
89
|
|
|
{ |
|
90
|
|
|
$data = strtr($data, '-_', '+/'); |
|
91
|
|
|
if ($pad) { |
|
92
|
|
|
$data = str_pad($data, strlen($data) + (4 - strlen($data) % 4) % 4); |
|
93
|
|
|
} |
|
94
|
|
|
return base64_decode($data); |
|
95
|
|
|
} |
|
96
|
|
|
} |