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
|
|
|
$pem = str_replace('-----BEGIN PUBLIC KEY-----', '', $pem); |
33
|
|
|
$pem = str_replace('-----END PUBLIC KEY-----', '', $pem); |
34
|
|
|
$pem = str_replace("\n", '', $pem); |
35
|
|
|
return $pem; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function certToArray($cert) { |
39
|
|
|
return [ |
40
|
|
|
'kty' => 'RSA', |
41
|
|
|
'alg' => 'RSA256', |
42
|
|
|
'use' => 'sig', |
43
|
|
|
'kid' => $cert->getKid(), |
44
|
|
|
'n' => $cert->getN(), |
45
|
|
|
'e' => $cert->getE(), |
46
|
|
|
'x5c' => self::pemToInline($cert->getPublicKey()) |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function generateRSAKeys() { |
51
|
|
|
$config = array( |
52
|
|
|
"digest_alg" => "sha512", |
53
|
|
|
"private_key_bits" => 4096, |
54
|
|
|
"private_key_type" => OPENSSL_KEYTYPE_RSA, |
55
|
|
|
); |
56
|
|
|
// Create the private and public key |
57
|
|
|
$res = openssl_pkey_new($config); |
58
|
|
|
|
59
|
|
|
// Extract the private key from $res to $privKey |
60
|
|
|
openssl_pkey_export($res, $privKey); |
61
|
|
|
|
62
|
|
|
// Extract the public key from $res to $pubKey |
63
|
|
|
$details = openssl_pkey_get_details($res); |
64
|
|
|
|
65
|
|
|
$pubKey = $details["key"]; |
66
|
|
|
return ['privKey' => $privKey, 'pubKey' => $pubKey, 'rsa' => $details['rsa']]; |
67
|
|
|
} |
68
|
|
|
} |