-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an Account Security Indicator (#11)
* Add Account Security Indicator
- Loading branch information
1 parent
317e905
commit 86af2d5
Showing
7 changed files
with
193 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace ClaudioDekker\LaravelAuth\Support; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
|
||
enum AccountSecurityIndicator implements Arrayable | ||
{ | ||
case NO_MFA_NO_RECOVERY_CODES; | ||
case NO_MFA_HAS_RECOVERY_CODES; | ||
case HAS_MFA_NO_RECOVERY_CODES; | ||
case HAS_MFA_HAS_RECOVERY_CODES; | ||
|
||
/** | ||
* Determine the color of the indicator. | ||
* | ||
* @return string | ||
*/ | ||
public function color(): string | ||
{ | ||
return match ($this) { | ||
self::NO_MFA_NO_RECOVERY_CODES, self::NO_MFA_HAS_RECOVERY_CODES => 'RED', | ||
self::HAS_MFA_NO_RECOVERY_CODES => 'ORANGE', | ||
self::HAS_MFA_HAS_RECOVERY_CODES => 'GREEN', | ||
}; | ||
} | ||
|
||
/** | ||
* Determine whether the account security indicator has any issues to indicate. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasIssues(): bool | ||
{ | ||
return $this->color() !== 'GREEN'; | ||
} | ||
|
||
/** | ||
* Determine the message that should be displayed for the indicator. | ||
* | ||
* @return string | ||
*/ | ||
public function message(): string | ||
{ | ||
return match ($this) { | ||
self::NO_MFA_NO_RECOVERY_CODES => __('laravel-auth::auth.security-indicator.no-mfa-no-recovery-codes'), | ||
self::NO_MFA_HAS_RECOVERY_CODES => __('laravel-auth::auth.security-indicator.no-mfa-has-recovery-codes'), | ||
self::HAS_MFA_NO_RECOVERY_CODES => __('laravel-auth::auth.security-indicator.has-mfa-no-recovery-codes'), | ||
self::HAS_MFA_HAS_RECOVERY_CODES => __('laravel-auth::auth.security-indicator.has-mfa-has-recovery-codes'), | ||
}; | ||
} | ||
|
||
/** | ||
* Get the instance as an array. | ||
* | ||
* @return array<TKey, TValue> | ||
*/ | ||
public function toArray() | ||
{ | ||
return [ | ||
'color' => $this->color(), | ||
'has_issues' => $this->hasIssues(), | ||
'message' => $this->message(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
namespace Tests\Unit; | ||
|
||
use App\Models\User; | ||
use ClaudioDekker\LaravelAuth\MultiFactorCredential; | ||
use ClaudioDekker\LaravelAuth\Support\AccountSecurityIndicator; | ||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
use Tests\TestCase; | ||
|
||
class UserTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
/** {!! '@' !!}test */ | ||
public function the_account_security_indicator_indicates_the_user_has_both_mfa_and_recovery_codes(): void | ||
{ | ||
$user = User::factory()->create(['recovery_codes' => ['foo', 'bar']]); | ||
MultiFactorCredential::factory()->forUser($user)->publicKey()->create(); | ||
|
||
tap($user->accountSecurityIndicator(), function (AccountSecurityIndicator $indicator) { | ||
$this->assertFalse($indicator->hasIssues()); | ||
$this->assertSame("GREEN", $indicator->color()); | ||
$this->assertSame(__('laravel-auth::auth.security-indicator.has-mfa-has-recovery-codes'), $indicator->message()); | ||
}); | ||
} | ||
|
||
/** {!! '@' !!}test */ | ||
public function the_account_security_indicator_indicates_the_user_has_no_mfa_and_no_recovery_codes(): void | ||
{ | ||
$user = User::factory()->create(); | ||
|
||
tap($user->accountSecurityIndicator(), function (AccountSecurityIndicator $indicator) { | ||
$this->assertTrue($indicator->hasIssues()); | ||
$this->assertSame("RED", $indicator->color()); | ||
$this->assertSame(__('laravel-auth::auth.security-indicator.no-mfa-no-recovery-codes'), $indicator->message()); | ||
}); | ||
} | ||
|
||
/** {!! '@' !!}test */ | ||
public function the_account_security_indicator_indicates_the_user_has_no_mfa(): void | ||
{ | ||
$user = User::factory()->create(['recovery_codes' => ['foo', 'bar']]); | ||
|
||
tap($user->accountSecurityIndicator(), function (AccountSecurityIndicator $indicator) { | ||
$this->assertTrue($indicator->hasIssues()); | ||
$this->assertSame("RED", $indicator->color()); | ||
$this->assertSame(__('laravel-auth::auth.security-indicator.no-mfa-has-recovery-codes'), $indicator->message()); | ||
}); | ||
} | ||
|
||
/** {!! '@' !!}test */ | ||
public function the_account_security_indicator_indicates_the_user_has_mfa_but_no_recovery_codes(): void | ||
{ | ||
$user = User::factory()->create(); | ||
MultiFactorCredential::factory()->forUser($user)->publicKey()->create(); | ||
|
||
tap($user->accountSecurityIndicator(), function (AccountSecurityIndicator $indicator) { | ||
$this->assertTrue($indicator->hasIssues()); | ||
$this->assertSame("ORANGE", $indicator->color()); | ||
$this->assertSame(__('laravel-auth::auth.security-indicator.has-mfa-no-recovery-codes'), $indicator->message()); | ||
}); | ||
} | ||
} |