You are here

public function Auth0::exchange in Auth0 Single Sign On 8.2

Exchange authorization code for access, ID, and refresh tokens

Return value

boolean

Throws

CoreException If the state value is missing or invalid.

CoreException If there is already an active session.

ApiException If access token is missing from the response.

RequestException If HTTP request fails (e.g. access token does not have userinfo scope).

See also

https://auth0.com/docs/api-auth/tutorials/authorization-code-grant

4 calls to Auth0::exchange()
Auth0::getAccessToken in vendor/auth0/auth0-php/src/Auth0.php
Get access token from persisted session or from a code exchange
Auth0::getIdToken in vendor/auth0/auth0-php/src/Auth0.php
Get ID token from persisted session or from a code exchange
Auth0::getRefreshToken in vendor/auth0/auth0-php/src/Auth0.php
Get refresh token from persisted session or from a code exchange
Auth0::getUser in vendor/auth0/auth0-php/src/Auth0.php
Get userinfo from persisted session or from a code exchange

File

vendor/auth0/auth0-php/src/Auth0.php, line 549

Class

Auth0
Class Auth0 Provides access to Auth0 authentication functionality.

Namespace

Auth0\SDK

Code

public function exchange() {
  $code = $this
    ->getAuthorizationCode();
  if (!$code) {
    return false;
  }
  $state = $this
    ->getState();
  if (!$this->stateHandler
    ->validate($state)) {
    throw new CoreException('Invalid state');
  }
  if ($this->user) {
    throw new CoreException('Can\'t initialize a new session while there is one active session already');
  }
  $response = $this->authentication
    ->code_exchange($code, $this->redirectUri);
  if (empty($response['access_token'])) {
    throw new ApiException('Invalid access_token - Retry login.');
  }
  $this
    ->setAccessToken($response['access_token']);
  if (isset($response['refresh_token'])) {
    $this
      ->setRefreshToken($response['refresh_token']);
  }
  if (!empty($response['id_token'])) {
    $this
      ->setIdToken($response['id_token']);
  }
  if ($this->skipUserinfo) {
    $user = $this->idTokenDecoded;
  }
  else {
    $user = $this->authentication
      ->userinfo($this->accessToken);
  }
  if ($user) {
    $this
      ->setUser($user);
  }
  return true;
}