Load site settings from dotenv and store OAuth credentials

This commit is contained in:
2026-11-23 16:53:29 +08:00
parent 1bb0554608
commit 01ede89dcd
27 changed files with 730 additions and 76 deletions
+42 -7
View File
@@ -42,7 +42,9 @@ final class DesignLibrary
if ($id < 1) {
return null;
}
$query = Database::connect()->prepare('SELECT * FROM design_presets WHERE id = ? AND kind = ?');
$query = Database::connect()->prepare('SELECT design_presets.*, credentials.revision AS credential_revision
FROM design_presets LEFT JOIN layout_oauth_credentials credentials ON credentials.layout_id = design_presets.id
WHERE design_presets.id = ? AND kind = ?');
$query->execute([$id, $kind]);
$row = $query->fetch();
if (!$row) {
@@ -141,14 +143,14 @@ final class DesignLibrary
throw new RuntimeException('请选择 OIDC 或 OAuth 2.0。');
}
$result = ['mode' => $mode];
foreach (['client_id', 'secret_env', 'scope', 'issuer', 'authorization', 'token', 'userinfo', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) {
foreach (['client_id', 'scope', 'issuer', 'authorization', 'token', 'userinfo', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) {
$result[$field] = self::string($data[$field] ?? '', 600);
}
if (!in_array($result['token_auth'], ['', 'client_secret_basic', 'client_secret_post'], true)) {
throw new RuntimeException('令牌端点认证方式无效。');
}
if ($result['client_id'] === '' || !preg_match('/^LAYSITE_OAUTH_[A-Z0-9_]{1,45}$/D', $result['secret_env'])) {
throw new RuntimeException('填写客户端 ID 和以 LAYSITE_OAUTH_ 开头的客户端密钥环境变量名。');
if ($result['client_id'] === '') {
throw new RuntimeException('请填写客户端 ID。');
}
foreach ($mode === 'oidc' ? ['issuer'] : ['authorization', 'token', 'userinfo'] as $field) {
$url = parse_url($result[$field]);
@@ -164,15 +166,41 @@ final class DesignLibrary
return $result;
}
public static function save(string $kind, ?int $id, string $name, string $preset, array $data, int $version, bool $default): int
public static function hasAccount(string $preset, array $components): bool
{
return $preset === 'member' && in_array('account', $components, true);
}
public static function identity(?array $layout): ?string
{
if (empty($layout['credential_revision']) || empty($layout['payload']['provider'])
|| !self::hasAccount($layout['preset'], $layout['payload']['components'])) {
return null;
}
return hash('sha256', Database::json([$layout['payload']['provider'], $layout['credential_revision']]));
}
public static function save(string $kind, ?int $id, string $name, string $preset, array $data, int $version, bool $default, string $secret = ''): int
{
$name = self::string($name, 100);
if ($name === '') {
throw new RuntimeException('请填写配置名称。');
}
$payload = Database::json(self::normalize($kind, $preset, $data));
$clean = self::normalize($kind, $preset, $data);
$payload = Database::json($clean);
$provider = $clean['provider'] ?? [];
$active = $provider !== [] && self::hasAccount($preset, $clean['components']);
return Database::write(function (PDO $pdo) use ($kind, $id, $name, $preset, $payload, $version, $default): int {
return Database::write(function (PDO $pdo) use ($kind, $id, $name, $preset, $payload, $version, $default, $provider, $active, $secret): int {
if ($active && $secret === '') {
$saved = $id === null ? null : self::find($id, $kind);
$fields = array_flip(['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id']);
if (empty($saved['credential_revision'])
|| array_intersect_key($provider, $fields) !== array_intersect_key($saved['payload']['provider'], $fields)) {
throw new RuntimeException('启用登录或变更身份提供商时,请填写客户端密钥。');
}
}
if ($id === null) {
$pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)')->execute([$kind, $name, $preset, $payload]);
$id = (int) $pdo->lastInsertId();
@@ -183,6 +211,13 @@ final class DesignLibrary
throw new RuntimeException('配置已更新,请重新打开后再保存。');
}
}
if ($kind === 'header') {
if ($provider === []) {
$pdo->prepare('DELETE FROM layout_oauth_credentials WHERE layout_id = ?')->execute([$id]);
} elseif ($active && $secret !== '') {
OAuthCredentials::store($pdo, $id, $secret);
}
}
if ($kind !== 'palette') {
$site = json_decode($pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
if ($default) {