0

0

告别身份验证难题:如何使用Smart-IDPHPClient实现安全便捷的身份验证

王林

王林

发布时间:2025-09-01 12:52:19

|

410人浏览过

|

来源于php中文网

原创

在构建需要安全身份验证的 Web 应用程序时,我遇到了一个常见的问题:如何集成一个既安全又用户友好的身份验证解决方案。传统的用户名/密码方式已经无法满足日益增长的安全需求,而复杂的双因素认证方案又可能降低用户体验。

这时,我发现了 smart-id,一种基于移动设备的数字身份验证解决方案。smart-id 允许用户使用他们的移动设备进行身份验证,无需记住复杂的密码或携带额外的硬件设备。然而,如何将 smart-id 集成到我的 php 应用程序中,成为了一个新的挑战。

经过一番研究,我找到了

sk-id-solutions/smart-id-php-client
这个 Composer 包,它提供了一个简洁的 PHP 接口,可以轻松地与 Smart-ID 服务进行交互。

安装

首先,你需要使用 Composer 安装该库:

composer require sk-id-solutions/smart-id-php-client "2.3.2"

配置客户端

立即学习PHP免费学习笔记(深入)”;

安装完成后,你需要配置客户端的详细信息,包括 Relying Party UUID、Relying Party Name 和 Smart-ID 服务的 URL。更重要的是,为了防止中间人攻击,你需要设置 HTTPS pinning,通过指定信任的 SSL 证书的公钥哈希来确保与 Smart-ID 服务的安全连接。

use SK\SmartId\Client\Client;

$this->client = new Client();
$this->client
    ->setRelyingPartyUUID( '00000000-0000-0000-0000-000000000000' ) // In production replace with your UUID
    ->setRelyingPartyName( 'DEMO' ) // In production replace with your name
    ->setHostUrl( 'https://sid.demo.sk.ee/smart-id-rp/v2/' ) // In production replace with production service URL
        // in production replace with correct server SSL key
    ->setPublicSslKeys("sha256//Ps1Im3KeB0Q4AlR+/J9KFd/MOznaARdwo4gURPCLaVA=");

使用语义标识符进行身份验证

接下来,你可以使用语义标识符(例如个人身份号码)来启动身份验证流程。这个过程包括生成一个随机的身份验证哈希,并将其呈现给用户,以便他们在 Smart-ID 应用程序中进行验证。

Toolplay
Toolplay

一站式AI应用聚合生成平台

下载
use SK\SmartId\Client\Api\Authentication\AuthenticationRequestBuilder;
use SK\SmartId\Client\Api\Authentication\AuthenticationResponseValidator;
use SK\SmartId\Client\Api\Authentication\NationalIdentity;
use SK\SmartId\Client\Api\Authentication\SemanticsIdentifier;
use SK\SmartId\Client\Api\SessionStatus\SessionStatusResponse;
use SK\SmartId\Client\Exception\UserRefusedException;
use SK\SmartId\Client\Exception\UserSelectedWrongVerificationCodeException;
use SK\SmartId\Client\Exception\SessionTimeoutException;
use SK\SmartId\Client\Exception\UserAccountNotFoundException;
use SK\SmartId\Client\Exception\UserAccountException;
use SK\SmartId\Client\Exception\EnduringSmartIdException;
use SK\SmartId\Client\Exception\SmartIdException;
use SK\SmartId\Client\Util\AuthenticationHash;
use SK\SmartId\Client\Api\Authentication\CertificateLevelCode;
use SK\SmartId\Client\Api\Authentication\Interaction;

$semanticsIdentifier = SemanticsIdentifier::builder()
    ->withSemanticsIdentifierType('PNO')
    ->withCountryCode('LT')
    ->withIdentifier('30303039914')
    ->build();

// For security reasons a new hash value must be created for each new authentication request
$authenticationHash = AuthenticationHash::generate();

$verificationCode = $authenticationHash->calculateVerificationCode();

// display verification code to the user
echo "Verification code: " . $verificationCode . "\n";

$authenticationResponse = null;
try
{
  $authenticationResponse = $this->client->authentication()
      ->createAuthentication()
      ->withSemanticsIdentifier( $semanticsIdentifier )
      ->withAuthenticationHash( $authenticationHash )
      ->withCertificateLevel( CertificateLevelCode::QUALIFIED ) // Certificate level can either be "QUALIFIED" or "ADVANCED"
      ->withAllowedInteractionsOrder((array(
          Interaction::ofTypeVerificationCodeChoice("Enter awesome portal?"),
          Interaction::ofTypeDisplayTextAndPIN("Enter awesome portal?"))))
      ->authenticate(); // this blocks until user has responded
}
catch (UserRefusedException $e) {
  throw new RuntimeException("You pressed cancel in Smart-ID app.");
}
catch (UserSelectedWrongVerificationCodeException $e) {
  throw new RuntimeException("You selected wrong verification code in Smart-ID app. Please try again. ");
}
catch (SessionTimeoutException $e) {
  throw new RuntimeException("Session timed out (you didn't enter PIN1 in Smart-ID app).");
}
catch (UserAccountNotFoundException $e) {
  throw new RuntimeException("User does not have a Smart-ID account");
}
catch (UserAccountException $e) {
  throw new RuntimeException("Unable to authenticate due to a problem with your Smart-ID account.");
}
catch (EnduringSmartIdException $e) {
  throw new RuntimeException("Problem with connecting to Smart-ID service. Please try again later.");
}
catch (SmartIdException $e) {
  throw new RuntimeException("Smart-ID authentication process failed for uncertain reason: ". $e);
}

