/** * Generate new recovery codes for provided user * Previously generated codes will be removed * * @param int $userId Needed user id. * @return bool Returns true if successful * @throws ArgumentTypeException */ public static function regenerateCodes($userId) { $userId = (int) $userId; if ($userId <= 0) { throw new ArgumentTypeException('userId', 'positive integer'); } static::clearByUser($userId); $random = new Random(); $randomVector = $random->getString(static::CODES_PER_USER * 8); $randomVector = str_split($randomVector, 4); for ($i = 0; $i < static::CODES_PER_USER; $i++) { $code = array('USER_ID' => $userId, 'USED' => 'N', 'CODE' => sprintf('%s-%s', $randomVector[$i * 2], $randomVector[$i * 2 + 1])); static::add($code); } return true; }
/** * Reinitialize OTP (generate new secret, set default algo, etc), must be called before connect new device * * @param null $newSecret Using custom secret. * @return $this */ public function regenerate($newSecret = null) { if (!$newSecret) { $newSecret = Random::getBytes(static::SECRET_LENGTH); } $this->regenerated = true; return $this->setType(static::getDefaultType())->setAttempts(0)->setSkipMandatory(false)->setInitialDate(new Type\DateTime())->setDeactivateUntil(null)->setParams(null)->setSecret($newSecret)->setActive(true); }