|
5 | 5 | class Install |
6 | 6 | { |
7 | 7 | public const WEBMAN_PLUGIN = true; |
| 8 | + private const ACCESS_PLACEHOLDER = '__JWT_ACCESS_SECRET_KEY__'; |
| 9 | + private const REFRESH_PLACEHOLDER = '__JWT_REFRESH_SECRET_KEY__'; |
8 | 10 |
|
9 | 11 | /** |
10 | 12 | * @var array |
@@ -46,6 +48,45 @@ public static function installByRelation() |
46 | 48 | } |
47 | 49 | //symlink(__DIR__ . "/$source", base_path()."/$dest"); |
48 | 50 | copy_dir(__DIR__ . "/$source", base_path()."/$dest"); |
| 51 | + self::initJwtSecrets(base_path()."/$dest/app.php"); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * 安装时初始化JWT密钥(64位随机字符串) |
| 57 | + * @param string $configFile |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + protected static function initJwtSecrets(string $configFile): void |
| 61 | + { |
| 62 | + if (!is_file($configFile) || !is_readable($configFile) || !is_writable($configFile)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + $content = file_get_contents($configFile); |
| 67 | + if (!is_string($content) || $content === '') { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (strpos($content, self::ACCESS_PLACEHOLDER) === false && strpos($content, self::REFRESH_PLACEHOLDER) === false) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + $accessKey = bin2hex(random_bytes(32)); |
| 77 | + $refreshKey = bin2hex(random_bytes(32)); |
| 78 | + } catch (\Exception $e) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + $updated = str_replace( |
| 83 | + [self::ACCESS_PLACEHOLDER, self::REFRESH_PLACEHOLDER], |
| 84 | + [$accessKey, $refreshKey], |
| 85 | + $content |
| 86 | + ); |
| 87 | + |
| 88 | + if ($updated !== $content) { |
| 89 | + file_put_contents($configFile, $updated); |
49 | 90 | } |
50 | 91 | } |
51 | 92 |
|
|
0 commit comments