aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/LTI13a/Registration.php
blob: 5498ace59ffe9165ecf6ea53254cc0f2e519753c (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
<?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
{
    public function __construct(
        protected ?\LtiTool $tool,
        protected ?\LtiResourceLink $link = null
    ) {
    }

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

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

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

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

    #[\Override]
    public function getIdentifier(): string
    {
        if (!$this->tool) {
            return '';
        }
        if ($this->link) {
            return $this->tool->id . '_' . $this->link->id;
        } else {
            return $this->tool->id;
        }
    }

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

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

    #[\Override]
    public function getTool(): ToolInterface
    {
        if (!$this->tool) {
            throw new \Studip\LTIException(
                'No LTI tool link present.',
                \Studip\LTIException::REGISTRATION_NOT_LINKED_TO_TOOL
            );
        }
        return $this->tool->getToolData();
    }

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

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

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

    #[\Override]
    public function getPlatformKeyChain(): ?KeyChainInterface
    {
        $platform_keyring = PlatformManager::getPlatformKeyring();
        if (!$platform_keyring) {
            $platform_keyring = PlatformManager::generatePlatformKeyring();
        }
        return $platform_keyring->toKeyChain();
    }

    #[\Override]
    public function getToolKeyChain(): ?KeyChainInterface
    {
        if (!$this->tool || $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();
    }

    #[\Override]
    public function getPlatformJwksUrl(): ?string
    {
        return PlatformManager::getJwksUrl();
    }

    #[\Override]
    public function getToolJwksUrl(): ?string
    {
        return $this->tool->jwks_url ?? null;
    }
}