class AuthTokenProvider in TMGMT Translator Smartling 8.2
Same name in this branch
- 8.2 api-sdk-php/src/AuthApi/AuthTokenProvider.php \Smartling\AuthApi\AuthTokenProvider
- 8.2 vendor/smartling/api-sdk-php/src/AuthApi/AuthTokenProvider.php \Smartling\AuthApi\AuthTokenProvider
Same name and namespace in other branches
- 8.4 vendor/smartling/api-sdk-php/src/AuthApi/AuthTokenProvider.php \Smartling\AuthApi\AuthTokenProvider
- 8.3 vendor/smartling/api-sdk-php/src/AuthApi/AuthTokenProvider.php \Smartling\AuthApi\AuthTokenProvider
Class AuthTokenProvider @package Smartling\AuthApi
Hierarchy
- class \Smartling\AuthApi\AuthTokenProvider extends \Smartling\BaseApiAbstract implements \Smartling\AuthApi\AuthApiInterface
Expanded class hierarchy of AuthTokenProvider
10 files declare their use of AuthTokenProvider
- AuthApiTest.php in api-sdk-php/
tests/ unit/ AuthApiTest.php - AuthApiTest.php in vendor/
smartling/ api-sdk-php/ tests/ unit/ AuthApiTest.php - BufferLogger.php in src/
Logger/ BufferLogger.php - ContextApiFunctionalTest.php in api-sdk-php/
tests/ functional/ ContextApiFunctionalTest.php - ContextApiFunctionalTest.php in vendor/
smartling/ api-sdk-php/ tests/ functional/ ContextApiFunctionalTest.php
File
- api-sdk-php/
src/ AuthApi/ AuthTokenProvider.php, line 13
Namespace
Smartling\AuthApiView 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';
/**
* @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);
}
/**
* 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() {
return $this
->tokenExists() && time() > $this->requestTimestamp + $this->data[self::RESPONSE_KEY_ACCESS_TOKEN_TTL];
}
/**
* 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 tokenRenew() {
if ($this
->tokenExists() && $this
->tokenCanBeRenewed()) {
$requestData = $this
->getDefaultRequestData('json', [
'refreshToken' => $this->data[self::RESPONSE_KEY_REFRESH_TOKEN],
], false);
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 getAccessToken() {
if (!$this
->tokenExists()) {
$this->requestTimestamp = time();
$this->data = $this
->authenticate();
}
if ($this
->tokenExpired()) {
$this
->tokenRenew();
}
return $this->data[self::RESPONSE_KEY_ACCESS_TOKEN];
}
/**
* @inheritdoc
*/
public function getTokenType() {
return $this
->tokenExists() ? $this->data[self::RESPONSE_KEY_TOKEN_TYPE] : '';
}
/**
* @inheritdoc
*/
public function resetToken() {
$this->data = [];
}
}