1
0
mirror of https://github.com/delight-im/PHP-Auth.git synced 2025-07-31 13:20:11 +02:00

Implement 'prepareTwoFactorViaSms' using 'prepareTwoFactor'

This commit is contained in:
Marco
2024-04-04 19:39:54 +02:00
parent 0ff92ce870
commit ea7b1208ad
2 changed files with 39 additions and 0 deletions

View File

@@ -1766,6 +1766,34 @@ final class Auth extends UserManager {
return $this->prepareTwoFactor(self::TWO_FACTOR_MECHANISM_TOTP, $serviceName, null);
}
/**
* Prepares the setup of two-factor authentification with one-time passwords sent via SMS
*
* After performing this step, a one-time password will have to be delivered to the user via SMS and then be requested from the user for verification
*
* When the user has entered the one-time password from the text message afterwards, call {@see enableTwoFactorViaSms} with that one-time password
*
* @param string $phoneNumber the phone number to send the one-time passwords to
* @return string[] an array with the phone number at index zero and the one-time password to be sent (but not otherwise displayed to the user) at index one
* @throws InvalidPhoneNumberException if no valid phone number has been provided
* @throws TwoFactorMechanismAlreadyEnabledException if this method of two-factor authentification has already been enabled
* @throws NotLoggedInException if the user is not currently signed in
* @throws TooManyRequestsException if the number of allowed attempts/requests has been exceeded
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
public function prepareTwoFactorViaSms($phoneNumber) {
$phoneNumber = !empty($phoneNumber) ? \trim((string) $phoneNumber) : '';
if (\strlen($phoneNumber) < 3) {
throw new InvalidPhoneNumberException();
}
$this->prepareTwoFactor(self::TWO_FACTOR_MECHANISM_SMS, null, $phoneNumber);
$otpValue = $this->generateAndStoreRandomOneTimePassword($this->getUserId(), self::TWO_FACTOR_MECHANISM_SMS);
return [ $phoneNumber, $otpValue ];
}
/**
* Prepares the setup of two-factor authentification via a specified mechanism
*

View File

@@ -0,0 +1,11 @@
<?php
/*
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
namespace Delight\Auth;
class InvalidPhoneNumberException extends AuthException {}