|
1
|
|
|
<?php |
|
2
|
|
|
// This file is part of Moodle - http://moodle.org/ |
|
3
|
|
|
// |
|
4
|
|
|
// Moodle is free software: you can redistribute it and/or modify |
|
5
|
|
|
// it under the terms of the GNU General Public License as published by |
|
6
|
|
|
// the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
// (at your option) any later version. |
|
8
|
|
|
// |
|
9
|
|
|
// Moodle is distributed in the hope that it will be useful, |
|
10
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
// GNU General Public License for more details. |
|
13
|
|
|
// |
|
14
|
|
|
// You should have received a copy of the GNU General Public License |
|
15
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Payload validator for the assignsubmission_edulegit plugin. |
|
19
|
|
|
* |
|
20
|
|
|
* @package assignsubmission_edulegit |
|
21
|
|
|
* @subpackage validation |
|
22
|
|
|
* @author Alex Crosby <[email protected]> |
|
23
|
|
|
* @copyright @2024 EduLegit.com |
|
24
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace assignsubmission_edulegit; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class edulegit_payload_validator |
|
31
|
|
|
* |
|
32
|
|
|
* This class validates and verifies the signature of payloads sent to the plugin. |
|
33
|
|
|
*/ |
|
34
|
|
|
class edulegit_payload_validator { |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The authorization key used for signature generation. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private string $authkey = ''; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor for the payload validator. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $authkey The authorization key used to sign payloads. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(string $authkey) { |
|
49
|
|
|
$this->authkey = $authkey; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Validates the payload structure to ensure required fields are present. |
|
54
|
|
|
* |
|
55
|
|
|
* @param mixed $payload The payload to be validated. |
|
56
|
|
|
* @return bool True if the payload is valid, false otherwise. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function is_valid(mixed $payload): bool { |
|
59
|
|
|
if (!is_object($payload)) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
foreach (['event', 'data', 'timestamp', 'signature'] as $key) { |
|
64
|
|
|
if (!property_exists($payload, $key)) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Validates the payload signature to ensure the data integrity. |
|
74
|
|
|
* |
|
75
|
|
|
* @param object $payload The payload object containing the signature. |
|
76
|
|
|
* @return bool True if the signature matches, false otherwise. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function is_signed(object $payload): bool { |
|
79
|
|
|
if (!$this->is_valid($payload)) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$signature = $this->generate_signature($payload->event . $payload->timestamp); |
|
84
|
|
|
|
|
85
|
|
|
return $signature && $signature === $payload->signature; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Generates a signature using the auth key and data provided. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $data The data used to generate the signature. |
|
92
|
|
|
* @return string|null The generated signature, or null if authkey or data is missing. |
|
93
|
|
|
*/ |
|
94
|
|
|
private function generate_signature(string $data): ?string { |
|
95
|
|
|
if (!$this->authkey || !$data) { |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
return md5(mb_substr($this->authkey, 0, 10) . $data); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|