generateRandomToken() public static method

Returns a random token in hex format.
public static generateRandomToken ( integer $count ) : string
$count integer Token length
return string A random token
 /**
  * Redirects to the Neos backend on the given site, passing a one-time login token
  *
  * @param Site $site
  * @return void
  */
 public function switchSiteAction($site)
 {
     $token = Algorithms::generateRandomToken(32);
     $this->loginTokenCache->set($token, $this->currentSession->getId());
     $siteUri = $this->linkingService->createSiteUri($this->controllerContext, $site);
     $loginUri = $this->controllerContext->getUriBuilder()->reset()->uriFor('tokenLogin', ['token' => $token], 'Login', 'Neos.Neos');
     $this->redirectToUri($siteUri . $loginUri);
 }
示例#2
0
 /**
  * Generate a Token and assign it the given $identifier.
  *
  * A $presetName can be given to use a specific configuration preset. The given $meta data
  * will be stored with the Token and can be retrieved again when validation of the token
  * is done and was successful.
  *
  * @param string $identifier
  * @param string $presetName
  * @param array $meta
  * @return Token
  */
 public function generateToken($identifier, $presetName = 'default', array $meta = [])
 {
     $preset = $this->getPreset($presetName);
     $tokenHash = Algorithms::generateRandomToken($preset['tokenLength']);
     $this->tokenCache->set($tokenHash, ['identifier' => $identifier, 'presetName' => $presetName, 'meta' => $meta], [md5($identifier)], $preset['lifetime']);
     $expiryTime = new \DateTime(sprintf('now +%s seconds', $preset['lifetime']));
     $this->logger->log(sprintf('Token with hash %s generated for identifier %s (valid until %s) [%s]', $tokenHash, $identifier, $expiryTime->format('Y-m-d H:i:s'), $presetName), LOG_INFO);
     return new Token($tokenHash, $identifier, $preset, $meta);
 }
 /**
  * @test
  */
 public function generateRandomTokenGeneratesRandomToken()
 {
     $this->assertRegExp('/^[[:xdigit:]]{64}$/', Algorithms::generateRandomToken(32));
 }
 /**
  * Returns the current CSRF protection token. A new one is created when needed, depending on the  configured CSRF
  * protection strategy.
  *
  * @return string
  * @Flow\Session(autoStart=true)
  */
 public function getCsrfProtectionToken()
 {
     if ($this->initialized === false) {
         $this->initialize();
     }
     if (count($this->csrfProtectionTokens) === 1 && $this->csrfProtectionStrategy !== self::CSRF_ONE_PER_URI) {
         reset($this->csrfProtectionTokens);
         return key($this->csrfProtectionTokens);
     }
     $newToken = Algorithms::generateRandomToken(16);
     $this->csrfProtectionTokens[$newToken] = true;
     return $newToken;
 }