ILIAS  release_8 Revision v8.24
ILIAS\LTI\ToolProvider\AccessToken Class Reference

Class to represent an HTTP message. More...

+ Collaboration diagram for ILIAS\LTI\ToolProvider\AccessToken:

Public Member Functions

 __construct (Platform $platform, array $scopes=null, string $token=null, int $expires=null)
 Class constructor. More...
 
 getPlatform ()
 Get platform. More...
 
 load ()
 Load a nonce value from the database. More...
 
 save ()
 Save a nonce value in the database. More...
 
 hasScope (string $scope='')
 Check if a valid access token exists for a specific scope (or any scope if none specified). More...
 
 get (string $scope='', bool $scopeOnly=false)
 Obtain a valid access token for a scope. More...
 

Data Fields

string $token = null
 Access token string. More...
 
int $expires = null
 Timestamp at which the token string expires. More...
 
array $scopes = array()
 Scope(s) for which the access token is valid. More...
 
int $created = null
 Timestamp for when the object was created. More...
 
int $updated = null
 Timestamp for when the object was last updated. More...
 

Private Attributes

Platform $platform = null
 Platform for this context. More...
 

Detailed Description

Class to represent an HTTP message.

Author
Stephen P Vickers steph.nosp@m.en@s.nosp@m.pvsof.nosp@m.twar.nosp@m.eprod.nosp@m.ucts.nosp@m..com
Version
3.0.0 @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3

Definition at line 32 of file AccessToken.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\LTI\ToolProvider\AccessToken::__construct ( Platform  $platform,
array  $scopes = null,
string  $token = null,
int  $expires = null 
)

Class constructor.

Parameters
Platform$platformPlatform
array | null$scopesScopes for which the access token is valid
string | null$tokenAccess token string
int | null$expiresTime in seconds after which the token string will expire //UK: changed datetime to int

Definition at line 83 of file AccessToken.php.

84 {
85 $this->platform = $platform;
86 $this->scopes = $scopes;
87 if (!empty($token)) {
88 $this->token = $token;
89 }
90 if (!empty($expires)) {
91 $this->expires = time() + $expires;
92 }
93 $this->created = null;
94 $this->updated = null;
95 if (empty($scopes)) {
96 $this->load();
97 }
98 }
string $token
Access token string.
Definition: AccessToken.php:39
array $scopes
Scope(s) for which the access token is valid.
Definition: AccessToken.php:53
load()
Load a nonce value from the database.
Platform $platform
Platform for this context.
Definition: AccessToken.php:60
int $expires
Timestamp at which the token string expires.
Definition: AccessToken.php:46

References ILIAS\LTI\ToolProvider\AccessToken\$expires, ILIAS\LTI\ToolProvider\AccessToken\$platform, ILIAS\LTI\ToolProvider\AccessToken\$scopes, ILIAS\LTI\ToolProvider\AccessToken\$token, and ILIAS\LTI\ToolProvider\AccessToken\load().

+ Here is the call graph for this function:

Member Function Documentation

◆ get()

ILIAS\LTI\ToolProvider\AccessToken::get ( string  $scope = '',
bool  $scopeOnly = false 
)

Obtain a valid access token for a scope.

Parameters
string$scopeAccess scope
bool$scopeOnlyIf true, a token is requested just for the specified scope
Returns
AccessToken New access token

Definition at line 153 of file AccessToken.php.

153 : AccessToken
154 {
155 $url = $this->platform->accessTokenUrl;
156 if (!empty($url) && !empty(Tool::$defaultTool) && !empty(Tool::$defaultTool->rsaKey)) {
157 if ($scopeOnly) {
158 $scopesRequested = array($scope);
159 } else {
160 $scopesRequested = Tool::$defaultTool->requiredScopes;
161 if (substr($scope, -9) === '.readonly') {
162 $scope2 = substr($scope, 0, -9);
163 } else {
164 $scope2 = $scope;
165 }
166 if (!empty($scope) && !in_array($scope, $scopesRequested) && !in_array($scope2, $scopesRequested)) {
167 $scopesRequested[] = $scope;
168 }
169 }
170 if (!empty($scopesRequested)) {
171 $retry = false;
172 do {
173 $method = 'POST';
174 $type = 'application/x-www-form-urlencoded';
175 $body = array(
176 'grant_type' => 'client_credentials',
177 'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
178 'scope' => implode(' ', $scopesRequested)
179 );
180 if (!empty(Tool::$defaultTool)) {
182 $body = Tool::$defaultTool->signServiceRequest($url, $method, $type, $body);
183 } else {
184 $body = $this->platform->signServiceRequest($url, $method, $type, $body);
185 }
186 $http = new HttpMessage($url, $method, $body);
187 if ($http->send() && !empty($http->response)) {
188 $http->responseJson = json_decode($http->response);
189 if (!is_null($http->responseJson) && !empty($http->responseJson->access_token) && !empty($http->responseJson->expires_in)) {
190 if (isset($http->responseJson->scope)) {
191 $scopesAccepted = explode(' ', $http->responseJson->scope);
192 } else {
193 $scopesAccepted = $scopesRequested;
194 }
195 $this->scopes = $scopesAccepted;
196 $this->token = $http->responseJson->access_token;
197 $this->expires = time() + $http->responseJson->expires_in;
198 if (!$scopeOnly) {
199 $this->save();
200 }
201 }
202 $retry = false;
203 } elseif ($retry) {
204 $retry = false;
205 } elseif (!empty($scope) && (count($scopesRequested) > 1)) { // Just ask for the single scope requested
206 $retry = true;
207 $scopesRequested = array($scope);
208 }
209 } while ($retry);
210 }
211 } else {
212 $this->scopes = null;
213 $this->token = null;
214 $this->expires = null;
215 $this->created = null;
216 $this->updated = null;
217 }
218
219 return $this;
220 }
save()
Save a nonce value in the database.
static Tool $defaultTool
Default tool for use with service requests.
Definition: Tool.php:299
$scope
Definition: ltiregstart.php:53
$type
$url
$http
Definition: raiseError.php:7

