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 | 可用哈希算法列表 |