เครื่องกำเนิดไฟฟ้าสำหรับตัวนับ (RFC 4226) และตามเวลา (RFC 6238) รหัสผ่านหนึ่งครั้ง (OTP) (aka เป็นอีกหนึ่งการใช้งาน Google Authenticator!)
ext-curl สำหรับการซิงโครไนซ์เซิร์ฟเวอร์เซิร์ฟเวอร์ Steam Guardext-sodium สำหรับการใช้งานเวลาคงที่ของ BASE64 ENCODE/DECODE และ HEX2BIN/BIN2HEX ( 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 | การตั้งค่า AuthenticatorOptions $length |
code(int $data = null) | string | $data อาจเป็น UNIX Timestamp (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 | รายการอัลกอริทึมแฮชที่มีอยู่ |