Un générateur de comptoir (RFC 4226) et basé sur le temps (RFC 6238) UNE TIME Mots de passe (OTP). (AKA encore une autre implémentation Google Authenticator!)
ext-curl pour la synchronisation du temps du serveur Steam Guardext-sodium pour les implémentations à temps constant de la base64 Encode / Decode et hex2bin / bin2hex ( paragonie/constant_time_encoding est utilisé comme secours) nécessite un compositeur
via le terminal: composer require chillerlan/php-authenticator
composer.json
{
"require" : {
"php" : " ^8.2 " ,
"chillerlan/php-authenticator" : " dev-main "
}
} Remarque: Remplacez dev-main par une contrainte de version, par exemple ^5.0 - Voir les versions pour les versions valides
Profit!
Le secret est généralement créé une fois pendant le processus d'activation dans un panneau de configuration d'utilisateur. Ainsi, tout ce que vous avez à faire est pour l'afficher à l'utilisateur de manière pratique - en tant que chaîne de texte et code QR par exemple - et enregistrez-la quelque part avec les données de l'utilisateur.
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 ); Un secret créé avec Authenticator::createSecret() sera également stocké en interne, afin que vous n'ayez pas besoin de fournir le secret que vous venez de créer sur les opérations de suivi avec l'instance actuelle.
Maintenant, pendant le processus de connexion - après que l'utilisateur a réussi ses informations d'identification - vous leur demanderiez un code unique pour le vérifier par rapport au secret de votre base de données utilisateur.
// verify the code
if ( $ authenticator -> verify ( $ otp )){
// that's it - 2FA has never been easier! :D
}Vérifiez les codes adjacents
// 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 Afin d'afficher un code QR pour un authentificateur mobile, vous aurez besoin d'un otpauth:// uri, qui peut être créé en utilisant la méthode suivante.
$label devrait être quelque chose qui identifie le compte auquel appartient le secret$issuer est le nom de votre site Web ou de votre entreprise par exemple, afin que l'utilisateur puisse identifier plusieurs comptes. $ uri = $ authenticator -> getUri ( $ label , $ issuer );
// -> otpauth://totp/my%20label?secret=NKSOQG7UKKID4IXW&issuer=chillerlan.net&digits=6&period=30&algorithm=SHA1 Gardez à l'esprit que plusieurs paramètres URI ne sont pas (encore) reconnus par tous les authentificateurs. Consultez le wiki Google Authenticator pour plus d'informations.
// 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| méthode | retour | description |
|---|---|---|
__construct(SettingsContainerInterface $options = null, string $secret = null) | - | |
setOptions(SettingsContainerInterface $options) | Authenticator | appelé en interne par __construct() |
setSecret(string $secret) | Authenticator | appelé en interne par __construct() |
getSecret() | string | |
createSecret(int $length = null) | string | $length remplace le paramètre AuthenticatorOptions |
code(int $data = null) | string | $data peuvent être un horodat UNIX (TOTP) ou une valeur de comptoir (HOTP) |
verify(string $otp, int $data = null) | bool | Pour $data , voir Authenticator::code() |
getUri(string $label, string $issuer, int $hotpCounter = null, bool $omitSettings = null) | string |
AuthenticatorOptions | propriété | taper | défaut | autorisé | description |
|---|---|---|---|---|
$digits | int | 6 | 6 ou 8 | Longueur du code d'automne |
$period | int | 30 | 15 - 60 | Période de validation (secondes) |
$secret_length | int | 20 | > = 16 | longueur de la phrase secrète (octets, binaire non codé) |
$algorithm | string | SHA1 | SHA1 , SHA256 ou SHA512 | Algorithme de hachage HMAC, voir AuthenticatorInterface::HASH_ALGOS |
$mode | string | totp | totp , hotp , battlenet ou steam | Mode d'authentificateur: basé sur le temps ou le compteur, voir AuthenticatorInterface::MODES |
$adjacent | int | 1 | > = 0 | Nombre de codes adjacents autorisés |
$time_offset | int | 0 | * | décalage de temps fixe qui sera ajouté à la valeur d'heure actuelle |
$useLocalTime | bool | vrai | * | Que ce soit pour utiliser l'heure locale ou demander l'heure du serveur |
$forceTimeRefresh | bool | FAUX | * | s'il faut forcer le temps de serveur rafraîchissant sur chaque appel |
AuthenticatorInterface | méthode | retour | description |
|---|---|---|
setOptions(SettingsContainerInterface $options) | AuthenticatorInterface | |
setSecret(string $encodedSecret) | AuthenticatorInterface | |
getSecret() | string | |
createSecret(int $length = null) | string | |
getServertime() | int | |
getCounter(int $data = null) | int | interne |
getHMAC(int $counter) | string | interne |
getCode(string $hmac) | int | interne |
getOTP(int $code) | string | interne |
code(int $data = null) | string | |
verify(string $otp, int $data = null) | bool |
| constante | taper | description |
|---|---|---|
TOTP | string | |
HOTP | string | |
STEAM_GUARD | string | |
ALGO_SHA1 | string | |
ALGO_SHA256 | string | |
ALGO_SHA512 | string | |
MODES | array | Carte du mode -> ClassName |
HASH_ALGOS | array | Liste des algorithmes de hachage disponibles |