You are here

protected function TfaTrustedBrowser::setTrusted in Two-factor Authentication (TFA) 8

Store browser value and issue cookie for user.

Parameters

string $id: Trusted browser id.

string $name: The custom browser name.

3 calls to TfaTrustedBrowser::setTrusted()
TfaTrustedBrowser::finalize in src/Plugin/TfaLogin/TfaTrustedBrowser.php
Finalize the browser setup.
TfaTrustedBrowser::submitForm in src/Plugin/TfaLogin/TfaTrustedBrowser.php
Submit form.
TfaTrustedBrowserSetup::submitSetupForm in src/Plugin/TfaSetup/TfaTrustedBrowserSetup.php
Submit the setup form.

File

src/Plugin/TfaLogin/TfaTrustedBrowser.php, line 132

Class

TfaTrustedBrowser
Trusted browser validation class.

Namespace

Drupal\tfa\Plugin\TfaLogin

Code

protected function setTrusted($id, $name = '') {

  // Currently broken.
  // Store id for account.
  $records = $this
    ->getUserData('tfa', 'tfa_trusted_browser', $this->configuration['uid'], $this->userData) ?: [];
  $request_time = \Drupal::time()
    ->getRequestTime();
  $records[$id] = [
    'created' => $request_time,
    'ip' => \Drupal::request()
      ->getClientIp(),
    'name' => $name,
  ];
  $data = [
    'tfa_trusted_browser' => $records,
  ];
  $this
    ->setUserData('tfa', $data, $this->configuration['uid'], $this->userData);

  // Issue cookie with ID.
  $cookie_secure = ini_get('session.cookie_secure');
  $expiration = $request_time + $this->expiration;
  $domain = strpos($_SERVER['HTTP_HOST'], 'localhost') === FALSE ? $_SERVER['HTTP_HOST'] : FALSE;
  setcookie($this->cookieName, $id, $expiration, '/', $domain, empty($cookie_secure) ? FALSE : TRUE, TRUE);
  $name = empty($name) ? $this
    ->getAgent() : $name;

  // @todo use services defined in module instead this procedural way.
  \Drupal::logger('tfa')
    ->info('Set trusted browser for user UID @uid, browser @name', [
    '@name' => $name,
    '@uid' => $this->uid,
  ]);
}