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
+17 -17
View File
@@ -6,6 +6,7 @@ namespace App\Controller;
use App\Content\DesignLibrary;
use App\Content\IdentityProvider;
use App\Content\OAuthCredentials;
use App\Content\Repository;
use App\Content\View;
use App\Http\VisitorSession;
@@ -17,17 +18,18 @@ final class AccountController
{
public function login(Request $request, string $id): Response
{
$config = $this->provider((int) $id);
if ($config === []) {
$layout = DesignLibrary::find((int) $id, 'header');
$identity = DesignLibrary::identity($layout);
if ($identity === null) {
return $this->page($request, '该用户区尚未配置登录。', 404);
}
try {
$provider = new IdentityProvider($config);
$provider = new IdentityProvider($layout['payload']['provider'], OAuthCredentials::read((int) $id, $layout['credential_revision']));
$nonce = bin2hex(random_bytes(32));
$url = $provider->authorization($nonce);
$session = VisitorSession::create();
$session->set('flow', [
'layout' => (int) $id, 'config' => $this->fingerprint($config), 'nonce' => $nonce,
'layout' => (int) $id, 'config' => $identity, 'nonce' => $nonce,
'state' => $provider->client->getState(), 'proof' => $provider->client->getPkceCode(), 'expires' => time() + 600,
]);
$session->save();
@@ -51,11 +53,12 @@ final class AccountController
$session->flush();
$session->save();
try {
$config = $this->provider($flow['layout']);
if (!$config || $this->fingerprint($config) !== $flow['config']) {
$layout = DesignLibrary::find($flow['layout'], 'header');
if (DesignLibrary::identity($layout) !== $flow['config']) {
return $this->page($request, '登录配置已变更,请重新登录。', 409);
}
$profile = (new IdentityProvider($config))->profile($code, $flow['proof'], $flow['nonce']);
$secret = OAuthCredentials::read($flow['layout'], $layout['credential_revision']);
$profile = (new IdentityProvider($layout['payload']['provider'], $secret))->profile($code, $flow['proof'], $flow['nonce']);
$current = VisitorSession::create();
$current->set('profile', $profile + [
'layout' => $flow['layout'], 'config' => $flow['config'], 'expires' => time() + 28800, 'logout' => bin2hex(random_bytes(32)),
@@ -91,20 +94,17 @@ final class AccountController
{
return View::render('account', [
'site' => (new Repository())->site(), 'title' => '用户中心', 'description' => '', 'path' => '/account',
'error' => $error, 'profile' => VisitorSession::profile($request), 'noindex' => true,
'error' => $error, 'profile' => $this->profile($request), 'noindex' => true,
], $status)->withHeader('Cache-Control', 'private, no-store');
}
private function provider(int $id): array
private function profile(Request $request): ?array
{
$row = DesignLibrary::find($id, 'header');
$profile = VisitorSession::profile($request);
if ($profile && DesignLibrary::identity(DesignLibrary::find($profile['layout'], 'header')) === $profile['config']) {
return $profile;
}
return ($row['preset'] ?? '') === 'member' && in_array('account', $row['payload']['components'], true)
? $row['payload']['provider'] : [];
}
private function fingerprint(array $config): string
{
return hash('sha256', json_encode($config, JSON_THROW_ON_ERROR));
return null;
}
}
+11 -5
View File
@@ -43,6 +43,8 @@ final class DesignController extends AdminController
$default = $id !== null && ((new Repository())->site()[$kind . '_id'] ?? null) === $id;
if ($request->method() === 'POST') {
try {
$savedProvider = $item['payload']['provider'] ?? [];
$secret = '';
$item['name'] = $this->input($request, 'name');
$item['preset'] = $this->input($request, 'preset');
if ($kind === 'palette') {
@@ -58,14 +60,18 @@ final class DesignController extends AdminController
'text' => $this->input($request, 'text'), 'legal' => $this->input($request, 'legal'),
'links' => SiteDesign::parseLinks($this->input($request, 'links')),
'action' => $url === '' && $label === '' ? [] : [[$url, $label, $this->input($request, 'action_style'), $request->post('action_blank') === '1']],
'provider' => [],
'provider' => $savedProvider,
];
foreach (['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id', 'secret_env', 'scope', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) {
$item['payload']['provider'][$field] = $this->input($request, 'provider_' . $field);
$components = $item['payload']['components'];
if ($kind === 'header' && is_array($components) && DesignLibrary::hasAccount($item['preset'], $components)) {
foreach (['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id', 'scope', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) {
$item['payload']['provider'][$field] = $this->input($request, 'provider_' . $field);
}
$secret = $this->input($request, 'provider_secret', trimValue: false);
}
}
$default = $request->post('make_default') === '1';
$id = DesignLibrary::save($kind, $id, $item['name'], $item['preset'], $item['payload'], $this->version($request), $default);
$id = DesignLibrary::save($kind, $id, $item['name'], $item['preset'], $item['payload'], $this->version($request), $default, $secret);
return $this->success($request, "/admin/design/$kind/$id?saved=1");
} catch (RuntimeException $ex) {
@@ -111,7 +117,7 @@ final class DesignController extends AdminController
], $error === '' ? 200 : 422);
}
public function preview(Request $request, string $kind, string $id): Response
public function previewDesign(Request $request, string $kind, string $id): Response
{
if ($failure = $this->guard($request)) {
return $failure;
+2 -1
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller;
use App\Config\Environment;
use App\Content\BlockTemplates;
use App\Content\Database;
use App\Content\DesignLibrary;
@@ -26,7 +27,7 @@ final class McpController
if ($origin !== null && $origin !== '' && $origin !== rtrim(View::absolute(''), '/')) {
return new Response(403, ['Cache-Control' => 'no-store']);
}
$secret = getenv('LAYSITE_MCP_TOKEN');
$secret = Environment::get('LAYSITE_MCP_TOKEN');
$header = $request->header('authorization', '');
if (!is_string($secret) || strlen($secret) < 32 || !is_string($header) || !hash_equals('Bearer ' . $secret, $header)) {
return new Response(401, ['WWW-Authenticate' => 'Bearer realm="LaySite MCP"', 'Cache-Control' => 'no-store']);