php authenticator
5.2.1
基于计数器的发电机(RFC 4226)和基于时间(RFC 6238)的一个时间密码(OTP)。 (又名另一个Google Authenticator实现!)
ext-curlext-sodium ( paragonie/constant_time_encoding用作后备) 需要作曲家
通过终端: composer require chillerlan/php-authenticator
作曲家
{
"require" : {
"php" : " ^8.2 " ,
"chillerlan/php-authenticator" : " dev-main "
}
}注意:用版本约束替换dev-main ,例如^5.0有关有效版本,请参见版本
利润!
通常在用户控制面板的激活过程中创建一次秘密。因此,您需要做的就是以方便的方式将其显示给用户 - 例如文本字符串和QR码 - 并用用户数据将其保存在某个地方。
use chillerlan Authenticator { Authenticator , AuthenticatorOptions };
$ options = new AuthenticatorOptions ;
$ options -> secret_length = 32 ;
$ authenticator = new Authenticator ( $ options );
// create a secret (stored somewhere in a *safe* place on the server. safe... hahaha jk)
$ secret = $ authenticator -> createSecret ();
// you can also specify the length of the secret key, which overrides the options setting
$ secret = $ authenticator -> createSecret ( 20 );
// set an existing secret
$ authenticator -> setSecret ( $ secret );使用Authenticator::createSecret()创建的秘密也将在内部存储,因此您无需提供刚刚在后续操作中创建的秘密。
现在,在登录过程中 - 用户成功输入其凭据之后 - 您会要求他们使用一个时间代码,以对其用户数据库的秘密进行检查。
// verify the code
if ( $ authenticator -> verify ( $ otp )){
// that's it - 2FA has never been easier! :D
}验证相邻的代码
// try the first adjacent
$ authenticator -> verify ( $ otp , time () - $ options -> period ); // -> true
// try the second adjacent, default is 1
$ authenticator -> verify ( $ otp , time () + 2 * $ options -> period ); // -> false
// allow 2 adjacent codes
$ options -> adjacent = 2 ;
$ authenticator -> verify ( $ otp , time () + 2 * $ options -> period ); // -> true // switch mode to HOTP
$ options -> mode = AuthenticatorInterface:: HOTP ;
// user sends the OTP for code #42, which is equivalent to
$ otp = $ authenticator -> code ( 42 ); // -> 123456
// verify [123456, 42]
$ authenticator -> verify ( $ otp , $ counterValueFromUserDatabase ) // -> true为了显示移动身份验证器的QR码,您需要一个otpauth:// uri,可以使用以下方法创建。
$label应该是识别秘密所属帐户的东西$issuer是您的网站或公司的名称,因此用户能够标识多个帐户。 $ uri = $ authenticator -> getUri ( $ label , $ issuer );
// -> otpauth://totp/my%20label?secret=NKSOQG7UKKID4IXW&issuer=chillerlan.net&digits=6&period=30&algorithm=SHA1 请记住,所有身份验证者尚未(尚未)识别几种URI设置。检查Google Authenticator Wiki以获取更多信息。
// code length, currently 6 or 8
$ options -> digits = 8 ;
// valid period between 15 and 60 seconds
$ options -> period = 45 ;
// set the HMAC hash algorithm
$ options -> algorithm = AuthenticatorInterface:: ALGO_SHA512 ;Authenticator| 方法 | 返回 | 描述 |
|---|---|---|
__construct(SettingsContainerInterface $options = null, string $secret = null) | - | |
setOptions(SettingsContainerInterface $options) | Authenticator | 由__construct()内部称为 |
setSecret(string $secret) | Authenticator | 由__construct()内部称为 |
getSecret() | string | |
createSecret(int $length = null) | string | $length覆盖AuthenticatorOptions设置 |
code(int $data = null) | string | $data可能是UNIX时间戳(TOTP)或计数器值(HOTP) |
verify(string $otp, int $data = null) | bool | 对于$data请参见Authenticator::code() |
getUri(string $label, string $issuer, int $hotpCounter = null, bool $omitSettings = null) | string |
AuthenticatorOptions | 财产 | 类型 | 默认 | 允许 | 描述 |
|---|---|---|---|---|
$digits | int | 6 | 6或8 | 身份代码长度 |
$period | int | 30 | 15-60 | 验证期(秒) |
$secret_length | int | 20 | > = 16 | 秘密短语的长度(字节,未编码的二进制) |
$algorithm | string | SHA1 | SHA1 , SHA256或SHA512 | HMAC HASH算法,请参见AuthenticatorInterface::HASH_ALGOS |
$mode | string | totp | totp , hotp , battlenet或steam | 身份验证器模式:基于时间或计数器,请参见AuthenticatorInterface::MODES |
$adjacent | int | 1 | > = 0 | 允许的相邻代码数量 |
$time_offset | int | 0 | * | 固定时间偏移将添加到当前时间值 |
$useLocalTime | bool | 真的 | * | 是使用本地时间还是请求服务器时间 |
$forceTimeRefresh | bool | 错误的 | * | 是否在每个呼叫上强制刷新服务器时间 |
AuthenticatorInterface | 方法 | 返回 | 描述 |
|---|---|---|
setOptions(SettingsContainerInterface $options) | AuthenticatorInterface | |
setSecret(string $encodedSecret) | AuthenticatorInterface | |
getSecret() | string | |
createSecret(int $length = null) | string | |
getServertime() | int | |
getCounter(int $data = null) | int | 内部的 |
getHMAC(int $counter) | string | 内部的 |
getCode(string $hmac) | int | 内部的 |
getOTP(int $code) | string | 内部的 |
code(int $data = null) | string | |
verify(string $otp, int $data = null) | bool |
| 持续的 | 类型 | 描述 |
|---|---|---|
TOTP | string | |
HOTP | string | |
STEAM_GUARD | string | |
ALGO_SHA1 | string | |
ALGO_SHA256 | string | |
ALGO_SHA512 | string | |
MODES | array | 模式图 - > className |
HASH_ALGOS | array | 可用哈希算法列表 |