php authenticator
5.2.1
カウンターベース(RFC 4226)およびタイムベース(RFC 6238)の1回のパスワード(OTP)用のジェネレーター。 (別名、さらに別のGoogle Authenticatorの実装!)
ext-curlparagonie/constant_time_encodingの一定の時間実装のためのext-sodium ) 作曲家が必要です
ターミナル経由: composer require chillerlan/php-authenticator
Composer.json
{
"require" : {
"php" : " ^8.2 " ,
"chillerlan/php-authenticator" : " dev-main "
}
}注: dev-mainバージョンの制約に置き換えます。 ^5.0 、有効なバージョンのリリースを参照してください
利益!
秘密は通常、ユーザーコントロールパネルのアクティベーションプロセス中に1回作成されます。そのため、たとえば、テキスト文字列や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 ALGORITHM、 AuthenticatorInterface::HASH_ALGOSを参照してください |
$mode | string | totp | totp 、 hotp 、 battlenet 、またはsteam | Authenticator Mode:時間ベースまたはカウンターベース、 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 | 利用可能なハッシュアルゴリズムのリスト |