128 lines
8.2 KiB
PHP
128 lines
8.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\esign {
|
|
final class PrescriptionEsignService
|
|
{
|
|
public static array $calls = [];
|
|
public static array $starts = [];
|
|
public static array $verifications = [];
|
|
public static function start(int $id, int $actor, array $info, bool $restartIdentity = false): array
|
|
{
|
|
self::$starts[] = [$id, $actor, $info, $restartIdentity];
|
|
return ['status' => 'authenticating'];
|
|
}
|
|
public static function complete(int $id, int $actor, array $info, string $identityNumber = '', bool $verifyOnly = false): array
|
|
{
|
|
self::$calls[] = [$id, $actor, $info, $identityNumber, $verifyOnly];
|
|
return ['status' => 'signed'];
|
|
}
|
|
public static function verifyWillingness(int $id, int $actor, array $info, string $code): array
|
|
{
|
|
self::$verifications[] = [$id, $actor, $info, $code];
|
|
return ['status' => 'authenticating'];
|
|
}
|
|
}
|
|
}
|
|
namespace app\common\model {
|
|
class OperationLog
|
|
{
|
|
public array $fields = [];
|
|
public static array $saved = [];
|
|
public function __set(string $key, $value): void { $this->fields[$key] = $value; }
|
|
public function save(): bool { self::$saved[] = $this->fields; return true; }
|
|
}
|
|
}
|
|
namespace app\adminapi\controller {
|
|
class BaseAdminController
|
|
{
|
|
public $request;
|
|
public int $adminId = 7;
|
|
public array $adminInfo = ['admin_id' => 7, 'name' => '测试医师'];
|
|
public function isNotNeedLogin(): bool { return false; }
|
|
protected function data(array $data): array { return ['code' => 1, 'data' => $data]; }
|
|
protected function fail(string $message): array { return ['code' => 0, 'msg' => $message]; }
|
|
}
|
|
}
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
require dirname(__DIR__) . '/vendor/topthink/framework/src/helper.php';
|
|
$app = new think\App(); // No initialize, environment, real DB, credentials or network.
|
|
$checks = 0;
|
|
$expect = static function (bool $condition, string $label) use (&$checks): void {
|
|
if (!$condition) { throw new RuntimeException($label); }
|
|
++$checks;
|
|
};
|
|
$number = '110101199001011237';
|
|
$controller = new app\adminapi\controller\tcm\PrescriptionController();
|
|
$request = (new app\Request())->withServer(['REQUEST_METHOD' => 'POST'])->setMethod('POST')->setController('tcm.Prescription')->setAction('esignComplete')
|
|
->setUrl('/adminapi/tcm.prescription/esignComplete?identity_number=' . $number)
|
|
->withPost(['id' => 41, 'identity_number' => $number, 'extra' => ['identity_number' => $number]]);
|
|
$request->controllerObject = $controller;
|
|
$request->adminInfo = $controller->adminInfo;
|
|
$controller->request = $request;
|
|
$result = $controller->esignComplete();
|
|
$calls = app\common\service\esign\PrescriptionEsignService::$calls;
|
|
$expect($result['code'] === 1 && $calls[0] === [41, 7, $controller->adminInfo, $number, false], 'POST input forwarded only to current authenticated doctor service');
|
|
$expect(!str_contains(json_encode($result), $number), 'controller response has no supplemental input');
|
|
|
|
think\Container::getInstance()->instance('request', $request);
|
|
$response = new class($result) {
|
|
public function __construct(private array $result) {}
|
|
public function getContent(): string { return json_encode($this->result); }
|
|
};
|
|
(new app\adminapi\listener\OperationLog())->handle($response);
|
|
$audit = app\common\model\OperationLog::$saved[0];
|
|
$expect(json_decode($audit['params'], true) === ['id' => 41], 'operation audit allowlists prescription id only, including nested malicious inputs');
|
|
$expect(!str_contains(json_encode($audit), $number) && !str_contains($audit['url'], '?'), 'operation audit removes submitted identity and URL query');
|
|
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 41, 'identity_number' => [$number]]);
|
|
$expect($controller->esignComplete()['code'] === 0 && count(app\common\service\esign\PrescriptionEsignService::$calls) === 1, 'array identity input rejected without invoking completion');
|
|
$controller->request = (new app\Request())->setMethod('GET')->withGet(['id' => 41, 'identity_number' => $number]);
|
|
$expect($controller->esignComplete()['code'] === 0 && count(app\common\service\esign\PrescriptionEsignService::$calls) === 1, 'GET never performs completion');
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42]);
|
|
$expect($controller->esignComplete()['code'] === 1 && app\common\service\esign\PrescriptionEsignService::$calls[1][3] === '', 'existing complete callers keep optional input compatibility');
|
|
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'verify_only' => true]);
|
|
$expect($controller->esignComplete()['code'] === 1 && app\common\service\esign\PrescriptionEsignService::$calls[2][4] === true, 'explicit archive verification flag forwarded');
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'restart_identity' => true]);
|
|
$expect($controller->esignStart()['code'] === 1 && app\common\service\esign\PrescriptionEsignService::$starts[0][3] === true, 'explicit replacement flag forwarded');
|
|
foreach ([null, 'true', 'false', 1, [], ['true']] as $invalid) {
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'verify_only' => $invalid, 'restart_identity' => $invalid]);
|
|
$expect($controller->esignComplete()['code'] === 0 && count(app\common\service\esign\PrescriptionEsignService::$calls) === 3, 'invalid archive verification flag cannot fall through to signing');
|
|
$expect($controller->esignStart()['code'] === 0 && count(app\common\service\esign\PrescriptionEsignService::$starts) === 1, 'invalid replacement flag cannot change identity flow');
|
|
}
|
|
|
|
$code = '023941';
|
|
$mobile = '13800000000';
|
|
$authUrl = 'https://smlt.esign.cn/example/private-auth-url';
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'auth_code' => $code, 'doctor_id' => 41]);
|
|
$expect($controller->esignVerifyWillingness()['code'] === 1
|
|
&& app\common\service\esign\PrescriptionEsignService::$verifications === [[42, 7, $controller->adminInfo, $code]], 'OTP verifies only for current authenticated actor; leading zero preserved');
|
|
$controller->request = (new app\Request())->setMethod('GET')->withGet(['id' => 42, 'auth_code' => $code]);
|
|
$expect($controller->esignVerifyWillingness()['code'] === 0, 'GET cannot submit an OTP');
|
|
foreach (['esignVerifyWillingness', 'esignDoctorStart', 'esignDoctorComplete', 'esignDoctorStatus', 'detail'] as $action) {
|
|
$privacyRequest = (new app\Request())->setMethod('POST')->setController('tcm.Prescription')->setAction($action)
|
|
->setUrl('/adminapi/tcm.prescription/' . $action . '?mobile=' . $mobile);
|
|
if ($action !== 'detail') {
|
|
$privacyRequest->withPost(['id' => 42, 'mobile' => $mobile, 'identity_number' => $number, 'auth_code' => $code]);
|
|
}
|
|
$privacyRequest->controllerObject = $controller;
|
|
$privacyRequest->adminInfo = $controller->adminInfo;
|
|
think\Container::getInstance()->instance('request', $privacyRequest);
|
|
$privacyResponse = new class($authUrl) {
|
|
public function __construct(private string $url) {}
|
|
public function getContent(): string { return json_encode(['code' => 1, 'data' => ['status' => 'authenticating', 'auth_url' => $this->url]]); }
|
|
};
|
|
if ($action === 'detail') {
|
|
$privacyRequest->setUrl('/adminapi/tcm.prescription/detail');
|
|
}
|
|
(new app\adminapi\listener\OperationLog())->handle($privacyResponse);
|
|
$audit = end(app\common\model\OperationLog::$saved);
|
|
$entry = json_encode($audit);
|
|
$expect(!str_contains($entry, $mobile) && !str_contains($entry, $number) && !str_contains($entry, $code)
|
|
&& !str_contains($audit['result'], 'private-auth-url'), "$action audit omits signing secrets and authorization URL");
|
|
}
|
|
echo "eSign identity controller/privacy: $checks checks passed (isolated doubles, no DB/network)\n";
|
|
}
|