-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.php
More file actions
248 lines (215 loc) · 7.69 KB
/
Copy pathutil.php
File metadata and controls
248 lines (215 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
use \Tsugi\Core\Keyset;
use \Tsugi\Util\LTI13;
use \Tsugi\Util\Net;
use \Tsugi\Util\U;
/** IMS LTI scope — known working for bearer-probe MVP. */
function sakai_scopes_lti_lineitem() {
return array(
"https://purl.imsglobal.org/spec/lti-ags/scope/lineitem",
);
}
/** Sakai-native scope (content.read permission); expected to fail until granted in Sakai. */
function sakai_scopes_content_read() {
return array(
"sakai.lti.api.content.read",
);
}
/**
* Resolve scopes for a probe run. Preset names: lti, sakai.
* Setting sakai_token_scope still overrides any preset.
*/
function sakai_probe_scopes($launch, $preset = 'lti') {
$override = $launch->settingsCascade('sakai_token_scope', null);
if ( is_string($override) && U::strlen(trim($override)) > 0 ) {
return preg_split('/\s+/', trim($override));
}
if ( $preset === 'sakai' ) {
return sakai_scopes_content_read();
}
return sakai_scopes_lti_lineitem();
}
function sakai_probe_preset_label($preset) {
if ( $preset === 'sakai' ) {
return 'Sakai scope (sakai.lti.api.content.read)';
}
return 'LTI scope (lti-ags/lineitem)';
}
/**
* Load LTI 1.3 token-request fields from the Tsugi session (same checks as Context).
*
* @return string Empty on success, otherwise a space-separated list of missing fields.
*/
function sakai_load_lti13_data($launch, &$lti13_token_url, &$privkey, &$kid,
&$lti13_token_audience, &$issuer_client, &$deployment_id) {
$success = Keyset::getSigning($privkey, $kid);
$lti13_token_url = $launch->ltiParameter('lti13_token_url');
$issuer_client = $launch->ltiParameter('issuer_client');
$lti13_token_audience = $launch->ltiParameter('lti13_token_audience');
$deployment_id = $launch->ltiParameter('deployment_id');
$missing = '';
if ( empty($issuer_client) ) $missing .= ' issuer_client';
if ( empty($privkey) ) $missing .= ' private_key';
if ( empty($kid) ) $missing .= ' public_key_kid';
if ( empty($lti13_token_url) ) $missing .= ' token_url';
return trim($missing);
}
/**
* Sakai server base URL from issuer_key (no path suffix).
*/
function sakai_server_root($launch) {
$iss = $launch->ltiParameter('issuer_key');
if ( ! is_string($iss) || U::strlen(trim($iss)) < 1 ) {
return false;
}
return rtrim(trim($iss), '/');
}
/**
* Sakai webapi context path (/api) from issuer URL or optional key setting.
*/
function sakai_api_root($launch) {
$override = $launch->settingsCascade('sakai_api_root', null);
if ( is_string($override) && U::strlen(trim($override)) > 0 ) {
return rtrim(trim($override), '/');
}
$root = sakai_server_root($launch);
if ( ! $root ) {
return false;
}
return $root . '/api';
}
/**
* Entity Broker context path (/direct) from issuer URL or optional key setting.
*/
function sakai_direct_root($launch) {
$override = $launch->settingsCascade('sakai_direct_root', null);
if ( is_string($override) && U::strlen(trim($override)) > 0 ) {
return rtrim(trim($override), '/');
}
$root = sakai_server_root($launch);
if ( ! $root ) {
return false;
}
return $root . '/direct';
}
function sakai_bearer_probe_url($launch) {
$root = sakai_api_root($launch);
if ( ! $root ) return false;
return $root . '/lti/bearer-probe';
}
function sakai_direct_bearer_probe_url($launch) {
$root = sakai_direct_root($launch);
if ( ! $root ) return false;
return $root . '/lti/bearer-probe';
}
/**
* Probe target: webapi ({@code api}) or Entity Broker ({@code direct}).
*/
function sakai_probe_target_url($launch, $target = 'api') {
if ( $target === 'direct' ) {
return sakai_direct_bearer_probe_url($launch);
}
return sakai_bearer_probe_url($launch);
}
function sakai_probe_target_label($target = 'api') {
if ( $target === 'direct' ) {
return '/direct';
}
return '/api';
}
/**
* GET a bearer-probe URL with the SAT.
*
* @return array probe_http_code, probe_body, probe_json
*/
function sakai_run_bearer_probe($probe_url, $access_token, &$debug_log) {
$result = array(
'probe_http_code' => false,
'probe_body' => false,
'probe_json' => false,
);
$header = "Authorization: Bearer " . $access_token . "\n"
. "Accept: application/json";
$debug_log[] = 'GET ' . $probe_url;
$debug_log[] = $header;
$probe_body = Net::doGet($probe_url, $header);
$result['probe_http_code'] = Net::getLastHttpResponse();
$result['probe_body'] = $probe_body;
if ( is_string($probe_body) && U::strlen($probe_body) > 0 ) {
$json = json_decode($probe_body);
if ( $json !== null ) {
$result['probe_json'] = $json;
}
}
return $result;
}
/**
* Obtain a Sakai Access Token (SAT) and call GET .../lti/bearer-probe on /api or /direct.
*
* @param string $preset Scope preset: lti, sakai
* @param string $target Probe target: api, direct
* @return array Keys: ok, missing, token_url, probe_url, probe_target, token_data, access_token,
* probe_http_code, probe_body, probe_json, debug_log
*/
function sakai_get_token_and_probe($launch, $preset = 'lti', $target = 'api') {
$debug_log = array();
$scopes = sakai_probe_scopes($launch, $preset);
$result = array(
'ok' => false,
'preset' => $preset,
'preset_label' => sakai_probe_preset_label($preset),
'probe_target' => $target,
'probe_target_label' => sakai_probe_target_label($target),
'scopes' => $scopes,
'missing' => '',
'token_url' => '',
'probe_url' => '',
'token_data' => false,
'access_token' => false,
'probe_http_code' => false,
'probe_body' => false,
'probe_json' => false,
'debug_log' => &$debug_log,
);
$missing = sakai_load_lti13_data($launch, $lti13_token_url, $privkey, $kid,
$lti13_token_audience, $issuer_client, $deployment_id);
$result['missing'] = $missing;
if ( U::strlen($missing) > 0 ) {
$debug_log[] = 'Missing LTI 1.3 session data:' . $missing;
return $result;
}
$probe_url = sakai_probe_target_url($launch, $target);
$result['token_url'] = $lti13_token_url;
$result['probe_url'] = $probe_url;
if ( ! $probe_url ) {
$debug_log[] = 'Could not determine Sakai ' . sakai_probe_target_label($target)
. ' base from issuer_key';
$result['missing'] = 'issuer_key';
return $result;
}
$debug_log[] = 'Requested scopes: ' . (is_array($scopes) ? implode(' ', $scopes) : $scopes);
$token_data = LTI13::get_access_token($scopes, $issuer_client, $lti13_token_url,
$privkey, $kid, $lti13_token_audience, $deployment_id, $debug_log);
$result['token_data'] = $token_data;
$access_token = LTI13::extract_access_token($token_data, $debug_log);
$result['access_token'] = $access_token;
if ( ! $access_token ) {
$debug_log[] = 'Token request did not return access_token';
return $result;
}
$probe = sakai_run_bearer_probe($probe_url, $access_token, $debug_log);
$result['probe_http_code'] = $probe['probe_http_code'];
$result['probe_body'] = $probe['probe_body'];
$result['probe_json'] = $probe['probe_json'];
$code = $result['probe_http_code'];
$result['ok'] = ($code == 200 && is_object($result['probe_json'])
&& isset($result['probe_json']->ok) && $result['probe_json']->ok);
return $result;
}
function sakai_print_debug_log($div, $debug_log) {
echo('<div id="'.$div.'"><pre>'."\n");
if ( is_array($debug_log) && count($debug_log) > 0 ) {
echo(htmlentities(\Tsugi\UI\Output::safe_print_r($debug_log), ENT_SUBSTITUTE));
}
echo("\n</pre></div>\n");
}