Create a new RefreshToken

@param string|string[]|Scope[]|null $scopes
552924
public static function createNewRefreshToken(
        int $ttl,
        TokenOwnerInterface $owner = null,
        Client $client = null,
        $scopes = null
    ): RefreshToken {
        return static::createNew($ttl, $owner, $client, $scopes);
    }
1198890
private static function initAccessToken($accessToken)
    {
        if (is_array($accessToken)) {
            return new AccessToken($accessToken);
        } elseif (is_a($accessToken, '\League\OAuth2\Client\Token\AccessToken')) {
            return $accessToken;
        } else {
            return null;
        }
    }
829614
public function refreshAccessToken($refreshToken, $expiresIn = null)
    {
        $parameters = [
            'grant_type' => 'refresh_token',
            'refresh_token' => $refreshToken,
        ];

        if ($expiresIn != null) {
            $parameters['expires_in'] = $expiresIn;
        }

        return $this->tokenRequest('oauth2/token', $parameters);
    }
706627
protected function prepareTokenResponse(
        AccessToken $accessToken,
        RefreshToken $refreshToken = null,
        bool $useRefreshTokenScopes = false
    ): ResponseInterface {
        $owner = $accessToken->getOwner();
        $scopes = $useRefreshTokenScopes ? $refreshToken->getScopes() : $accessToken->getScopes();

        $responseBody = [
            'access_token' => $accessToken->getToken(),
            'token_type' => 'Bearer',
            'expires_in' => $accessToken->getExpiresIn(),
            'scope' => implode(' ', $scopes),
            'owner_id' => $owner ? $owner->getTokenOwnerId() : null,
        ];

        if (null !== $refreshToken) {
            $responseBody['refresh_token'] = $refreshToken->getToken();
        }

        return new Response\JsonResponse(array_filter($responseBody));
    }