References ILIAS\LTI\ToolProvider\Tool\$defaultTool, $http, ILIAS\LTI\ToolProvider\AccessToken\$platform, $scope, $type, $url, and ILIAS\LTI\ToolProvider\AccessToken\save().

+ Here is the call graph for this function:

◆ getPlatform()

ILIAS\LTI\ToolProvider\AccessToken::getPlatform ( )

Get platform.

Returns
Platform Platform object for this resource link.

Definition at line 105 of file AccessToken.php.

105 : ?Platform
106 {
107 return $this->platform;
108 }
Class to represent a platform.
Definition: Platform.php:36

References ILIAS\LTI\ToolProvider\AccessToken\$platform.

◆ hasScope()

ILIAS\LTI\ToolProvider\AccessToken::hasScope ( string  $scope = '')

Check if a valid access token exists for a specific scope (or any scope if none specified).

Parameters
string$scopeAccess scope
Returns
bool True if there is an unexpired access token for specified scope

Definition at line 136 of file AccessToken.php.

136 : bool
137 {
138 if (substr($scope, -9) === '.readonly') {
139 $scope2 = substr($scope, 0, -9);
140 } else {
141 $scope2 = $scope;
142 }
143 return !empty($this->token) && (empty($this->expires) || ($this->expires > time())) &&
144 (empty($scope) || empty($this->scopes) || (in_array($scope, $this->scopes) || in_array($scope2, $this->scopes)));
145 }

References $scope.

◆ load()

ILIAS\LTI\ToolProvider\AccessToken::load ( )

Load a nonce value from the database.

Returns
bool True if the nonce value was successfully loaded

Definition at line 115 of file AccessToken.php.

115 : bool
116 {
117 return $this->platform->getDataConnector()->loadAccessToken($this);
118 }

Referenced by ILIAS\LTI\ToolProvider\AccessToken\__construct().

+ Here is the caller graph for this function:

◆ save()

ILIAS\LTI\ToolProvider\AccessToken::save ( )

Save a nonce value in the database.

Returns
bool True if the nonce value was successfully saved

Definition at line 125 of file AccessToken.php.

125 : bool
126 {
127 sort($this->scopes);
128 return $this->platform->getDataConnector()->saveAccessToken($this);
129 }

Referenced by ILIAS\LTI\ToolProvider\AccessToken\get().

+ Here is the caller graph for this function:

Field Documentation

◆ $created

int null ILIAS\LTI\ToolProvider\AccessToken::$created = null

Timestamp for when the object was created.

Definition at line 67 of file AccessToken.php.

◆ $expires

int null ILIAS\LTI\ToolProvider\AccessToken::$expires = null

Timestamp at which the token string expires.

Definition at line 46 of file AccessToken.php.

Referenced by ILIAS\LTI\ToolProvider\AccessToken\__construct().

◆ $platform

Platform null ILIAS\LTI\ToolProvider\AccessToken::$platform = null
private

◆ $scopes

array ILIAS\LTI\ToolProvider\AccessToken::$scopes = array()

Scope(s) for which the access token is valid.

Definition at line 53 of file AccessToken.php.

Referenced by ILIAS\LTI\ToolProvider\AccessToken\__construct().

◆ $token

string null ILIAS\LTI\ToolProvider\AccessToken::$token = null

Access token string.

Definition at line 39 of file AccessToken.php.

Referenced by ILIAS\LTI\ToolProvider\AccessToken\__construct().

◆ $updated

int null ILIAS\LTI\ToolProvider\AccessToken::$updated = null

Timestamp for when the object was last updated.

Definition at line 74 of file AccessToken.php.


The documentation for this class was generated from the following file: