access_token = $access_token;
$this->expires_at = $expires_at;
$this->token_type = $token_type;
$this->refresh_token = $refresh_token;
$this->id_token = $id_token;
$this->scope = $scope;
$this->error_count = $error_count;
}
/**
* Returns the access token.
*
* @return string
*/
public function get_access_token(): string {
return $this->access_token;
}
/**
* Returns the Unix timestamp at which the access token expires.
*
* @return int
*/
public function get_expires_at(): int {
return $this->expires_at;
}
/**
* Returns the token type.
*
* @return string
*/
public function get_token_type(): string {
return $this->token_type;
}
/**
* Returns the refresh token, or null if not available.
*
* @return string|null
*/
public function get_refresh_token(): ?string {
return $this->refresh_token;
}
/**
* Returns the OIDC ID token, or null if not available.
*
* @return string|null
*/
public function get_id_token(): ?string {
return $this->id_token;
}
/**
* Returns the granted scope string, or null if not available.
*
* @return string|null
*/
public function get_scope(): ?string {
return $this->scope;
}
/**
* Checks if the token set has the required scope(s).
* Returns true if AT LEAST all required scopes are granted, false otherwise.
*
* @param string[] $required_scopes The required scopes as an array of strings.
*
* @return bool True if all required scopes are granted, false otherwise.
*/
public function has_scopes( array $required_scopes ): bool {
if ( $this->scope === null ) {
return \count( $required_scopes ) === 0;
}
$granted_scopes = \explode( ' ', $this->scope );
return \count( \array_diff( $required_scopes, $granted_scopes ) ) === 0;
}
/**
* Returns the number of consecutive refresh errors.
*
* @return int
*/
public function get_error_count(): int {
return $this->error_count;
}
/**
* Returns a new Token_Set with an incremented error count.
*
* @return self
*/
public function with_incremented_error_count(): self {
return new self(
$this->access_token,
$this->expires_at,
$this->token_type,
$this->refresh_token,
$this->id_token,
$this->scope,
$this->error_count + 1,
);
}
/**
* Whether the access token has expired.
*
* Uses a 60-second buffer to allow for request latency.
*
* @return bool
*/
public function is_expired(): bool {
return \time() >= ( $this->expires_at - self::EXPIRY_BUFFER_SECONDS );
}
/**
* Converts the token set to an associative array for storage.
*
* @return array