-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
executable file
·50 lines (42 loc) · 1.7 KB
/
index.php
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
<?php
include(__DIR__ . '/lib/init.php');
#require_once "phar://mdq-php.phar/init.php";
global $logger;
// 1- check received request
// * Must contain entityID attribute (URL encoded)
// * If not provided, must must provide all the entities (ie MD file)
// * Check accept is "application/samlmetadata+xml"
if ($_SERVER['REQUEST_METHOD'] != 'GET') {
$logger->addError("Non GET method");
http_response_code(405);
exit("Non supported method");
}
if ($_SERVER['HTTP_ACCEPT'] != "application/samlmetadata+xml") {
$logger->addError("Unsupported accept value: ".$_SERVER['HTTP_ACCEPT']);
http_response_code(406);
exit("Unsupported accept value: ".$_SERVER['HTTP_ACCEPT']);
}
// 2- Decode entityID
$logger->addDebug("Path Info = ".$_SERVER['PATH_INFO']);
if (!isset($_SERVER['PATH_INFO']) || !startsWith($_SERVER['PATH_INFO'], "/entities")) {
http_response_code(400);
exit('Bad request');
}
if (endsWith($_SERVER['PATH_INFO'], "/entities")) {
$mdFile = $config["federation"]["localPath"] ."/". $config["federation"]["metadataFile"];
} else {
$index = strpos($_SERVER['PATH_INFO'], "/entities/");
$entityId = urldecode(substr($_SERVER['PATH_INFO'], $index + 10));
// 3- Compute hash (lower-case hex-encoded SHA-1 digest of the entityID)
$mdFile = $config["federation"]["localPath"] ."/". sha1($entityId) . ".xml";
$logger->addDebug("Requested entity ID: ".$entityId." / mdFile: ".$mdFile);
// 4- Check if file exists
if(!file_exists($mdFile)) {
http_response_code(404);
exit("Unknown entityID ".$entityId);
}
}
// 5- Return the file
header('Content-Type: application/samlmetadata+xml');
http_response_code(200);
echo file_get_contents($mdFile);