1113815
private function createCreateForm(Scope $scope)
    {
        $form = $this->createForm(new ScopeType(), $scope, array(
            'action' => $this->generateUrl('admin_scope_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }
760649
public function createToken($owner, $client, array $scopes = []): AccessToken
    {
        if (empty($scopes)) {
            $scopes = $this->scopeService->getDefaultScopes();
        } else {
            $this->validateTokenScopes($scopes);
        }

        do {
            $token = AccessToken::createNewAccessToken(
                $this->serverOptions->getAccessTokenTtl(),
                $owner,
                $client,
                $scopes
            );
        } while ($this->tokenRepository->tokenExists($token->getToken()));

        return $this->tokenRepository->save($token);
    }
123964
protected function refreshGetScope(array $parameters): ?array
    {
        $scope = $this->refreshReadStringValue($parameters, 'scope');

        return empty($scope) === false ? explode(' ', $scope) : null;
    }
78046
public function refreshToken($accessToken = null)
    {
        $accessToken = $accessToken ?: $this->accessToken;
        $this->accessToken = $this->provider->getAccessToken('refresh_token', [
            'refresh_token' => $accessToken->getRefreshToken()
        ]);
        return $this->accessToken;
    }
1215554
public function setMergedGrantedScopes(array $scopes = null)
    {
        ValidationUtility::ensureNullOrArrayOfString('$scopes', $scopes);

        $this->mergedGrantedScopes = $scopes;

        return $this;
    }
1239874
public static function createNewAccessToken(
        int $ttl,
        TokenOwnerInterface $owner = null,
        Client $client = null,
        $scopes = null
    ): AccessToken {
        return static::createNew($ttl, $owner, $client, $scopes);
    }
507383
public function setRefreshToken($refreshToken = null)
    {
        $this->refreshToken = null === $refreshToken
            ? bin2hex(openssl_random_pseudo_bytes(64))
            : $refreshToken;

        return $this;
    }
532774
protected static function createNew(
        int $ttl,
        TokenOwnerInterface $owner = null,
        Client $client = null,
        array $scopes = null
    ): self {
        if (is_array($scopes)) {
            $scopes = array_map(function ($scope) {
                return (string) $scope;
            }, $scopes);
        }

        $token = new static();

        $token->token = bin2hex(random_bytes(20));
        $token->owner = $owner;
        $token->client = $client;
        $token->scopes = $scopes ?? [];
        $token->expiresAt = $ttl ? (new DateTime('@' . time(), new DateTimeZone('UTC')))->modify("+$ttl seconds") : null;

        return $token;
    }
1023653
public function setScopes(array $scopes = null)
    {
        ValidationUtility::ensureNullOrArrayOfString('$scopes', $scopes);

        $this->scopes = $scopes;

        return $this;
    }
655806
public function refreshToken($forceRefresh = false)
    {
        if (property_exists($this, "sdl") && $this->sdl == 0) {
            parent::__construct($this->xmlLoc, array('trace'=>1, 'exceptions'=>0));
        }
        try {
            $currentTime = new DateTime();
            if (is_null($this->getAuthTokenExpiration($this->tenantKey))) {
                $timeDiff = 0;
            } else {
                $timeDiff = $currentTime->diff($this->getAuthTokenExpiration($this->tenantKey))->format('%i');
                $timeDiff = $timeDiff  + (60 * $currentTime->diff($this->getAuthTokenExpiration($this->tenantKey))->format('%H'));
            }
            if (is_null($this->getAuthToken($this->tenantKey)) || ($timeDiff < 5) || $forceRefresh) {
                $url = $this->tenantKey == null
                        ? $this->baseAuthUrl."/v1/requestToken?legacy=1"
                        : $this->baseUrl."/provisioning/v1/tenants/{$this->tenantKey}/requestToken?legacy=1";

                $jsonRequest = new stdClass();
                $jsonRequest->clientId = $this->clientId;
                $jsonRequest->clientSecret = $this->clientSecret;
                $jsonRequest->accessType = "offline";
                if (!is_null($this->getRefreshToken($this->tenantKey))) {
                    $jsonRequest->refreshToken = $this->getRefreshToken($this->tenantKey);
                }
                $authResponse = ET_Util::restPost($url, json_encode($jsonRequest), $this);
                $authObject = json_decode($authResponse->body);
                //echo "auth:  \n";
                //print_r($authResponse);

                if ($authResponse && property_exists($authObject, "accessToken")) {
                    $dv = new DateInterval('PT'.$authObject->expiresIn.'S');
                    $newexpTime = new DateTime();
                    $this->setAuthToken($this->tenantKey, $authObject->accessToken, $newexpTime->add($dv));
                    $this->setInternalAuthToken($this->tenantKey, $authObject->legacyToken);
                    if (property_exists($authObject, 'refreshToken')) {
                        $this->setRefreshToken($this->tenantKey, $authObject->refreshToken);
                    }
                } else {
                    throw new Exception('Unable to validate App Keys(ClientID/ClientSecret) provided, requestToken response:'.$authResponse->body);
                }
            }
        } catch (Exception $e) {
            throw new Exception('Unable to validate App Keys(ClientID/ClientSecret) provided.: '.$e->getMessage());
        }
    }
648890
public function validateAccessToken(Array $scopes = array())
  {
    try {
      $this->getResource()->isValid($this->getConfig('oauth2.http_headers_only'));
    } catch (InvalidAccessTokenException $e) {
      return $this->resourceJson(array(
        'message'   => 'forbidden',
        'description' => $e->getMessage(),
      ), 403);
    }

    if ( ! empty($scopes)) {
      foreach ($scopes as $item) {
        if ( ! $this->getResource()->hasScope($item)) {
          return $this->resourceJson(array(
            'message'   => 'forbidden',
            'description' => 'Only access token with scope '.$item.' can be use in this endpoint',
          ), 403);
        }
      }
    }
  }
430957
public static function getToken($params = null)
    {
        if (!$params) {
            $params = array();
        }
        $requestor = new ApiRequestor(Sumup::$connectBase, true);
        $params['client_id'] = self::_getConnectParam('client_id', $params);
        $params['client_secret'] = self::_getConnectParam('client_secret', $params);
        $response = $requestor->request('post', '/token', $params);

        if ($response->raw->getStatusCode() !== 200) {
            throw new Error\AuthenticationError('Token request error');
        }

        return new AccessToken($response);
    }
791009
public function refreshTokenWithAssertion($assertionCredentials = null) {
    if (!$assertionCredentials) {
      $assertionCredentials = $this->assertionCredentials;
    }

    $this->refreshTokenRequest(array(
        'grant_type' => 'assertion',
        'assertion_type' => $assertionCredentials->assertionType,
        'assertion' => $assertionCredentials->generateAssertion(),
    ));
  }