Load site settings from dotenv and store OAuth credentials
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user