|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Utility; |
|
4
|
|
|
|
|
5
|
|
|
use League\OAuth2\Server\CryptKey; |
|
6
|
|
|
use phpseclib\Crypt\RSA; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Helper |
|
10
|
|
|
* @package ByTIC\Hello\Utility |
|
11
|
|
|
*/ |
|
12
|
|
|
class CryptHelper |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The storage location of the encryption keys. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
public static $keyPath; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Set the storage location of the encryption keys. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $path |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function loadKeysFrom($path) |
|
28
|
|
|
{ |
|
29
|
|
|
static::$keyPath = $path; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The location of the encryption keys. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $file |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public static function keyPath($file) |
|
39
|
|
|
{ |
|
40
|
3 |
|
$file = ltrim($file, '/\\'); |
|
41
|
3 |
|
if (static::$keyPath) { |
|
42
|
|
|
return rtrim(static::$keyPath, '/\\') . DIRECTORY_SEPARATOR . $file; |
|
43
|
|
|
} |
|
44
|
3 |
|
if (function_exists('Nip\storage_path')) { |
|
45
|
3 |
|
return \Nip\storage_path('hello/keys/' . $file); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (function_exists('storage_path')) { |
|
49
|
|
|
return storage_path('hello/keys/' . $file); |
|
50
|
|
|
} |
|
51
|
|
|
return PathHelper::keys($file); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param null $basePath |
|
|
|
|
|
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public static function generateKeys($basePath = null) |
|
59
|
|
|
{ |
|
60
|
2 |
|
$privateKeyName = 'oauth-private.key'; |
|
61
|
2 |
|
$publicKeyName = 'oauth-public.key'; |
|
62
|
2 |
|
$privateKeyPath = $basePath ? $basePath . DIRECTORY_SEPARATOR . $privateKeyName : static::keyPath($privateKeyName); |
|
|
|
|
|
|
63
|
2 |
|
$publicKeyPath = $basePath ? $basePath . DIRECTORY_SEPARATOR . $publicKeyName : static::keyPath($publicKeyName); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
2 |
|
$rsa = new RSA(); |
|
66
|
2 |
|
$keys = $rsa->createKey(2048, false); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
2 |
|
file_put_contents($privateKeyPath, $keys['privatekey']); |
|
69
|
2 |
|
file_put_contents($publicKeyPath, $keys['publickey']); |
|
70
|
|
|
|
|
71
|
2 |
|
$result = chmod($privateKeyPath, 0600); |
|
72
|
2 |
|
$result = $result && chmod($publicKeyPath, 0600); |
|
73
|
|
|
|
|
74
|
2 |
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $type |
|
79
|
|
|
* @return CryptKey |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public static function makeCryptKey($type) |
|
82
|
|
|
{ |
|
83
|
2 |
|
$configKey = null; |
|
|
|
|
|
|
84
|
2 |
|
$configKey = ConfigHelper::get($type . '_key'); |
|
85
|
|
|
|
|
86
|
2 |
|
$key = str_replace('\\n', "\n", $configKey); |
|
87
|
2 |
|
if (!$key) { |
|
88
|
1 |
|
$path = CryptHelper::keyPath('oauth-' . $type . '.key'); |
|
89
|
1 |
|
if (!file_exists($path)) { |
|
90
|
1 |
|
CryptHelper::generateKeys(dirname($path)); |
|
91
|
|
|
} |
|
92
|
1 |
|
$key = 'file://' . $path; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
return new CryptKey($key, null, false); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|