phant/otp

轻松管理OTP

1.0 2023-09-28 12:52 UTC

This package is auto-updated.

Last update: 2024-09-18 16:56:54 UTC


README

演示

OTP服务旨在管理OTP(一次性密码),可用于用户身份验证、确认或其他验证要求。

过程

  1. 服务生成OTP并发送给用户
  2. 用户接收OTP
  3. 应用程序从用户那里检索OTP
  4. 应用程序与该服务验证OTP

OTP通过您的OtpSender服务(电子邮件、短信等)发送给用户。

使用的技术

  • PHP 8.2
  • Composer 用于依赖管理(PHP)

安装

composer install

用法

配置

use Phant\DataStructure\Key\Ssl as SslKey;
use Phant\Otp\Service\Request as Service;
use App\OtpRepository;
use App\OtpSender;

// Config

$otpRepository = new OtpRepository();
$otpSender = new OtpSender();
$sslKey = new SslKey($privateKey, $publicKey);


// Build service

$service = new Service(
	$otpRepository,
	$otpSender,
	$sslKey
);

请求OTP

// OTP context transmitted to sender
$payload = [
	'email' => 'willy@wonka.com',
];

$requestToken = $service->generate(
	$payload
);

验证OTP

use Phant\Error\NotCompliant;

// Request token obtained previously
$requestToken = '...';

// Obtain Otp from user
$otp = '123456';

try {
	$payload = $service->verify(
		$requestToken,
		$otp
	);
} catch (NotCompliant $e) {
	$numberOfAttemptsRemaining = $otpService->getNumberOfRemainingAttempts(
		$requestToken
	);
}