feat(auth): 统一身份 - 增加可选IAM快捷登录并保留原业务权限

This commit is contained in:
2026-09-10 10:33:03 +08:00
parent 27fbef9321
commit aa0d22bbe2
518 changed files with 5919 additions and 13 deletions
+115
View File
@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
namespace think\facade {
// In-memory config only: no application bootstrap, database, cache or network.
final class Config
{
public static array $values = [];
public static function get(string $key, $default = null) { return self::$values[$key] ?? $default; }
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use app\adminapi\service\iam\IamLoginService;
use app\adminapi\service\iam\IamLoginTransactionStore;
use think\facade\Config;
function txExpect(bool $ok, string $message): void {
if (!$ok) { throw new \RuntimeException('FAIL: ' . $message); }
}
function txReject(callable $call, string $message): void {
try { $call(); } catch (\Throwable $error) { return; }
throw new \RuntimeException('FAIL: accepted ' . $message);
}
$directory = sys_get_temp_dir() . '/zyt-iam-test-' . bin2hex(random_bytes(12));
try {
$store = new IamLoginTransactionStore($directory);
$value = bin2hex(random_bytes(32));
$browser = bin2hex(random_bytes(32));
$store->put('state', $value, $browser, ['nonce' => 'nonce'], 600);
txReject(fn() => $store->consume('state', $value, 'another-browser'), 'wrong browser');
txReject(fn() => $store->consume('ticket', $value, $browser), 'state as ticket');
txExpect($store->consume('state', $value, $browser) === ['nonce' => 'nonce'], 'correct browser can consume after rejected foreign browser');
txReject(fn() => $store->consume('state', $value, $browser), 'state replay');
$store->put('ticket', $value, $browser, ['subject' => 'employee'], 60);
txReject(fn() => $store->consume('state', $value, $browser), 'ticket as state');
txExpect($store->consume('ticket', $value, $browser) === ['subject' => 'employee'], 'same value separate ticket namespace');
txReject(fn() => $store->consume('ticket', $value, $browser), 'ticket replay');
foreach (['state', 'ticket'] as $kind) {
$expired = bin2hex(random_bytes(32));
$store->put($kind, $expired, $browser, ['expired' => true], -1);
txReject(fn() => $store->consume($kind, $expired, $browser), 'expired ' . $kind);
}
foreach (['../escape', '', str_repeat('z', 64), str_repeat('a', 63)] as $invalid) {
txReject(fn() => $store->put('state', $invalid, $browser, [], 60), 'invalid transaction identifier');
}
txReject(fn() => $store->consume('invalid-kind', $value, $browser), 'invalid namespace');
foreach (glob($directory . '/*.json') ?: [] as $file) {
txExpect((fileperms($file) & 0777) === 0600, 'private file permissions');
txExpect(!str_contains((string) file_get_contents($file), $browser), 'browser cookie is hashed at rest');
}
for ($i = 1; $i <= 20; ++$i) {
txExpect($store->allowStart('192.0.2.1'), 'allowed request ' . $i);
}
txExpect(!$store->allowStart('192.0.2.1'), 'request 21 rejected');
txExpect(!$store->allowStart('192.0.2.1'), 'later request rejected');
txExpect($store->allowStart('192.0.2.2'), 'rate limit IP isolation');
$limit = $directory . '/limit-' . hash('sha256', '192.0.2.1') . '.json';
file_put_contents($limit, json_encode(['count' => 20, 'expires' => time() - 1]));
txExpect($store->allowStart('192.0.2.1'), 'expired rate limit resets');
$context = ['applicationId' => 'zyt', 'localIdentityKey' => 'employee',
'employee' => ['oidcSubject' => 'employee', 'status' => 'active'],
'externalAccountId' => '42', 'shouldCreateLocalAccount' => false];
txExpect(IamLoginService::boundAdminId($context, 'employee', 'zyt') === 42, 'explicit existing binding');
foreach (['applicationId' => 'other', 'localIdentityKey' => 'other', 'externalAccountId' => '',
'shouldCreateLocalAccount' => true] as $field => $bad) {
$changed = $context;
$changed[$field] = $bad;
txReject(fn() => IamLoginService::boundAdminId($changed, 'employee', 'zyt'), $field);
}
foreach (['oidcSubject' => 'other', 'status' => 'disabled'] as $field => $bad) {
$changed = $context;
$changed['employee'][$field] = $bad;
txReject(fn() => IamLoginService::boundAdminId($changed, 'employee', 'zyt'), 'employee ' . $field);
}
foreach (['0', '-1', '01', '42.1', '42e1', 'admin', '42 ', '99999999999', null, true, false, 42.0] as $bad) {
$changed = $context;
$changed['externalAccountId'] = $bad;
txReject(fn() => IamLoginService::boundAdminId($changed, 'employee', 'zyt'), 'invalid externalAccountId');
}
foreach (array_keys($context) as $field) {
$changed = $context;
unset($changed[$field]);
txReject(fn() => IamLoginService::boundAdminId($changed, 'employee', 'zyt'), 'missing ' . $field);
}
$changed = $context;
$changed['shouldCreateLocalAccount'] = 'false';
txReject(fn() => IamLoginService::boundAdminId($changed, 'employee', 'zyt'), 'non-boolean creation flag');
foreach ([[], ['enabled' => false], ['enabled' => 'false'], ['enabled' => true]] as $config) {
Config::$values = ['iam' => $config];
txExpect(IamLoginService::settings() === ['enabled' => false, 'loginUrl' => ''], 'disabled/incomplete config stays closed');
txReject(fn() => new IamLoginService(), 'disabled constructor without bootstrap');
}
$config = ['enabled' => true, 'issuer' => 'https://iam.example.test', 'client_id' => 'zyt',
'client_secret' => 'secret', 'redirect_uri' => 'https://zyt.example.test/adminapi/iam/callback',
'api_url' => 'https://iam.example.test', 'application_id' => 'zyt', 'application_token' => 'secret',
'public_url' => 'https://zyt.example.test'];
Config::$values = ['iam' => $config];
txExpect(IamLoginService::settings() === ['enabled' => true, 'loginUrl' => 'https://zyt.example.test/adminapi/iam/start'], 'complete config enables without HTTP');
$config['redirect_uri'] = 'https://zyt.example.test/wrong';
Config::$values = ['iam' => $config];
txExpect(IamLoginService::settings()['enabled'] === false, 'callback URI mismatch');
echo "IamLoginTransactionTest passed (browser binding, replay, namespaces, expiry, 20-request limit, explicit binding, disabled config; no DB/network)\n";
} finally {
foreach (glob($directory . '/*') ?: [] as $file) { unlink($file); }
if (is_dir($directory)) { rmdir($directory); }
}
}