Framework: Addressed deprecations

This commit is contained in:
Dan Brown 2024-03-17 16:52:19 +00:00
parent b4b84f81a0
commit 28d6292278
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
5 changed files with 32 additions and 37 deletions

View File

@ -19,20 +19,25 @@ class MfaTotpController extends Controller
protected const SETUP_SECRET_SESSION_KEY = 'mfa-setup-totp-secret'; protected const SETUP_SECRET_SESSION_KEY = 'mfa-setup-totp-secret';
public function __construct(
protected TotpService $totp
) {
}
/** /**
* Show a view that generates and displays a TOTP QR code. * Show a view that generates and displays a TOTP QR code.
*/ */
public function generate(TotpService $totp) public function generate()
{ {
if (session()->has(static::SETUP_SECRET_SESSION_KEY)) { if (session()->has(static::SETUP_SECRET_SESSION_KEY)) {
$totpSecret = decrypt(session()->get(static::SETUP_SECRET_SESSION_KEY)); $totpSecret = decrypt(session()->get(static::SETUP_SECRET_SESSION_KEY));
} else { } else {
$totpSecret = $totp->generateSecret(); $totpSecret = $this->totp->generateSecret();
session()->put(static::SETUP_SECRET_SESSION_KEY, encrypt($totpSecret)); session()->put(static::SETUP_SECRET_SESSION_KEY, encrypt($totpSecret));
} }
$qrCodeUrl = $totp->generateUrl($totpSecret, $this->currentOrLastAttemptedUser()); $qrCodeUrl = $this->totp->generateUrl($totpSecret, $this->currentOrLastAttemptedUser());
$svg = $totp->generateQrCodeSvg($qrCodeUrl); $svg = $this->totp->generateQrCodeSvg($qrCodeUrl);
$this->setPageTitle(trans('auth.mfa_gen_totp_title')); $this->setPageTitle(trans('auth.mfa_gen_totp_title'));
@ -56,7 +61,7 @@ class MfaTotpController extends Controller
'code' => [ 'code' => [
'required', 'required',
'max:12', 'min:4', 'max:12', 'min:4',
new TotpValidationRule($totpSecret), new TotpValidationRule($totpSecret, $this->totp),
], ],
]); ]);
@ -87,7 +92,7 @@ class MfaTotpController extends Controller
'code' => [ 'code' => [
'required', 'required',
'max:12', 'min:4', 'max:12', 'min:4',
new TotpValidationRule($totpSecret), new TotpValidationRule($totpSecret, $this->totp),
], ],
]); ]);

View File

@ -2,36 +2,26 @@
namespace BookStack\Access\Mfa; namespace BookStack\Access\Mfa;
use Illuminate\Contracts\Validation\Rule; use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class TotpValidationRule implements Rule class TotpValidationRule implements ValidationRule
{ {
protected $secret;
protected $totpService;
/** /**
* Create a new rule instance. * Create a new rule instance.
* Takes the TOTP secret that must be system provided, not user provided. * Takes the TOTP secret that must be system provided, not user provided.
*/ */
public function __construct(string $secret) public function __construct(
{ protected string $secret,
$this->secret = $secret; protected TotpService $totpService,
$this->totpService = app()->make(TotpService::class); ) {
} }
/** public function validate(string $attribute, mixed $value, Closure $fail): void
* Determine if the validation rule passes.
*/
public function passes($attribute, $value)
{ {
return $this->totpService->verifyCode($value, $this->secret); $passes = $this->totpService->verifyCode($value, $this->secret);
} if (!$passes) {
$fail(trans('validation.totp'));
/** }
* Get the validation error message.
*/
public function message()
{
return trans('validation.totp');
} }
} }

View File

@ -26,9 +26,9 @@ return new class extends Migration
public function down(): void public function down(): void
{ {
$sm = Schema::getConnection()->getDoctrineSchemaManager(); $sm = Schema::getConnection()->getDoctrineSchemaManager();
$pages = $sm->listTableDetails('pages'); $pages = $sm->introspectTable('pages');
$books = $sm->listTableDetails('books'); $books = $sm->introspectTable('books');
$chapters = $sm->listTableDetails('chapters'); $chapters = $sm->introspectTable('chapters');
if ($pages->hasIndex('search')) { if ($pages->hasIndex('search')) {
Schema::table('pages', function (Blueprint $table) { Schema::table('pages', function (Blueprint $table) {

View File

@ -26,9 +26,9 @@ return new class extends Migration
public function down(): void public function down(): void
{ {
$sm = Schema::getConnection()->getDoctrineSchemaManager(); $sm = Schema::getConnection()->getDoctrineSchemaManager();
$pages = $sm->listTableDetails('pages'); $pages = $sm->introspectTable('pages');
$books = $sm->listTableDetails('books'); $books = $sm->introspectTable('books');
$chapters = $sm->listTableDetails('chapters'); $chapters = $sm->introspectTable('chapters');
if ($pages->hasIndex('name_search')) { if ($pages->hasIndex('name_search')) {
Schema::table('pages', function (Blueprint $table) { Schema::table('pages', function (Blueprint $table) {

View File

@ -25,9 +25,9 @@ return new class extends Migration
}); });
$sm = Schema::getConnection()->getDoctrineSchemaManager(); $sm = Schema::getConnection()->getDoctrineSchemaManager();
$pages = $sm->listTableDetails('pages'); $pages = $sm->introspectTable('pages');
$books = $sm->listTableDetails('books'); $books = $sm->introspectTable('books');
$chapters = $sm->listTableDetails('chapters'); $chapters = $sm->introspectTable('chapters');
if ($pages->hasIndex('search')) { if ($pages->hasIndex('search')) {
Schema::table('pages', function (Blueprint $table) { Schema::table('pages', function (Blueprint $table) {