在开发需要高安全性的 Web 应用时,我们经常需要集成可靠的身份验证机制。Mobile-ID 是一种流行的移动身份验证方案,它允许用户使用手机 SIM 卡进行身份验证和数字签名。然而,直接与 Mobile-ID 的 API 进行交互可能会非常复杂,需要处理各种底层细节,例如生成哈希、处理会话状态以及验证签名等。
为了解决这个问题,我发现了
sk-id-solutions/mobile-id-php-client
使用 Composer 安装
sk-id-solutions/mobile-id-php-client
<pre class="brush:php;toolbar:false;">composer require sk-id-solutions/mobile-id-php-client "<VERSION>"
安装完成后,就可以开始使用该库进行 Mobile-ID 身份验证了。以下是一个简化的身份验证流程示例:
<pre class="brush:php;toolbar:false;">require_once __DIR__ . '/vendor/autoload.php';
use Sk\Mid\MobileIdClient;
use Sk\Mid\AuthenticationRequestBuilder;
use Sk\Mid\DisplayTextFormat;
use Sk\Mid\Language\ENG;
use Sk\Mid\Exception\MidException;
use Sk\Mid\Util\MidInputUtil;
use Sk\Mid\Exception\MidInvalidPhoneNumberException;
use Sk\Mid\Exception\MidInvalidNationalIdentityNumberException;
try {
// 1. 验证用户输入
$phoneNumber = MidInputUtil::getValidatedPhoneNumber($_POST['phoneNumber']);
$nationalIdentityNumber = MidInputUtil::getValidatedNationalIdentityNumber($_POST['nationalIdentityNumber']);
// 2. 创建 MobileIdClient 实例
$client = MobileIdClient::newBuilder()
->withRelyingPartyUUID($config['relyingPartyUUID'])
->withRelyingPartyName($config['relyingPartyName'])
->withHostUrl($config['hostUrl'])
->withLongPollingTimeoutSeconds(60)
->withSslPinnedPublicKeys("sha256//k/w7/9MIvdN6O/rE1ON+HjbGx9PRh/zSnNJ61pldpCs=;sha256//some-future-ssl-host-key")
->build();
// 3. 创建身份验证请求
$authenticationHash = MobileIdAuthenticationHashToSign::generateRandomHashOfDefaultType();
$verificationCode = $authenticationHash->calculateVerificationCode();
$request = AuthenticationRequest::newBuilder()
->withPhoneNumber($phoneNumber)
->withNationalIdentityNumber($nationalIdentityNumber)
->withHashToSign($authenticationHash)
->withLanguage(ENG::asType())
->withDisplayText("Log into self-service?")
->withDisplayTextFormat(DisplayTextFormat::GSM7)
->build();
// 4. 发起身份验证请求
$response = $client->getMobileIdConnector()->initAuthentication($request);
// 5. 轮询会话状态,直到身份验证完成
$finalSessionStatus = $client->getSessionStatusPoller()->fetchFinalSessionStatus($response->getSessionID());
// 6. 获取身份验证结果
$authenticationResult = $client->createMobileIdAuthentication($finalSessionStatus, $authenticationHash);
// 7. 验证身份验证结果
$validator = AuthenticationResponseValidator::newBuilder()
->withTrustedCaCertificatesFolder(__DIR__ . "/test_numbers_ca_certificates/")
->build();
$validator->validate($authenticationResult);
// 8. 获取用户信息
$authenticatedPerson = $authenticationResult->constructAuthenticationIdentity();
echo 'Welcome, ' . $authenticatedPerson->getGivenName() . ' ' . $authenticatedPerson->getSurName() . '!';
} catch (MidInvalidPhoneNumberException $e) {
echo 'The phone number you entered is invalid';
} catch (MidInvalidNationalIdentityNumberException $e) {
echo 'The national identity number you entered is invalid';
} catch (MidException $e) {
echo 'Mobile-ID authentication failed: ' . $e->getMessage();
}通过使用
sk-id-solutions/mobile-id-php-client
优势总结:
立即学习“PHP免费学习笔记(深入)”;
sk-id-solutions/mobile-id-php-client
以上就是PHP集成Mobile-ID身份验证:sk-id-solutions/mobile-id-php-client如何解决身份验证难题的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号