Token

class Token implements JsonSerializable

Class Token

Describes the access and refresh tokens for an authentication result. Requires that all relevant token fields contain non-null values. The expires field must also be an integer. The scope field should contain a string of one ore more space separated scopes.

Constants

REQUIRED = [

Methods

public static TokenResult fromJSON(string $record) No description
public static TokenResult fromArray(array $record) No description
public static TokenResult fromStdClass(stdClass $record) No description
public array toArray() No description
public stdClass toStdClass() No description
public mixed jsonSerialize() No description
public string getTokenType() Returns the type of token being held.
public array getScope() Returns an array of scopes that this token is valid for.
public string getAccessToken() Returns the access token for this authentication result.
public string getRefreshToken() Returns the refresh token for this authentication result.
public int getExpires() Returns the expiration value for this authentication result.

Details

at line 71

fromJSON()

public static TokenResult fromJSON(string $record)

Parameters

string $record

Return Value

TokenResult
at line 84

fromArray()

public static TokenResult fromArray(array $record)

Parameters

array $record

Return Value

TokenResult
at line 99

fromStdClass()

public static TokenResult fromStdClass(stdClass $record)

Parameters

stdClass $record

Return Value

TokenResult
at line 131

toArray()

public array toArray()

Return Value

array
at line 144

toStdClass()

public stdClass toStdClass()

Return Value

stdClass
at line 151

jsonSerialize()

public mixed jsonSerialize()

Return Value

mixed
at line 160

getTokenType()

public string getTokenType()

Returns the type of token being held.

Return Value

string
at line 169

getScope()

public array getScope()

Returns an array of scopes that this token is valid for.

Return Value

array
at line 178

getAccessToken()

public string getAccessToken()

Returns the access token for this authentication result.

Return Value

string
at line 187

getRefreshToken()

public string getRefreshToken()

Returns the refresh token for this authentication result.

Return Value

string
at line 196

getExpires()

public int getExpires()

Returns the expiration value for this authentication result.

Return Value

int

Source code

<?php

namespace LibPBSAuth;

use LibPBSAuth\Result\TokenResult;

/**
 * Class Token
 *
 * Describes the access and refresh tokens for an authentication result.
 * Requires that all relevant token fields contain non-null values. The expires
 * field must also be an integer. The scope field should contain a string of
 * one ore more space separated scopes.
 *
 * @package LibPBSAuth
 */
class Token implements \JsonSerializable {

  /**
   * @var array
   */
  const REQUIRED = [
    'token_type', 'scope', 'access_token', 'refresh_token', 'expires'
  ];

  /**
   * @var string
   */
  private $_tokenType;

  /**
   * @var array
   */
  private $_scope;

  /**
   * @var string
   */
  private $_accessToken;

  /**
   * @var string
   */
  private $_refreshToken;

  /**
   * @var int
   */
  private $_expires;

  /**
   * Token constructor.
   * @param string $tokenType
   * @param array $scope
   * @param string $accessToken
   * @param string $refreshToken
   * @param int $expires
   */
  private function __construct(string $tokenType, array $scope, string $accessToken, string $refreshToken, int $expires) {
    $this->_tokenType = $tokenType;
    $this->_scope = $scope;
    $this->_accessToken = $accessToken;
    $this->_refreshToken = $refreshToken;
    $this->_expires = $expires;
  }

  /**
   * @param string $record
   * @return TokenResult
   */
  public static function fromJSON(string $record): TokenResult {
    try {
      $parsed = ex_json_decode($record);
      return self::fromStdClass($parsed);
    } catch (\Exception $e) {
      return TokenResult::err($e);
    }
  }

  /**
   * @param array $record
   * @return TokenResult
   */
  public static function fromArray(array $record): TokenResult {

    // Records do not get terribly large, so for simplicity we encode and then
    // decode from JSON at the cost of a little performance
    try {
      return self::fromJSON(ex_json_encode($record));
    } catch (\Exception $e) {
      return TokenResult::err($e);
    }
  }

  /**
   * @param \stdClass $record
   * @return TokenResult
   */
  public static function fromStdClass(\stdClass $record): TokenResult {
    foreach (self::REQUIRED as $req) {
      if (!isset($record->{$req})) {
        return TokenResult::err(new \InvalidArgumentException("Malformed token data. {$req} field is missing."));
      }
    }

    if (!is_int($record->expires)) {
      return TokenResult::err(new \Exception('Malformed token data. Expires field must be an integer'));
    }

    $scopes = array_filter(
      explode(' ', $record->scope),
      function($scope) {
        return $scope !== '';
      }
    );

    return TokenResult::ok(
      new Token(
        $record->token_type,
        $scopes,
        $record->access_token,
        $record->refresh_token,
        $record->expires
      )
    );
  }

  /**
   * @return array
   */
  public function toArray(): array {
    return [
      'token_type' => $this->getTokenType(),
      'scope' => implode(' ', $this->getScope()),
      'access_token' => $this->getAccessToken(),
      'refresh_token' => $this->getRefreshToken(),
      'expires' => $this->getExpires()
    ];
  }

  /**
   * @return \stdClass
   */
  public function toStdClass(): \stdClass {
    return json_decode(json_encode($this));
  }

  /**
   * @return mixed
   */
  public function jsonSerialize() {
    return $this->toArray();
  }

  /**
   * Returns the type of token being held.
   *
   * @return string
   */
  public function getTokenType(): string {
    return $this->_tokenType;
  }

  /**
   * Returns an array of scopes that this token is valid for.
   *
   * @return array
   */
  public function getScope(): array {
    return $this->_scope;
  }

  /**
   * Returns the access token for this authentication result.
   *
   * @return string
   */
  public function getAccessToken(): string {
    return $this->_accessToken;
  }

  /**
   * Returns the refresh token for this authentication result.
   *
   * @return string
   */
  public function getRefreshToken(): string {
    return $this->_refreshToken;
  }

  /**
   * Returns the expiration value for this authentication result.
   *
   * @return int
   */
  public function getExpires(): int {
    return $this->_expires;
  }
}