Airship\Cabin\Bridge\Blueprint\UserAccounts::resetTwoFactorSecret PHP Method

resetTwoFactorSecret() public method

Get the user's two-factor authentication secret
public resetTwoFactorSecret ( integer $userID ) : boolean
$userID integer
return boolean
    public function resetTwoFactorSecret(int $userID) : bool
    {
        $state = State::instance();
        $this->db->beginTransaction();
        $secret = \random_bytes(20);
        $this->db->update('airship_users', ['totp_secret' => Symmetric::encrypt($secret, $state->keyring['auth.password_key'])], ['userid' => $userID]);
        return $this->db->commit();
    }

Usage Example

Example #1
0
 /**
  * Make sure the secret exists, then get the GoogleAuth object
  *
  * @param int $userID
  * @return GoogleAuth
  * @throws \Airship\Alerts\Security\UserNotLoggedIn
  */
 protected function twoFactorPreamble(int $userID = 0) : GoogleAuth
 {
     if (!$userID) {
         $userID = $this->getActiveUserId();
     }
     $secret = $this->acct->getTwoFactorSecret($userID);
     if (empty($secret)) {
         if (!$this->acct->resetTwoFactorSecret($userID)) {
             \Airship\json_response(['test2']);
             \Airship\redirect($this->airship_cabin_prefix);
         }
         $secret = $this->acct->getTwoFactorSecret($userID);
     }
     return new GoogleAuth($secret, new TOTP(0, (int) ($this->config('two-factor.period') ?? 30), (int) ($this->config('two-factor.length') ?? 6)));
 }