ILIAS  release_8 Revision v8.24
ltitoken.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
26
27chdir("../../");
28
29require_once("Services/Init/classes/class.ilInitialisation.php");
30
33
34global $DIC;
35
36ilObjLTIConsumer::getLogger()->debug("accesstoken request");
37
39
42}
43
44if (strtoupper($DIC->http()->request()->getMethod()) !== "POST") {
45 invalidRequest("wrong method " . $DIC->http()->request()->getMethod());
46}
47
48$params = $DIC->http()->wrapper()->query();
49$post = $DIC->http()->wrapper()->post();
50
51if (!$post->has('client_assertion') || !$post->has('client_assertion_type') || !$post->has('grant_type') || !$post->has('scope')) {
52 invalidRequest("bad request: " . var_export($params, true) . "\n" . var_export($post, true));
53}
54
55$clientAssertion = $post->retrieve('client_assertion', $DIC->refinery()->kindlyTo()->string());
56$clientAssertionType = $post->retrieve('client_assertion_type', $DIC->refinery()->kindlyTo()->string());
57$grantType = $post->retrieve('grant_type', $DIC->refinery()->kindlyTo()->string());
58$scope = $post->retrieve('scope', $DIC->refinery()->kindlyTo()->string());
59
60if ($clientAssertionType != 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer' || $grantType != 'client_credentials') {
61 invalidRequest("bad request: unsupported grant_type: " . $grantType);
62}
63
64$parts = explode('.', $clientAssertion);
65
66if (count($parts) != 3) {
67 invalidRequest("bad request: " . var_export($parts, true));
68}
69
70$payload = JWT::urlsafeB64Decode($parts[1]);
71$claims = json_decode($payload, true);
72
73if ($claims == null) {
74 invalidRequest("bad request: no claims");
75}
76
78if (empty($clientId)) {
79 invalidRequest("bad request: no claims");
80}
81
83$provider = null;
84
85try {
87} catch (IOException $e) {
88 invalidRequest(var_export($e, true));
89}
90
91try {
93} catch (IOException $e) {
94 serverError(var_export($e, true));
95}
96
98
99$scopes = array();
100// ToDo: support for other services
102$requestedscopes = explode(' ', $scope);
103$scopes = array_intersect($requestedscopes, $gradeService->getPermittedScopes());
104
105if (empty($scopes)) {
106 invalidRequest("empty scopes");
107}
108
109sendAccessToken(implode(" ", $scopes), $provider);
110
112{
113 try {
114 ilObjLTIConsumer::getLogger()->debug("validateServiceToken");
115 // ToDo: caching
116 $jwks = file_get_contents($provider->getPublicKeyset());
117 $keyset = json_decode($jwks, true);
118 $keys = JWK::parseKeySet($keyset);
119 $data = JWT::decode($token, $keys);
120 //ilObjLTIConsumer::getLogger()->debug(var_export($data, TRUE));
121 if ($provider->getClientId() != $data->iss || $provider->getClientId() != $data->sub) {
122 invalidRequest("invalid clientId");
123 }
124 } catch (Exception $e) {
125 serverError(var_export($e, true));
126 }
127}
128
130{
131 ilObjLTIConsumer::getLogger()->debug("sendAccesToken");
132 $now = time();
133 $token = [
134 "sub" => $provider->getClientId(),
135 "iat" => $now,
136 "exp" => $now + 3600,
137 "imsglobal.org.security.scope" => $scopes
138 ];
139 try {
141 $accessToken = JWT::encode($token, $privateKey['key'], 'RS256', $privateKey['kid']);
142 $responseData = array(
143 'access_token' => $accessToken,
144 'token_type' => 'baerer',
145 'expires_in' => 3600,
146 'scope' => $scopes
147 );
149 } catch (Exception $e) {
150 serverError(var_export($e, true));
151 }
152}
153
154function serverError(string $log = ""): void
155{
156 if (!empty($log)) {
158 }
159 ilObjLTIConsumer::sendResponseError(500, json_encode(array('error' => "ERROR_OPEN_SSL_CONF")));
160}
161
162function invalidRequest(string $log = ""): void
163{
164 if (!empty($log)) {
166 }
167 ilObjLTIConsumer::sendResponseError(400, json_encode(array('error' => 'invalid_request')));
168}
const CONTEXT_SCORM
static init(string $a_type)
Init context by type.
static initILIAS()
ilias initialisation
static getProviderIdFromClientId(string $clientId)
static sendResponseError(int $code, string $message, $log=true)
static sendResponseJson(array $obj)
$responseData
$privateKey
Definition: ltiregstart.php:68
sendAccessToken(string $scopes, ilLTIConsumeProvider $provider)
Definition: ltitoken.php:129
$clientAssertionType
Definition: ltitoken.php:56
$claims
Definition: ltitoken.php:71
$scope
Definition: ltitoken.php:58
if(!empty(ilObjLTIConsumer::verifyPrivateKey())) if(strtoupper($DIC->http() ->request() ->getMethod()) !=="POST") $params
Definition: ltitoken.php:48
global $DIC
Definition: ltitoken.php:34
$post
Definition: ltitoken.php:49
invalidRequest(string $log="")
Definition: ltitoken.php:162
$grantType
Definition: ltitoken.php:57
if(! $post->has('client_assertion')||! $post->has('client_assertion_type')||! $post->has('grant_type')||! $post->has('scope')) $clientAssertion
Definition: ltitoken.php:55
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
$requestedscopes
Definition: ltitoken.php:102
$scopes
Definition: ltitoken.php:99
$provider
Definition: ltitoken.php:83
validateServiceToken(string $token, ilLTIConsumeProvider $provider)
Definition: ltitoken.php:111
if($claims==null) $clientId
Definition: ltitoken.php:77
$gradeService
Definition: ltitoken.php:38
serverError(string $log="")
Definition: ltitoken.php:154
if(count($parts) !=3) $payload
Definition: ltitoken.php:70
if(empty($clientId)) $providerId
Definition: ltitoken.php:82
$keys
Definition: metadata.php:204
$log
Definition: result.php:33
$token
Definition: xapitoken.php:70