forked from laysense/laysite
111 lines
4.3 KiB
PHP
111 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Content\DesignLibrary;
|
|
use App\Content\IdentityProvider;
|
|
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
|
|
{
|
|
$config = $this->provider((int) $id);
|
|
if ($config === []) {
|
|
return $this->page($request, '该用户区尚未配置登录。', 404);
|
|
}
|
|
try {
|
|
$provider = new IdentityProvider($config);
|
|
$nonce = bin2hex(random_bytes(32));
|
|
$url = $provider->authorization($nonce);
|
|
$session = VisitorSession::create();
|
|
$session->set('flow', [
|
|
'layout' => (int) $id, 'config' => $this->fingerprint($config), '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 {
|
|
$config = $this->provider($flow['layout']);
|
|
if (!$config || $this->fingerprint($config) !== $flow['config']) {
|
|
return $this->page($request, '登录配置已变更,请重新登录。', 409);
|
|
}
|
|
$profile = (new IdentityProvider($config))->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' => VisitorSession::profile($request), 'noindex' => true,
|
|
], $status)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
private function provider(int $id): array
|
|
{
|
|
$row = DesignLibrary::find($id, 'header');
|
|
|
|
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));
|
|
}
|
|
}
|