Repository navigation
Expand file tree
/
Copy pathlib.php
More file actions
84 lines (73 loc) · 3.1 KB
/
Copy pathlib.php
File metadata and controls
84 lines (73 loc) · 3.1 KB
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
<?php
defined('MOODLE_INTERNAL') || die();
use local_isycredentials\package_csc_signing_service;
use local_isycredentials\badge_credential_mapper;
use local_isycredentials\csc\profile_repository;
/**
* Signs a document using the configured signing service.
*
* @param string $document The document to sign.
* @param string $service_type The signing service type. Only 'csc' is supported.
* @return string The signed document data.
* @throws Exception If any error occurs during the signing process.
*/
function local_isycredentials_sign_document(string $document, string $service_type = 'csc'): string {
if ($service_type !== 'csc') {
throw new moodle_exception('csc_only_signing_service', 'local_isycredentials');
}
$document_data = json_decode($document, true);
if (json_last_error() !== JSON_ERROR_NONE || !is_array($document_data) || array_is_list($document_data)) {
throw new moodle_exception('csc_invalid_document', 'local_isycredentials');
}
$issuer = json_decode(get_config('local_isycredentials', 'elm_issuer_data'), true);
if (!is_array($issuer) || empty($issuer['id']) || !is_string($issuer['id'])) {
throw new moodle_exception('csc_invalid_issuer', 'local_isycredentials');
}
if (isset($document_data['issuer']['id'])
&& $document_data['issuer']['id'] !== $issuer['id']) {
throw new moodle_exception(
'csc_issuer_mismatch',
'local_isycredentials',
'',
[$document_data['issuer']['id'], $issuer['id']]
);
}
$profile = profile_repository::for_issuer($issuer['id']);
$signing_service = new package_csc_signing_service($profile, $issuer);
return $signing_service->sign($document);
}
/**
* Creates a credential document based on the given badge id and user id and then signs it
* @param int $badgeid the id of the badge that is the basis for the credential
* @param int $userid the id of the user that is awarded with the badge
* @param bool $withDeliveryDetails if true, the credential will be wrapped in a object with a key 'credential' and a key 'deliveryDetails'.
* @return string returns the signed credential json document.
* @throws Exception if badge or user not found.
*/
function local_isycredentials_create_credential_from_badge(int $badgeid, int $userid, bool $withDeliveryDetails = false): string {
global $DB;
// Fetch badge data
$badge = $DB->get_record('badge', ['id' => $badgeid], '*', MUST_EXIST);
// Fetch User data
$user = \core_user::get_user($userid);
if (!$user) {
throw new Exception('User not found.');
}
$credential = (new badge_credential_mapper())->from_badge($badge, $user);
if ($withDeliveryDetails) {
$credential = [
'credential' => $credential->toArray(),
'deliveryDetails' => [
'deliveryAddress' => [
$user->email
]
]
];
} else {
return $credential->toJson();
}
return json_encode(
$credential,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR
);
}