/** * Create a new token (and generate the token) * * @param AbstractToken $token * @return AbstractToken */ public function createToken(AbstractToken $token) : AbstractToken { $scopes = $token->getScopes(); if (empty($scopes)) { $defaultScopes = $this->scopeService->getDefaultScopes(); $token->setScopes($defaultScopes); } else { $this->validateTokenScopes($scopes); } $expiresAt = new DateTime(); $expiresAt->setTimestamp(time() + $this->tokenTTL); $token->setExpiresAt($expiresAt); do { $tokenHash = bin2hex(random_bytes(20)); } while ($this->tokenRepository->findByToken($tokenHash) !== null); $token->setToken($tokenHash); return $this->tokenRepository->save($token); }