Files
laysense.cn/app/Controller/AccountController.php
T

111 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
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;
use support\Request;
use support\Response;
use Throwable;
final class AccountController
{
public function login(Request $request, string $id): Response
{
$layout = DesignLibrary::find((int) $id, 'header');
$identity = DesignLibrary::identity($layout);
if ($identity === null) {
return $this->page($request, '该用户区尚未配置登录。', 404);
}
try {
$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' => $identity, 'nonce' => $nonce,
'state' => $provider->client->getState(), 'proof' => $provider->client->getPkceCode(), 'expires' => time() + 600,
]);
$session->save();
return VisitorSession::cookie(View::redirect($url), $session, 600);
} catch (Throwable $error) {
error_log('LaySite account login: ' . get_class($error));
return $this->page($request, '无法连接身份服务,请联系站点管理员。', 502);
}
}
public function callback(Request $request): Response
{
$session = VisitorSession::open($request);
$flow = $session?->get('flow');
$state = $request->get('state');
$code = $request->get('code');
if (!is_array($flow) || $flow['expires'] < time() || !is_string($state) || !hash_equals($flow['state'], $state)
|| !is_string($code) || $code === '' || strlen($code) > 4096) {
return $this->page($request, '登录请求已过期或未完成,请重新登录。', 400);
}
$session->flush();
$session->save();
try {
$layout = DesignLibrary::find($flow['layout'], 'header');
if (DesignLibrary::identity($layout) !== $flow['config']) {
return $this->page($request, '登录配置已变更,请重新登录。', 409);
}
$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)),
]);
$current->save();
return VisitorSession::cookie(View::redirect('/account'), $current, 28800);
} catch (Throwable $error) {
error_log('LaySite account callback: ' . get_class($error));
return $this->page($request, '身份验证失败,请重新登录。', 502);
}
}
public function index(Request $request): Response
{
return $this->page($request);
}
public function logout(Request $request): Response
{
$profile = VisitorSession::profile($request);
$token = $request->post('token');
if (!$profile || !is_string($token) || !hash_equals($profile['logout'], $token)) {
return $this->page($request, '退出请求已过期。', 403);
}
$session = VisitorSession::open($request);
$session->flush();
$session->save();
return VisitorSession::cookie(View::redirect('/'), null, 0);
}
private function page(Request $request, string $error = '', int $status = 200): Response
{
return View::render('account', [
'site' => (new Repository())->site(), 'title' => '用户中心', 'description' => '', 'path' => '/account',
'error' => $error, 'profile' => $this->profile($request), 'noindex' => true,
], $status)->withHeader('Cache-Control', 'private, no-store');
}
private function profile(Request $request): ?array
{
$profile = VisitorSession::profile($request);
if ($profile && DesignLibrary::identity(DesignLibrary::find($profile['layout'], 'header')) === $profile['config']) {
return $profile;
}
return null;
}
}