Skip to content

Commit

Permalink
Adding context length configuration for 2FA to ensure better security…
Browse files Browse the repository at this point in the history
… standards (#568)

* Update TwoFactorAuthenticationProvider.php

Added secret length option to generate secret key

* Update EnableTwoFactorAuthentication.php

Consume a new option that can be set from config files to ensure basic required length for 2FA security

* Update TwoFactorAuthenticationProvider.php

Update contract to reflect new security standard in 2FA

* Update TwoFactorAuthenticationProvider.php

Reverted Contract mandatory parameter to avoid backward incompatibility

* Fixing typo TwoFactorAuthenticationProvider.php

* Switched case in EnableTwoFactorAuthentication.php

* Update TwoFactorAuthenticationProvider.php
  • Loading branch information
MattLoyeD committed Sep 16, 2024
1 parent a3cae72 commit ccc5511
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Actions/EnableTwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ public function __construct(TwoFactorAuthenticationProvider $provider)
public function __invoke($user, $force = false)
{
if (empty($user->two_factor_secret) || $force === true) {

$secretLength = (int) config('fortify-options.two-factor-authentication.secret-length', 16);

$user->forceFill([
'two_factor_secret' => encrypt($this->provider->generateSecretKey()),
'two_factor_secret' => encrypt($this->provider->generateSecretKey($secretLength)),
'two_factor_recovery_codes' => encrypt(json_encode(Collection::times(8, function () {
return RecoveryCode::generate();
})->all())),
Expand Down
5 changes: 3 additions & 2 deletions src/TwoFactorAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public function __construct(Google2FA $engine, Repository $cache = null)
/**
* Generate a new secret key.
*
* @param int $secretLength
* @return string
*/
public function generateSecretKey()
public function generateSecretKey(int $secretLength = 16)
{
return $this->engine->generateSecretKey();
return $this->engine->generateSecretKey($secretLength);
}

/**
Expand Down

0 comments on commit ccc5511

Please sign in to comment.