// create a folder with name "trusted_certificates" and set path to that folder here:
$pathToFolderWithTrustedCertificates = __DIR__ . '/../../../resources';

$authenticationResponseValidator = new AuthenticationResponseValidator($pathToFolderWithTrustedCertificates);
$authenticationResult = $authenticationResponseValidator->validate( $authenticationResponse );


if ($authenticationResult->isValid()) {
  echo "Hooray! Authentication result is valid";
}
else {
   throw new RuntimeException("Error! Response is not valid! Error(s): ". implode(",", $authenticationResult->getErrors()));
}



$authenticationIdentity = $authenticationResult->getAuthenticationIdentity();

echo "hello name: " . $authenticationIdentity->getGivenName() . ' ' . $authenticationIdentity->getSurName() . "\n";
echo "from " . $authenticationIdentity->getCountry() . "\n";
echo "born " . $authenticationIdentity->getDateOfBirth()->format("D d F o") . "\n";

// you might need this if you want to start authentication with document number
echo "Authenticated user documentNumber is: ".$authenticationResponse->getDocumentNumber(). "\n";

处理异常

在身份验证过程中,可能会出现各种异常情况,例如用户取消操作、选择错误的验证码、会话超时等等。该库提供了专门的异常类来处理这些情况,你可以使用 try-catch 块来捕获这些异常并采取适当的措施。

验证身份验证结果

为了确保身份验证结果的真实性,你需要验证 Smart-ID 服务的签名。这需要你配置一个包含受信任证书的目录,并使用

AuthenticationResponseValidator
类来验证签名。

通过使用

sk-id-solutions/smart-id-php-client
库,我能够轻松地将 Smart-ID 集成到我的 PHP 应用程序中,并提供了一个安全、便捷的身份验证体验。该库的优点包括:

  • 简单易用: 提供了简洁的 API,可以轻松地与 Smart-ID 服务进行交互。
  • 安全性高: 支持 HTTPS pinning,可以防止中间人攻击。
  • 异常处理: 提供了专门的异常类来处理各种身份验证错误。
  • 结果验证: 提供了验证身份验证结果真实性的机制。

sk-id-solutions/smart-id-php-client
库极大地简化了 Smart-ID 集成过程,使我能够专注于构建应用程序的核心功能,而无需担心复杂的身份验证细节。如果你正在寻找一种安全、便捷的身份验证解决方案,那么 Smart-ID 和
sk-id-solutions/smart-id-php-client
库绝对值得考虑。

Composer在线学习地址:学习地址

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
composer是什么插件
composer是什么插件

Composer是一个PHP的依赖管理工具,它可以帮助开发者在PHP项目中管理和安装依赖的库文件。Composer通过一个中央化的存储库来管理所有的依赖库文件,这个存储库包含了各种可用的依赖库的信息和版本信息。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

154

2023.12.25

mysql标识符无效错误怎么解决
mysql标识符无效错误怎么解决

mysql标识符无效错误的解决办法:1、检查标识符是否被其他表或数据库使用;2、检查标识符是否包含特殊字符;3、使用引号包裹标识符;4、使用反引号包裹标识符;5、检查MySQL的配置文件等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

183

2023.12.04

Python标识符有哪些
Python标识符有哪些

Python标识符有变量标识符、函数标识符、类标识符、模块标识符、下划线开头的标识符、双下划线开头、双下划线结尾的标识符、整型标识符、浮点型标识符等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

287

2024.02.23

java标识符合集
java标识符合集

本专题整合了java标识符相关内容,想了解更多详细内容,请阅读下面的文章。

258

2025.06.11

c++标识符介绍
c++标识符介绍

本专题整合了c++标识符相关内容,阅读专题下面的文章了解更多详细内容。

124

2025.08.07

硬盘接口类型介绍
硬盘接口类型介绍

硬盘接口类型有IDE、SATA、SCSI、Fibre Channel、USB、eSATA、mSATA、PCIe等等。详细介绍:1、IDE接口是一种并行接口,主要用于连接硬盘和光驱等设备,它主要有两种类型:ATA和ATAPI,IDE接口已经逐渐被SATA接口;2、SATA接口是一种串行接口,相较于IDE接口,它具有更高的传输速度、更低的功耗和更小的体积;3、SCSI接口等等。

1127

2023.10.19

PHP接口编写教程
PHP接口编写教程

本专题整合了PHP接口编写教程,阅读专题下面的文章了解更多详细内容。

193

2025.10.17

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1667

2025.12.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

391

2026.01.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
第二十四期_PHP8编程
第二十四期_PHP8编程

共86课时 | 3.4万人学习

成为PHP架构师-自制PHP框架
成为PHP架构师-自制PHP框架

共28课时 | 2.5万人学习

第二十三期_PHP编程
第二十三期_PHP编程

共93课时 | 6.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号