From 4eef5cdbf5ce229c01b15690ac2a44be468e9cdb Mon Sep 17 00:00:00 2001 From: Italo Date: Tue, 5 Mar 2024 04:07:41 +0000 Subject: [PATCH] Apply fixes from StyleCI [ci skip] [skip ci] --- config/two-factor.php | 20 +++---- lang/en/messages.php | 16 +++--- src/Models/Concerns/HandlesRecoveryCodes.php | 6 +-- .../Concerns/SerializesSharedSecret.php | 8 +-- src/Models/TwoFactorAuthentication.php | 15 +++--- src/TwoFactorAuthentication.php | 4 +- tests/CreatesTwoFactorUser.php | 4 +- .../Eloquent/TwoFactorAuthenticationTest.php | 54 +++++++++---------- tests/Events/EventsTest.php | 2 +- .../ConfirmTwoFactorEnabledTest.php | 4 +- .../RequireTwoFactorEnabledTest.php | 4 +- tests/TwoFactorAuthenticationTest.php | 8 +-- 12 files changed, 73 insertions(+), 72 deletions(-) diff --git a/config/two-factor.php b/config/two-factor.php index f8c923f..748d088 100644 --- a/config/two-factor.php +++ b/config/two-factor.php @@ -13,7 +13,7 @@ */ 'cache' => [ - 'store' => null, + 'store' => null, 'prefix' => '2fa.code', ], @@ -30,8 +30,8 @@ 'recovery' => [ 'enabled' => true, - 'codes' => 10, - 'length' => 8, + 'codes' => 10, + 'length' => 8, ], /* @@ -46,9 +46,9 @@ */ 'safe_devices' => [ - 'enabled' => false, - 'cookie' => '_2fa_remember', - 'max_devices' => 3, + 'enabled' => false, + 'cookie' => '_2fa_remember', + 'max_devices' => 3, 'expiration_days' => 14, ], @@ -118,9 +118,9 @@ 'issuer' => env('OTP_TOTP_ISSUER'), 'totp' => [ - 'digits' => 6, - 'seconds' => 30, - 'window' => 1, + 'digits' => 6, + 'seconds' => 30, + 'window' => 1, 'algorithm' => 'sha1', ], @@ -136,7 +136,7 @@ */ 'qr_code' => [ - 'size' => 400, + 'size' => 400, 'margin' => 4, ], ]; diff --git a/lang/en/messages.php b/lang/en/messages.php index 5b93eea..2fdffa1 100644 --- a/lang/en/messages.php +++ b/lang/en/messages.php @@ -1,26 +1,26 @@ 'Two-Factor Authentication', + 'title' => 'Two-Factor Authentication', 'required' => 'Two-Factor Authentication is required.', - 'back' => 'Go back', + 'back' => 'Go back', 'continue' => 'To continue, open up your Authenticator app and issue your 2FA code.', - 'enable' => 'You need to enable Two-Factor Authentication.', + 'enable' => 'You need to enable Two-Factor Authentication.', 'success' => 'The 2FA code has been validated successfully.', 'fail_confirm' => 'The code to activate Two-Factor Authentication is invalid.', - 'enabled' => 'Two-Factor Authentication has been enabled for your account.', - 'disabled' => 'Two-Factor Authentication has been disabled for your account.', + 'enabled' => 'Two-Factor Authentication has been enabled for your account.', + 'disabled' => 'Two-Factor Authentication has been disabled for your account.', 'safe_device' => 'We won\'t ask you for Two-Factor Authentication codes in this device for some time.', - 'confirm' => 'Confirm code', + 'confirm' => 'Confirm code', 'switch_on' => 'Go to enable Two-Factor Authentication.', 'recovery_code' => [ - 'used' => 'You have used a Recovery Code. Remember to regenerate them if you have used almost all.', - 'depleted' => 'You have used all your Recovery Codes. Please use alternate authentication methods to continue.', + 'used' => 'You have used a Recovery Code. Remember to regenerate them if you have used almost all.', + 'depleted' => 'You have used all your Recovery Codes. Please use alternate authentication methods to continue.', 'generated' => 'You have generated a new set of Recovery Codes. Any previous set of codes have been invalidated.', ], ]; diff --git a/src/Models/Concerns/HandlesRecoveryCodes.php b/src/Models/Concerns/HandlesRecoveryCodes.php index f3cb7e2..57aecb5 100644 --- a/src/Models/Concerns/HandlesRecoveryCodes.php +++ b/src/Models/Concerns/HandlesRecoveryCodes.php @@ -37,7 +37,7 @@ public function containsUnusedRecoveryCodes(): bool protected function getUnusedRecoveryCodeIndex(string $code): int|null|bool { return $this->recovery_codes?->search([ - 'code' => $code, + 'code' => $code, 'used_at' => null, ], true); } @@ -57,7 +57,7 @@ public function setRecoveryCodeAsUsed(string $code): bool } $this->recovery_codes = $this->recovery_codes->put($index, [ - 'code' => $code, + 'code' => $code, 'used_at' => now(), ]); @@ -90,7 +90,7 @@ public static function generateRecoveryCodes(int $amount, int $length): Collecti return Collection::times($amount, static function (int $iteration) use ($generator, $amount, $length): array { return [ - 'code' => $generator($length, $iteration, $amount), + 'code' => $generator($length, $iteration, $amount), 'used_at' => null, ]; }); diff --git a/src/Models/Concerns/SerializesSharedSecret.php b/src/Models/Concerns/SerializesSharedSecret.php index efd603c..631fcb3 100644 --- a/src/Models/Concerns/SerializesSharedSecret.php +++ b/src/Models/Concerns/SerializesSharedSecret.php @@ -29,11 +29,11 @@ public function toUri(): string ?: config('app.name') ?: throw new InvalidArgumentException('The TOTP issuer cannot be empty.'); $query = http_build_query([ - 'issuer' => $issuer, - 'label' => $this->attributes['label'], - 'secret' => $this->shared_secret, + 'issuer' => $issuer, + 'label' => $this->attributes['label'], + 'secret' => $this->shared_secret, 'algorithm' => strtoupper($this->attributes['algorithm']), - 'digits' => $this->attributes['digits'], + 'digits' => $this->attributes['digits'], ], '', '&', PHP_QUERY_RFC3986); return 'otpauth://totp/'.rawurlencode($issuer).'%3A'.$this->attributes['label']."?$query"; diff --git a/src/Models/TwoFactorAuthentication.php b/src/Models/TwoFactorAuthentication.php index 0ea26b6..f22531e 100644 --- a/src/Models/TwoFactorAuthentication.php +++ b/src/Models/TwoFactorAuthentication.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\MorphTo; use Laragear\TwoFactor\Contracts\TwoFactorTotp; use ParagonIE\ConstantTime\Base32; + use function array_merge; use function config; use function json_encode; @@ -50,13 +51,13 @@ class TwoFactorAuthentication extends Model implements TwoFactorTotp * @var array */ protected $casts = [ - 'shared_secret' => 'encrypted', - 'digits' => 'int', - 'seconds' => 'int', - 'window' => 'int', - 'recovery_codes' => 'encrypted:collection', - 'safe_devices' => 'collection', - 'enabled_at' => 'datetime', + 'shared_secret' => 'encrypted', + 'digits' => 'int', + 'seconds' => 'int', + 'window' => 'int', + 'recovery_codes' => 'encrypted:collection', + 'safe_devices' => 'collection', + 'enabled_at' => 'datetime', 'recovery_codes_generated_at' => 'datetime', ]; diff --git a/src/TwoFactorAuthentication.php b/src/TwoFactorAuthentication.php index 784bd21..a52bd20 100644 --- a/src/TwoFactorAuthentication.php +++ b/src/TwoFactorAuthentication.php @@ -251,8 +251,8 @@ public function addSafeDevice(Request $request): string $this->twoFactorAuth->safe_devices = $this->safeDevices() ->push([ '2fa_remember' => $token = $this->generateTwoFactorRemember(), - 'ip' => $request->ip(), - 'added_at' => $this->freshTimestamp()->getTimestamp(), + 'ip' => $request->ip(), + 'added_at' => $this->freshTimestamp()->getTimestamp(), ]) ->sortByDesc('added_at') // Ensure the last is the first, so we can slice it. ->slice(0, config('two-factor.safe_devices.max_devices', 3)) diff --git a/tests/CreatesTwoFactorUser.php b/tests/CreatesTwoFactorUser.php index 54d85c5..f04ba43 100644 --- a/tests/CreatesTwoFactorUser.php +++ b/tests/CreatesTwoFactorUser.php @@ -13,8 +13,8 @@ trait CreatesTwoFactorUser protected function createTwoFactorUser(): void { $this->user = UserTwoFactorStub::create([ - 'name' => 'foo', - 'email' => 'foo@test.com', + 'name' => 'foo', + 'email' => 'foo@test.com', 'password' => UserStub::PASSWORD_SECRET, ]); diff --git a/tests/Eloquent/TwoFactorAuthenticationTest.php b/tests/Eloquent/TwoFactorAuthenticationTest.php index 113331f..9ca04db 100644 --- a/tests/Eloquent/TwoFactorAuthenticationTest.php +++ b/tests/Eloquent/TwoFactorAuthenticationTest.php @@ -22,8 +22,8 @@ class TwoFactorAuthenticationTest extends TestCase public function test_returns_authenticatable(): void { $user = UserTwoFactorStub::create([ - 'name' => 'foo', - 'email' => 'foo@test.com', + 'name' => 'foo', + 'email' => 'foo@test.com', 'password' => UserStub::PASSWORD_SECRET, ]); @@ -65,7 +65,7 @@ public function test_flushes_authentication(): void ->withRecovery()->withSafeDevices() ->create([ 'authenticatable_type' => 'test', - 'authenticatable_id' => 9, + 'authenticatable_id' => 9, ]); static::assertNotNull($old = $tfa->shared_secret); @@ -142,7 +142,7 @@ public function test_validate_code(): void { $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ 'shared_secret' => static::SECRET, - 'window' => 0, + 'window' => 0, ]); $this->travelTo(Date::create(2020, 1, 1, 20, 30, 0)); @@ -161,7 +161,7 @@ public function test_validate_code_with_window(): void { $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ 'shared_secret' => static::SECRET, - 'window' => 1, + 'window' => 1, ]); $this->travelTo(Date::create(2020, 1, 1, 20, 30, 0)); @@ -201,7 +201,7 @@ public function test_contains_unused_recovery_codes(): void $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ 'recovery_codes' => collect([ [ - 'code' => '2G5oP36', + 'code' => '2G5oP36', 'used_at' => 'anything not null', ], ]), @@ -252,10 +252,10 @@ public function test_serializes_to_uri(): void $this->app->make('config')->set('two-factor.issuer', 'quz'); $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ - 'label' => 'test@foo.com', + 'label' => 'test@foo.com', 'shared_secret' => static::SECRET, - 'algorithm' => 'sHa256', - 'digits' => 14, + 'algorithm' => 'sHa256', + 'digits' => 14, ]); $uri = 'otpauth://totp/quz%3Atest@foo.com?issuer=quz&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14'; @@ -268,10 +268,10 @@ public function test_serializes_to_qr_and_renders_to_qr(): void $this->app->make('config')->set('two-factor.issuer', 'quz'); $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ - 'label' => 'test@foo.com', + 'label' => 'test@foo.com', 'shared_secret' => static::SECRET, - 'algorithm' => 'sHa256', - 'digits' => 14, + 'algorithm' => 'sHa256', + 'digits' => 14, ]); static::assertStringEqualsFile(__DIR__.'/../Stubs/QrStub.svg', $tfa->toQr()); @@ -289,10 +289,10 @@ public function test_serializes_to_qr_and_renders_to_qr_with_custom_values(): vo ]); $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ - 'label' => 'test@foo.com', + 'label' => 'test@foo.com', 'shared_secret' => static::SECRET, - 'algorithm' => 'sHa256', - 'digits' => 14, + 'algorithm' => 'sHa256', + 'digits' => 14, ]); static::assertStringEqualsFile(__DIR__.'/../Stubs/CustomQrStub.svg', $tfa->toQr()); @@ -304,10 +304,10 @@ public function test_serializes_uri_to_json(): void $this->app->make('config')->set('two-factor.issuer', 'quz'); $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ - 'label' => 'test@foo.com', + 'label' => 'test@foo.com', 'shared_secret' => static::SECRET, - 'algorithm' => 'sHa256', - 'digits' => 14, + 'algorithm' => 'sHa256', + 'digits' => 14, ]); $uri = '"otpauth:\/\/totp\/quz%3Atest@foo.com?issuer=quz&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14"'; @@ -319,10 +319,10 @@ public function test_serializes_uri_to_json(): void public function test_uses_app_name_as_issuer(): void { $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ - 'label' => 'test@foo.com', + 'label' => 'test@foo.com', 'shared_secret' => static::SECRET, - 'algorithm' => 'sHa256', - 'digits' => 14, + 'algorithm' => 'sHa256', + 'digits' => 14, ]); $uri = 'otpauth://totp/Laravel%3Atest@foo.com?issuer=Laravel&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14'; @@ -335,10 +335,10 @@ public function test_changes_issuer(): void $this->app->make('config')->set('two-factor.issuer', 'foo bar'); $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ - 'label' => 'test@foo.com', + 'label' => 'test@foo.com', 'shared_secret' => static::SECRET, - 'algorithm' => 'sHa256', - 'digits' => 14, + 'algorithm' => 'sHa256', + 'digits' => 14, ]); $uri = 'otpauth://totp/foo%20bar%3Atest@foo.com?issuer=foo%20bar&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14'; @@ -352,10 +352,10 @@ public function test_throws_exception_when_issuer_is_empty(): void $this->app->make('config')->set('two-factor.issuer', ''); $tfa = TwoFactorAuthentication::factory()->withRecovery()->withSafeDevices()->make([ - 'label' => 'test@foo.com', + 'label' => 'test@foo.com', 'shared_secret' => static::SECRET, - 'algorithm' => 'sHa256', - 'digits' => 14, + 'algorithm' => 'sHa256', + 'digits' => 14, ]); $this->expectException(InvalidArgumentException::class); diff --git a/tests/Events/EventsTest.php b/tests/Events/EventsTest.php index 0d5d72a..fe03ea0 100644 --- a/tests/Events/EventsTest.php +++ b/tests/Events/EventsTest.php @@ -58,7 +58,7 @@ public function test_fires_two_factor_recovery_codes_depleted(): void $this->user->twoFactorAuth->recovery_codes = Collection::times(1, static function () use ($code): array { return [ - 'code' => $code, + 'code' => $code, 'used_at' => null, ]; }); diff --git a/tests/Http/Middleware/ConfirmTwoFactorEnabledTest.php b/tests/Http/Middleware/ConfirmTwoFactorEnabledTest.php index 945ec03..ba4163f 100644 --- a/tests/Http/Middleware/ConfirmTwoFactorEnabledTest.php +++ b/tests/Http/Middleware/ConfirmTwoFactorEnabledTest.php @@ -45,8 +45,8 @@ public function test_guest_cant_access(): void public function test_continues_if_user_is_not_2fa_instance(): void { $this->actingAs(UserStub::create([ - 'name' => 'test', - 'email' => 'bar@test.com', + 'name' => 'test', + 'email' => 'bar@test.com', 'password' => UserStub::PASSWORD_SECRET, ])); diff --git a/tests/Http/Middleware/RequireTwoFactorEnabledTest.php b/tests/Http/Middleware/RequireTwoFactorEnabledTest.php index 9af1aa2..f5d0c3b 100644 --- a/tests/Http/Middleware/RequireTwoFactorEnabledTest.php +++ b/tests/Http/Middleware/RequireTwoFactorEnabledTest.php @@ -47,8 +47,8 @@ public function test_guest_cant_access(): void public function test_user_no_2fa_can_access(): void { $this->actingAs(UserStub::create([ - 'name' => 'test', - 'email' => 'bar@test.com', + 'name' => 'test', + 'email' => 'bar@test.com', 'password' => UserStub::PASSWORD_SECRET, ])); diff --git a/tests/TwoFactorAuthenticationTest.php b/tests/TwoFactorAuthenticationTest.php index e27e479..7ada778 100644 --- a/tests/TwoFactorAuthenticationTest.php +++ b/tests/TwoFactorAuthenticationTest.php @@ -79,8 +79,8 @@ public function test_creates_two_factor_authentication(): void { $events = Event::fake(); $user = UserTwoFactorStub::create([ - 'name' => 'bar', - 'email' => 'bar@test.com', + 'name' => 'bar', + 'email' => 'bar@test.com', 'password' => UserStub::PASSWORD_SECRET, ]); @@ -147,8 +147,8 @@ public function test_new_user_confirms_two_factor_successfully(): void Date::setTestNow($now = Date::create(2020, 01, 01, 18, 30)); $user = UserTwoFactorStub::create([ - 'name' => 'bar', - 'email' => 'bar@test.com', + 'name' => 'bar', + 'email' => 'bar@test.com', 'password' => UserStub::PASSWORD_SECRET, ]);