You are here

class AuthTokenProvider in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8.2 api-sdk-php/src/AuthApi/AuthTokenProvider.php \Smartling\AuthApi\AuthTokenProvider
  2. 8.2 vendor/smartling/api-sdk-php/src/AuthApi/AuthTokenProvider.php \Smartling\AuthApi\AuthTokenProvider
  3. 8.3 vendor/smartling/api-sdk-php/src/AuthApi/AuthTokenProvider.php \Smartling\AuthApi\AuthTokenProvider

Class AuthTokenProvider @package Smartling\AuthApi

Hierarchy

Expanded class hierarchy of AuthTokenProvider

9 files declare their use of AuthTokenProvider
AuditLogApiFunctionalTest.php in vendor/smartling/api-sdk-php/tests/functional/AuditLogApiFunctionalTest.php
AuthApiTest.php in vendor/smartling/api-sdk-php/tests/unit/AuthApiTest.php
ContextApiFunctionalTest.php in vendor/smartling/api-sdk-php/tests/functional/ContextApiFunctionalTest.php
FileApiFunctionalTest.php in vendor/smartling/api-sdk-php/tests/functional/FileApiFunctionalTest.php
JobsApiFunctionalTest.php in vendor/smartling/api-sdk-php/tests/functional/JobsApiFunctionalTest.php

... See full list

File

vendor/smartling/api-sdk-php/src/AuthApi/AuthTokenProvider.php, line 13

Namespace

Smartling\AuthApi
View source
class AuthTokenProvider extends BaseApiAbstract implements AuthApiInterface {
  const ENDPOINT_URL = 'https://api.smartling.com/auth-api/v2';
  const RESPONSE_KEY_ACCESS_TOKEN = 'accessToken';
  const RESPONSE_KEY_ACCESS_TOKEN_TTL = 'expiresIn';
  const RESPONSE_KEY_REFRESH_TOKEN = 'refreshToken';
  const RESPONSE_KEY_REFRESH_TOKEN_TTL = 'refreshExpiresIn';
  const RESPONSE_KEY_TOKEN_TYPE = 'tokenType';
  const TTL_CORRECTION_TIME_SEC = 10;

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

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

  /**
   * @var array
   */
  private $data = [];

  /**
   * @var int
   */
  private $requestTimestamp = 0;

  /**
   * @return string
   */
  private function getUserIdentifier() {
    return $this->userIdentifier;
  }

  /**
   * @param string $userIdentifier
   */
  private function setUserIdentifier($userIdentifier) {
    $this->userIdentifier = $userIdentifier;
  }

  /**
   * @return string
   */
  private function getSecretKey() {
    return $this->secretKey;
  }

  /**
   * @param string $secretKey
   */
  private function setSecretKey($secretKey) {
    $this->secretKey = $secretKey;
  }

  /**
   * AuthTokenProvider constructor.
   *
   * @param string $userIdentifier
   * @param string $secretKey
   * @param ClientInterface $client
   * @param LoggerInterface $logger
   */
  public function __construct($userIdentifier, $secretKey, ClientInterface $client, $logger = null) {
    parent::__construct('', $client, $logger, self::ENDPOINT_URL);
    $this
      ->setUserIdentifier($userIdentifier);
    $this
      ->setSecretKey($secretKey);
  }

  /**
   * Creates and returns instance of AuthTokenProvider
   *
   * @param string $userIdentifier
   * @param string $secretKey
   * @param LoggerInterface $logger
   *
   * @return AuthTokenProvider
   */
  public static function create($userIdentifier, $secretKey, $logger = null) {
    $client = self::initializeHttpClient(self::ENDPOINT_URL);
    return new self($userIdentifier, $secretKey, $client, $logger);
  }

  /**
   * @inheritdoc
   */
  public function getAccessToken() {
    if (!$this
      ->tokenExists()) {
      $this->requestTimestamp = time();
      $this->data = $this
        ->authenticate();
    }
    elseif ($this
      ->tokenExpired()) {
      $this->data = $this
        ->refreshToken();
    }
    return $this->data[self::RESPONSE_KEY_ACCESS_TOKEN];
  }

  /**
   * Checks if token exists
   */
  private function tokenExists() {
    return is_array($this->data) && array_key_exists(self::RESPONSE_KEY_ACCESS_TOKEN, $this->data);
  }

  /**
   * Checks if token is expired
   */
  private function tokenExpired() {
    $tokenExpirationTime = $this->requestTimestamp + $this->data[static::RESPONSE_KEY_ACCESS_TOKEN_TTL] - static::TTL_CORRECTION_TIME_SEC;
    return $this
      ->tokenExists() && time() > $tokenExpirationTime;
  }

  /**
   * Sends /authenticate request
   */
  private function authenticate() {
    $this->requestTimestamp = time();
    $requestData = $this
      ->getDefaultRequestData('json', [
      'userIdentifier' => $this
        ->getUserIdentifier(),
      'userSecret' => $this
        ->getSecretKey(),
    ], false);
    return $this
      ->sendRequest('authenticate', $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Renews tokens
   */
  private function refreshToken() {
    if ($this
      ->tokenExists() && $this
      ->tokenCanBeRenewed()) {
      $requestData = $this
        ->getDefaultRequestData('json', [
        'refreshToken' => $this->data[self::RESPONSE_KEY_REFRESH_TOKEN],
      ], false);
      $this->requestTimestamp = time();
      return $this
        ->sendRequest('authenticate/refresh', $requestData, self::HTTP_METHOD_POST);
    }
    else {
      return $this
        ->authenticate();
    }
  }

  /**
   * Checks if token can be renewed
   */
  private function tokenCanBeRenewed() {
    return $this
      ->tokenExists() && time() < $this->requestTimestamp + $this->data[self::RESPONSE_KEY_REFRESH_TOKEN_TTL];
  }

  /**
   * @inheritdoc
   */
  public function getTokenType() {
    return $this
      ->tokenExists() ? $this->data[self::RESPONSE_KEY_TOKEN_TYPE] : '';
  }

  /**
   * @inheritdoc
   */
  public function resetToken() {
    $this->data = [];
    $this->requestTimestamp = 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthTokenProvider::$data private property
AuthTokenProvider::$requestTimestamp private property
AuthTokenProvider::$secretKey private property
AuthTokenProvider::$userIdentifier private property
AuthTokenProvider::authenticate private function Sends /authenticate request
AuthTokenProvider::create public static function Creates and returns instance of AuthTokenProvider
AuthTokenProvider::ENDPOINT_URL constant
AuthTokenProvider::getAccessToken public function @inheritdoc Overrides AuthApiInterface::getAccessToken
AuthTokenProvider::getSecretKey private function
AuthTokenProvider::getTokenType public function @inheritdoc Overrides AuthApiInterface::getTokenType
AuthTokenProvider::getUserIdentifier private function
AuthTokenProvider::refreshToken private function Renews tokens
AuthTokenProvider::resetToken public function @inheritdoc Overrides AuthApiInterface::resetToken
AuthTokenProvider::RESPONSE_KEY_ACCESS_TOKEN constant
AuthTokenProvider::RESPONSE_KEY_ACCESS_TOKEN_TTL constant
AuthTokenProvider::RESPONSE_KEY_REFRESH_TOKEN constant
AuthTokenProvider::RESPONSE_KEY_REFRESH_TOKEN_TTL constant
AuthTokenProvider::RESPONSE_KEY_TOKEN_TYPE constant
AuthTokenProvider::setSecretKey private function
AuthTokenProvider::setUserIdentifier private function
AuthTokenProvider::tokenCanBeRenewed private function Checks if token can be renewed
AuthTokenProvider::tokenExists private function Checks if token exists
AuthTokenProvider::tokenExpired private function Checks if token is expired
AuthTokenProvider::TTL_CORRECTION_TIME_SEC constant
AuthTokenProvider::__construct public function AuthTokenProvider constructor. Overrides BaseApiAbstract::__construct
BaseApiAbstract::$auth private property
BaseApiAbstract::$baseUrl private property Smartling API base url.
BaseApiAbstract::$currentClientId private static property
BaseApiAbstract::$currentClientUserAgentExtension private static property
BaseApiAbstract::$currentClientVersion private static property
BaseApiAbstract::$httpClient private property Http Client abstraction.
BaseApiAbstract::$logger private property Logger.
BaseApiAbstract::$projectId private property Project Id in Smartling dashboard
BaseApiAbstract::checkAuthenticationError private function
BaseApiAbstract::CLIENT_LIB_ID_SDK constant
BaseApiAbstract::CLIENT_LIB_ID_VERSION constant
BaseApiAbstract::CLIENT_USER_AGENT_EXTENSION constant
BaseApiAbstract::getAuth protected function
BaseApiAbstract::getBaseUrl protected function
BaseApiAbstract::getCurrentClientId public static function
BaseApiAbstract::getCurrentClientUserAgentExtension public static function
BaseApiAbstract::getCurrentClientVersion public static function
BaseApiAbstract::getDefaultRequestData protected function 1
BaseApiAbstract::getHttpClient protected function
BaseApiAbstract::getLogger protected function
BaseApiAbstract::getProjectId protected function
BaseApiAbstract::HTTP_METHOD_DELETE constant
BaseApiAbstract::HTTP_METHOD_GET constant
BaseApiAbstract::HTTP_METHOD_POST constant
BaseApiAbstract::HTTP_METHOD_PUT constant
BaseApiAbstract::initializeHttpClient protected static function
BaseApiAbstract::normalizeUri private function
BaseApiAbstract::PATTERN_DATE_TIME_ISO_8601 constant PHP equivalent to 'YYYY-MM-DDThh:mm:ssZ'
BaseApiAbstract::processBodyOptions protected function 3
BaseApiAbstract::processError private function
BaseApiAbstract::processErrors private function
BaseApiAbstract::readFile protected function OOP wrapper for fopen() function.
BaseApiAbstract::sendRequest protected function
BaseApiAbstract::setAuth protected function
BaseApiAbstract::setBaseUrl protected function
BaseApiAbstract::setCurrentClientId public static function
BaseApiAbstract::setCurrentClientUserAgentExtension public static function
BaseApiAbstract::setCurrentClientVersion public static function
BaseApiAbstract::setHttpClient protected function
BaseApiAbstract::setLogger protected function
BaseApiAbstract::setProjectId protected function