AuthTokenProvider.php in TMGMT Translator Smartling 8.4
File
vendor/smartling/api-sdk-php/src/AuthApi/AuthTokenProvider.php
View source
<?php
namespace Smartling\AuthApi;
use GuzzleHttp\ClientInterface;
use Psr\Log\LoggerInterface;
use Smartling\BaseApiAbstract;
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;
private $userIdentifier;
private $secretKey;
private $data = [];
private $requestTimestamp = 0;
private function getUserIdentifier() {
return $this->userIdentifier;
}
private function setUserIdentifier($userIdentifier) {
$this->userIdentifier = $userIdentifier;
}
private function getSecretKey() {
return $this->secretKey;
}
private function setSecretKey($secretKey) {
$this->secretKey = $secretKey;
}
public function __construct($userIdentifier, $secretKey, ClientInterface $client, $logger = null) {
parent::__construct('', $client, $logger, self::ENDPOINT_URL);
$this
->setUserIdentifier($userIdentifier);
$this
->setSecretKey($secretKey);
}
public static function create($userIdentifier, $secretKey, $logger = null) {
$client = self::initializeHttpClient(self::ENDPOINT_URL);
return new self($userIdentifier, $secretKey, $client, $logger);
}
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];
}
private function tokenExists() {
return is_array($this->data) && array_key_exists(self::RESPONSE_KEY_ACCESS_TOKEN, $this->data);
}
private function tokenExpired() {
$tokenExpirationTime = $this->requestTimestamp + $this->data[static::RESPONSE_KEY_ACCESS_TOKEN_TTL] - static::TTL_CORRECTION_TIME_SEC;
return $this
->tokenExists() && time() > $tokenExpirationTime;
}
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);
}
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();
}
}
private function tokenCanBeRenewed() {
return $this
->tokenExists() && time() < $this->requestTimestamp + $this->data[self::RESPONSE_KEY_REFRESH_TOKEN_TTL];
}
public function getTokenType() {
return $this
->tokenExists() ? $this->data[self::RESPONSE_KEY_TOKEN_TYPE] : '';
}
public function resetToken() {
$this->data = [];
$this->requestTimestamp = 0;
}
}