aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/LTI13a/Registration.php
blob: 2ce1e8dd07dbda6c624d1cd95151301c3e696929 (plain)
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
<?php

namespace Studip\LTI13a;

use OAT\Library\Lti1p3Core\Exception\LtiException;
use OAT\Library\Lti1p3Core\Registration\RegistrationInterface;
use OAT\Library\Lti1p3Core\Tool\ToolInterface;
use OAT\Library\Lti1p3Core\Platform\PlatformInterface;
use OAT\Library\Lti1p3Core\Security\Key\KeyChainInterface;

class Registration implements RegistrationInterface
{
    /**
     * @var \LtiTool|null The LTI tool for the registration instance.
     */
    protected ?\LtiTool $tool = null;

    /**
     * @var \LtiPlatform|null The LTI platform for the registration instance.
     */
    protected ?\LtiPlatform $platform = null;

    public function __construct(
        \LtiTool|\LtiPlatform|null $item,
        protected ?\LtiResourceLink $link = null
    ) {
        if ($item instanceof \LtiTool) {
            $this->tool = $item;
        }
        if ($item instanceof \LtiPlatform) {
            $this->platform = $item;
        }
    }

    public function setLtiTool(\LtiTool $tool)
    {
        $this->tool = $tool;
    }

    public function getLtiTool() : ?\LtiTool
    {
        return $this->tool;
    }

    public function setLtiPlatform(\LtiPlatform $platform)
    {
        $this->platform = $platform;
    }

    public function getLtiPlatform() : ?\LtiPlatform
    {
        return $this->platform;
    }

    public function setLtiResourceLink(\LtiResourceLink $link)
    {
        $this->link = $link;
    }

    public function getLtiResourceLink() : ?\LtiResourceLink
    {
        return $this->link;
    }

    #[\Override]
    public function getIdentifier(): string
    {
        if ($this->tool) {
            return $this->tool->id;
        }

        if ($this->platform) {
            return $this->platform->id;
        }

        if ($this->link) {
            return $this->tool->id . '_' . $this->link->id;
        }

        return '';
    }

    #[\Override]
    public function getClientId(): string
    {
        return $this->getIdentifier();
    }

    #[\Override]
    public function getPlatform(): PlatformInterface
    {
        return PlatformManager::getPlatformConfiguration();

//        if ($this->platform) {
//            return $this->platform->getPlatformData();
//        } elseif ($this->tool) {
//            //Use the global platform configuration for Stud.IP as LTI platform.
//            return PlatformManager::getPlatformConfiguration();
//        }
//        //If no platform or tool is present, the registration is not linked to a platform.
//        throw new \Studip\LTIException(
//            'No LTI platform present.',
//            \Studip\LTIException::REGISTRATION_NOT_LINKED_TO_PLATFORM
//        );
    }

    #[\Override]
    public function getTool(): ToolInterface
    {
        if ($this->tool) {
            return $this->tool->getToolData();
        } elseif ($this->platform) {
            //Return the global tool:
            return \LtiTool::getGlobalTool();
        }
        //If no tool or platform is present, the registration is not linked to a tool.
        throw new \Studip\LTIException(
            'No LTI tool link present.',
            \Studip\LTIException::REGISTRATION_NOT_LINKED_TO_TOOL
        );
    }

    #[\Override]
    public function getDeploymentIds(): array
    {
        if ($this->tool) {
            return \DBManager::get()->fetchFirst("SELECT `id` FROM `lti_deployments` WHERE `tool_id` = ?", [$this->tool->id]);
        }

        if ($this->link) {
            return [$this->link->deployment_id];
        }

        return [];
    }

    #[\Override]
    public function hasDeploymentId(string $deploymentId): bool
    {
        if ($this->tool) {
            return \LtiDeployment::countBySql(
                    "`tool_id` = :tool_id AND `id` = :deployment_id",
                    ['tool_id' => $this->tool->id, 'deployment_id' => $deploymentId]
                ) > 0;
        }

        if ($this->link) {
            return $this->link->deployment_id == $deploymentId;
        }

        return false;
    }

    #[\Override]
    public function getDefaultDeploymentId(): ?string
    {
        //There is no default deployment-ID in Stud.IP:
        return null;
    }

    #[\Override]
    public function getPlatformKeyChain(): ?KeyChainInterface
    {
        if ($this->platform) {
            //TODO: return the platform keyring.
        } elseif ($this->tool) {
            $platform_keyring = PlatformManager::getPlatformKeyring();
            if (!$platform_keyring) {
                $platform_keyring = PlatformManager::generatePlatformKeyring();
            }
            return $platform_keyring->toKeyChain();
        }

        return null;
    }

    #[\Override]
    public function getToolKeyChain(): ?KeyChainInterface
    {
        if ($this->tool) {
            if ($this->tool->jwks_url) {
                return null;
            }
            $keyring = $this->tool->getKeyring();
            if (!$keyring) {
                throw new LtiException('Failed to load public key for tool ' . $this->tool->id);
            }
            return $keyring->toKeyChain();
        } elseif ($this->platform) {
            //Return the global tool keychain.
            $keyring = \LtiTool::getGlobalToolKeyring(true);
            if ($keyring) {
                return $keyring->toKeyChain();
            }
        }

        return null;
    }

    #[\Override]
    public function getPlatformJwksUrl(): ?string
    {
        if ($this->platform) {
            return $this->platform->jwks_url ?? null;
        } else {
            return PlatformManager::getJwksUrl();
        }
    }

    #[\Override]
    public function getToolJwksUrl(): ?string
    {
        if ($this->tool) {
            return $this->tool->jwks_url ?? null;
        } else {
            //Return the global JWKS URL for Stud.IP as LTI tool.
            return \LtiTool::getGlobalJwksUrl();
        }
    }
}