LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
AccessToken.php
1<?php
2
3namespace ceLTIc\LTI;
4
8
18{
19
25 public $token = null;
26
32 public $expires = null;
33
39 public $scopes = array();
40
46 private $platform = null;
47
53 public $created = null;
54
60 public $updated = null;
61
70 public function __construct($platform, $scopes = null, $token = null, $expires = null)
71 {
72 $this->platform = $platform;
73 $this->scopes = $scopes;
74 if (!empty($token)) {
75 $this->token = $token;
76 }
77 if (!empty($expires)) {
78 $this->expires = time() + $expires;
79 }
80 $this->created = null;
81 $this->updated = null;
82 if (empty($scopes)) {
83 $this->load();
84 }
85 }
86
92 public function getPlatform()
93 {
94 return $this->platform;
95 }
96
102 public function load()
103 {
104 return $this->platform->getDataConnector()->loadAccessToken($this);
105 }
106
112 public function save()
113 {
114 sort($this->scopes);
115 return $this->platform->getDataConnector()->saveAccessToken($this);
116 }
117
125 public function hasScope($scope = '')
126 {
127 if (substr($scope, -9) === '.readonly') {
128 $scope2 = substr($scope, 0, -9);
129 } else {
130 $scope2 = $scope;
131 }
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)));
134 }
135
144 public function get($scope = '', $scopeOnly = false)
145 {
146 $url = $this->platform->accessTokenUrl;
147 if (!empty($url) && !empty(Tool::$defaultTool) && !empty(Tool::$defaultTool->rsaKey)) {
148 if ($scopeOnly) {
149 $scopesRequested = array($scope);
150 } else {
151 $scopesRequested = Tool::$defaultTool->requiredScopes;
152 if (substr($scope, -9) === '.readonly') {
153 $scope2 = substr($scope, 0, -9);
154 } else {
155 $scope2 = $scope;
156 }
157 if (!empty($scope) && !in_array($scope, $scopesRequested) && !in_array($scope2, $scopesRequested)) {
158 $scopesRequested[] = $scope;
159 }
160 }
161 if (!empty($scopesRequested)) {
162 $retry = false;
163 do {
164 $method = 'POST';
165 $type = 'application/x-www-form-urlencoded';
166 $body = array(
167 'grant_type' => 'client_credentials',
168 'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
169 'scope' => implode(' ', $scopesRequested)
170 );
171 if (!empty(Tool::$defaultTool)) {
172 Tool::$defaultTool->platform = $this->platform;
173 $body = Tool::$defaultTool->signServiceRequest($url, $method, $type, $body);
174 } else {
175 $body = $this->platform->signServiceRequest($url, $method, $type, $body);
176 }
177 $http = new HttpMessage($url, $method, $body, 'Accept: application/json');
178 if ($http->send() && !empty($http->response)) {
179 $http->responseJson = Util::jsonDecode($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);
183 } else {
184 $scopesAccepted = $scopesRequested;
185 }
186 $this->scopes = $scopesAccepted;
187 $this->token = $http->responseJson->access_token;
188 $this->expires = time() + $http->responseJson->expires_in;
189 if (!$scopeOnly) {
190 $this->save();
191 }
192 }
193 $retry = false;
194 } elseif ($retry) {
195 $retry = false;
196 } elseif (!empty($scope) && (count($scopesRequested) > 1)) { // Just ask for the single scope requested
197 $retry = true;
198 $scopesRequested = array($scope);
199 }
200 } while ($retry);
201 }
202 } else {
203 $this->scopes = null;
204 $this->token = null;
205 $this->expires = null;
206 $this->created = null;
207 $this->updated = null;
208 }
209
210 return $this;
211 }
212
213}
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 represent an LTI Tool.
Definition Tool.php:24
static $defaultTool
Default tool for use with service requests.
Definition Tool.php:294
Class to implement utility methods.
Definition Util.php:15
static jsonDecode($str, $associative=false)
Decode a JSON string.
Definition Util.php:560