php authenticator
5.2.1
카운터 기반 (RFC 4226) 및 시간 기반 (RFC 6238) 일회성 암호 (OTP)를위한 생성기. (일명 또 다른 Google 인증 자 구현!)
ext-curlext-sodium ( paragonie/constant_time_encoding 은 폴백으로 사용됩니다) 작곡가가 필요합니다
터미널을 통해 : composer require chillerlan/php-authenticator
composer.json
{
"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 해시 알고리즘, AuthenticatorInterface::HASH_ALGOS 참조하십시오 |
$mode | string | totp | totp , hotp , battlenet 또는 steam | Authenticator 모드 : 시간 또는 카운터 기반, 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 | 사용 가능한 해시 알고리즘 목록 |