Skip to content

Commit

Permalink
Merge pull request #66 from Laragear/analysis-329D7P
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI

[ci skip] [skip ci]
  • Loading branch information
DarkGhostHunter authored Mar 5, 2024
2 parents 923dc75 + 4eef5cd commit 3356ab8
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 72 deletions.
20 changes: 10 additions & 10 deletions config/two-factor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'cache' => [
'store' => null,
'store' => null,
'prefix' => '2fa.code',
],

Expand All @@ -30,8 +30,8 @@

'recovery' => [
'enabled' => true,
'codes' => 10,
'length' => 8,
'codes' => 10,
'length' => 8,
],

/*
Expand All @@ -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,
],

Expand Down Expand Up @@ -118,9 +118,9 @@
'issuer' => env('OTP_TOTP_ISSUER'),

'totp' => [
'digits' => 6,
'seconds' => 30,
'window' => 1,
'digits' => 6,
'seconds' => 30,
'window' => 1,
'algorithm' => 'sha1',
],

Expand All @@ -136,7 +136,7 @@
*/

'qr_code' => [
'size' => 400,
'size' => 400,
'margin' => 4,
],
];
16 changes: 8 additions & 8 deletions lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<?php

return [
'title' => '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.',
],
];
6 changes: 3 additions & 3 deletions src/Models/Concerns/HandlesRecoveryCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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(),
]);

Expand Down Expand Up @@ -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,
];
});
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Concerns/SerializesSharedSecret.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
15 changes: 8 additions & 7 deletions src/Models/TwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
];

Expand Down
4 changes: 2 additions & 2 deletions src/TwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions tests/CreatesTwoFactorUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ trait CreatesTwoFactorUser
protected function createTwoFactorUser(): void
{
$this->user = UserTwoFactorStub::create([
'name' => 'foo',
'email' => '[email protected]',
'name' => 'foo',
'email' => '[email protected]',
'password' => UserStub::PASSWORD_SECRET,
]);

Expand Down
54 changes: 27 additions & 27 deletions tests/Eloquent/TwoFactorAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class TwoFactorAuthenticationTest extends TestCase
public function test_returns_authenticatable(): void
{
$user = UserTwoFactorStub::create([
'name' => 'foo',
'email' => '[email protected]',
'name' => 'foo',
'email' => '[email protected]',
'password' => UserStub::PASSWORD_SECRET,
]);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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',
],
]),
Expand Down Expand Up @@ -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' => '[email protected]',
'label' => '[email protected]',
'shared_secret' => static::SECRET,
'algorithm' => 'sHa256',
'digits' => 14,
'algorithm' => 'sHa256',
'digits' => 14,
]);

$uri = 'otpauth://totp/quz%[email protected]?issuer=quz&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14';
Expand All @@ -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' => '[email protected]',
'label' => '[email protected]',
'shared_secret' => static::SECRET,
'algorithm' => 'sHa256',
'digits' => 14,
'algorithm' => 'sHa256',
'digits' => 14,
]);

static::assertStringEqualsFile(__DIR__.'/../Stubs/QrStub.svg', $tfa->toQr());
Expand All @@ -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' => '[email protected]',
'label' => '[email protected]',
'shared_secret' => static::SECRET,
'algorithm' => 'sHa256',
'digits' => 14,
'algorithm' => 'sHa256',
'digits' => 14,
]);

static::assertStringEqualsFile(__DIR__.'/../Stubs/CustomQrStub.svg', $tfa->toQr());
Expand All @@ -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' => '[email protected]',
'label' => '[email protected]',
'shared_secret' => static::SECRET,
'algorithm' => 'sHa256',
'digits' => 14,
'algorithm' => 'sHa256',
'digits' => 14,
]);

$uri = '"otpauth:\/\/totp\/quz%[email protected]?issuer=quz&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14"';
Expand All @@ -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' => '[email protected]',
'label' => '[email protected]',
'shared_secret' => static::SECRET,
'algorithm' => 'sHa256',
'digits' => 14,
'algorithm' => 'sHa256',
'digits' => 14,
]);

$uri = 'otpauth://totp/Laravel%[email protected]?issuer=Laravel&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14';
Expand All @@ -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' => '[email protected]',
'label' => '[email protected]',
'shared_secret' => static::SECRET,
'algorithm' => 'sHa256',
'digits' => 14,
'algorithm' => 'sHa256',
'digits' => 14,
]);

$uri = 'otpauth://totp/foo%20bar%[email protected]?issuer=foo%20bar&label=test%40foo.com&secret=KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3&algorithm=SHA256&digits=14';
Expand All @@ -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' => '[email protected]',
'label' => '[email protected]',
'shared_secret' => static::SECRET,
'algorithm' => 'sHa256',
'digits' => 14,
'algorithm' => 'sHa256',
'digits' => 14,
]);

$this->expectException(InvalidArgumentException::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/Events/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Middleware/ConfirmTwoFactorEnabledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
'name' => 'test',
'email' => '[email protected]',
'password' => UserStub::PASSWORD_SECRET,
]));

Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Middleware/RequireTwoFactorEnabledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
'name' => 'test',
'email' => '[email protected]',
'password' => UserStub::PASSWORD_SECRET,
]));

Expand Down
8 changes: 4 additions & 4 deletions tests/TwoFactorAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public function test_creates_two_factor_authentication(): void
{
$events = Event::fake();
$user = UserTwoFactorStub::create([
'name' => 'bar',
'email' => '[email protected]',
'name' => 'bar',
'email' => '[email protected]',
'password' => UserStub::PASSWORD_SECRET,
]);

Expand Down Expand Up @@ -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' => '[email protected]',
'name' => 'bar',
'email' => '[email protected]',
'password' => UserStub::PASSWORD_SECRET,
]);

Expand Down

0 comments on commit 3356ab8

Please sign in to comment.