public function createAccessToken($clientId, $username = '', $scope = [], $state = null, $withRefreshToken = true)
{
$accessTokenManager = $this->modelManagerFactory->getModelManager('access_token');
$class = $accessTokenManager->getClassName();
$accessToken = new $class();
$accessToken->setAccessToken(md5(openssl_random_pseudo_bytes(256)))->setTokenType('bearer')->setClientId($clientId)->setUsername($username)->setExpires(new \DateTime('+1 hours'))->setScope((array) $scope);
$accessToken = $accessTokenManager->createModel($accessToken);
$parameters = ['access_token' => $accessToken->getAccessToken(), 'token_type' => $accessToken->getTokenType(), 'expires_in' => $accessToken->getExpires()->getTimestamp() - time()];
if (!empty($scope)) {
$parameters['scope'] = implode(' ', (array) $scope);
}
if (!empty($state)) {
$parameters['state'] = $state;
}
if ($withRefreshToken === true) {
$refreshTokenManager = $this->modelManagerFactory->getModelManager('refresh_token');
$class = $refreshTokenManager->getClassName();
$refreshToken = new $class();
$refreshToken->setRefreshToken(md5(openssl_random_pseudo_bytes(256)))->setClientId($clientId)->setUsername($username)->setExpires(new \DateTime('+1 days'))->setScope((array) $scope);
$refreshToken = $refreshTokenManager->createModel($refreshToken);
$parameters['refresh_token'] = $refreshToken->getRefreshToken();
}
return $parameters;
}