ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ltitoken.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use Firebase\JWT\JWT;
24use Firebase\JWT\JWK;
25
26require_once("../vendor/composer/vendor/autoload.php");
27
30
31global $DIC;
32
33ilObjLTIConsumer::getLogger()->debug("accesstoken request");
34
36
39}
40
41if (strtoupper($DIC->http()->request()->getMethod()) !== "POST") {
42 invalidRequest("wrong method " . $DIC->http()->request()->getMethod());
43}
44
45$params = $DIC->http()->wrapper()->query();
46$post = $DIC->http()->wrapper()->post();
47
48if (!$post->has('client_assertion') || !$post->has('client_assertion_type') || !$post->has('grant_type') || !$post->has('scope')) {
49 invalidRequest("bad request: " . var_export($params, true) . "\n" . var_export($post, true));
50}
51
52$clientAssertion = $post->retrieve('client_assertion', $DIC->refinery()->kindlyTo()->string());
53$clientAssertionType = $post->retrieve('client_assertion_type', $DIC->refinery()->kindlyTo()->string());
54$grantType = $post->retrieve('grant_type', $DIC->refinery()->kindlyTo()->string());
55$scope = $post->retrieve('scope', $DIC->refinery()->kindlyTo()->string());
56
57if ($clientAssertionType != 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer' || $grantType != 'client_credentials') {
58 invalidRequest("bad request: unsupported grant_type: " . $grantType);
59}
60
61$parts = explode('.', $clientAssertion);
62
63if (count($parts) != 3) {
64 invalidRequest("bad request: " . var_export($parts, true));
65}
66
67$payload = JWT::urlsafeB64Decode($parts[1]);
68$claims = json_decode($payload, true);
69
70if ($claims == null) {
71 invalidRequest("bad request: no claims");
72}
73
75if (empty($clientId)) {
76 invalidRequest("bad request: no claims");
77}
78
80$provider = null;
81
82try {
84} catch (IOException $e) {
85 invalidRequest(var_export($e, true));
86}
87
88try {
90} catch (IOException $e) {
91 serverError(var_export($e, true));
92}
93
95
96$scopes = array();
97// ToDo: support for other services
99$requestedscopes = explode(' ', $scope);
100$scopes = array_intersect($requestedscopes, $gradeService->getPermittedScopes());
101
102if (empty($scopes)) {
103 invalidRequest("empty scopes");
104}
105
106sendAccessToken(implode(" ", $scopes), $provider);
107
109{
110 try {
111 ilObjLTIConsumer::getLogger()->debug("validateServiceToken");
112 // ToDo: caching
113 $jwks = file_get_contents($provider->getPublicKeyset());
114 $keyset = json_decode($jwks, true);
115 $keys = JWK::parseKeySet($keyset);
116 $data = JWT::decode($token, $keys);
117 //ilObjLTIConsumer::getLogger()->debug(var_export($data, TRUE));
118 if ($provider->getClientId() != $data->iss || $provider->getClientId() != $data->sub) {
119 invalidRequest("invalid clientId");
120 }
121 } catch (Exception $e) {
122 serverError(var_export($e, true));
123 }
124}
125
127{
128 ilObjLTIConsumer::getLogger()->debug("sendAccesToken");
129 $now = time();
130 $token = [
131 "sub" => $provider->getClientId(),
132 "iat" => $now,
133 "exp" => $now + 3600,
134 "imsglobal.org.security.scope" => $scopes
135 ];
136 try {
138 $accessToken = JWT::encode($token, $privateKey['key'], 'RS256', $privateKey['kid']);
139 $responseData = array(
140 'access_token' => $accessToken,
141 'token_type' => 'bearer',
142 'expires_in' => 3600,
143 'scope' => $scopes
144 );
146 } catch (Exception $e) {
147 serverError(var_export($e, true));
148 }
149}
150
151function serverError(string $log = ""): void
152{
153 if (!empty($log)) {
155 }
156 ilObjLTIConsumer::sendResponseError(500, json_encode(array('error' => "ERROR_OPEN_SSL_CONF")));
157}
158
159function invalidRequest(string $log = ""): void
160{
161 if (!empty($log)) {
163 }
164 ilObjLTIConsumer::sendResponseError(400, json_encode(array('error' => 'invalid_request')));
165}
Indicates general problems with the input or output operations.
Definition: IOException.php:28
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:66
$log
Definition: ltiresult.php:34
sendAccessToken(string $scopes, ilLTIConsumeProvider $provider)
Definition: ltitoken.php:126
$clientAssertionType
Definition: ltitoken.php:53
$claims
Definition: ltitoken.php:68
$scope
Definition: ltitoken.php:55
if(!empty(ilObjLTIConsumer::verifyPrivateKey())) if(strtoupper($DIC->http() ->request() ->getMethod()) !=="POST") $params
Definition: ltitoken.php:45
global $DIC
Definition: ltitoken.php:31
$post
Definition: ltitoken.php:46
invalidRequest(string $log="")
Definition: ltitoken.php:159
$grantType
Definition: ltitoken.php:54
if(! $post->has('client_assertion')||! $post->has('client_assertion_type')||! $post->has('grant_type')||! $post->has('scope')) $clientAssertion
Definition: ltitoken.php:52
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
$requestedscopes
Definition: ltitoken.php:99
$scopes
Definition: ltitoken.php:96
$provider
Definition: ltitoken.php:80
validateServiceToken(string $token, ilLTIConsumeProvider $provider)
Definition: ltitoken.php:108
if($claims==null) $clientId
Definition: ltitoken.php:74
$gradeService
Definition: ltitoken.php:35
serverError(string $log="")
Definition: ltitoken.php:151
if(count($parts) !=3) $payload
Definition: ltitoken.php:67
if(empty($clientId)) $providerId
Definition: ltitoken.php:79
$token
Definition: xapitoken.php:70