function OneTimeAuthentication::generateHmac

Same name and namespace in other branches
  1. 11.x core/modules/user/src/OneTimeAuthentication.php \Drupal\user\OneTimeAuthentication::generateHmac()

Create a one time authentication code.

One time authentication codes are used to build a unique and secure URL that is sent to the user by email for purposes such as resetting the user's password.

For a usage example, see \Drupal\user\OneTimeAuthentication::generateCancelConfirmUrl() and \Drupal\user\Controller\UserController::confirmCancel().

Parameters

\Drupal\user\UserInterface $account: An object containing the user account.

int $timestamp: A UNIX timestamp, typically \Drupal::time()->getRequestTime().

Return value

string A string that is safe for use in URLs and SQL statements.

File

core/modules/user/src/OneTimeAuthentication.php, line 51

Class

OneTimeAuthentication
Generate and verify one time authentication codes.

Namespace

Drupal\user

Code

public function generateHmac(UserInterface $account, int $timestamp) : string {
  $data = $timestamp;
  $data .= ':' . $account->getLastLoginTime();
  $data .= ':' . $account->id();
  $data .= ':' . $account->getEmail();
  return Crypt::hmacBase64($data, Settings::getHashSalt() . $account->getPassword());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.