46 private $platform =
null;
72 $this->platform = $platform;
80 $this->created =
null;
81 $this->updated =
null;
94 return $this->platform;
104 return $this->platform->getDataConnector()->loadAccessToken($this);
115 return $this->platform->getDataConnector()->saveAccessToken($this);
127 if (substr($scope, -9) ===
'.readonly') {
128 $scope2 = substr($scope, 0, -9);
132 return !empty($this->token) && (empty($this->expires) || ($this->expires > time())) &&
133 (empty($scope) || empty($this->scopes) || (in_array($scope, $this->scopes) || in_array($scope2, $this->scopes)));
144 public function get($scope =
'', $scopeOnly =
false)
146 $url = $this->platform->accessTokenUrl;
149 $scopesRequested = array($scope);
152 if (substr($scope, -9) ===
'.readonly') {
153 $scope2 = substr($scope, 0, -9);
157 if (!empty($scope) && !in_array($scope, $scopesRequested) && !in_array($scope2, $scopesRequested)) {
158 $scopesRequested[] = $scope;
161 if (!empty($scopesRequested)) {
165 $type =
'application/x-www-form-urlencoded';
167 'grant_type' =>
'client_credentials',
168 'client_assertion_type' =>
'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
169 'scope' => implode(
' ', $scopesRequested)
175 $body = $this->platform->signServiceRequest($url, $method, $type, $body);
177 $http =
new HttpMessage($url, $method, $body,
'Accept: application/json');
178 if ($http->send() && !empty($http->response)) {
180 if (!is_null($http->responseJson) && !empty($http->responseJson->access_token) && !empty($http->responseJson->expires_in)) {
181 if (isset($http->responseJson->scope)) {
182 $scopesAccepted = explode(
' ', $http->responseJson->scope);
184 $scopesAccepted = $scopesRequested;
186 $this->scopes = $scopesAccepted;
187 $this->token = $http->responseJson->access_token;
188 $this->expires = time() + $http->responseJson->expires_in;
196 } elseif (!empty($scope) && (count($scopesRequested) > 1)) {
198 $scopesRequested = array($scope);
203 $this->scopes =
null;
205 $this->expires =
null;
206 $this->created =
null;
207 $this->updated =
null;
Class to represent an HTTP message.
$expires
Timestamp at which the token string expires.
$token
Access token string.
load()
Load a nonce value from the database.
$updated
Timestamp for when the object was last updated.
__construct($platform, $scopes=null, $token=null, $expires=null)
Class constructor.
$scopes
Scope(s) for which the access token is valid.
$created
Timestamp for when the object was created.
hasScope($scope='')
Check if a valid access token exists for a specific scope (or any scope if none specified).
getPlatform()
Get platform.
save()
Save a nonce value in the database.
Class to represent an HTTP message request.
Class to implement utility methods.
static jsonDecode($str, $associative=false)
Decode a JSON string.