1<?php
2/* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Saml/interfaces/interface.ilSamlIdpDiscovery.php';
5
6/**
7 * Class ilSimpleSAMLphplIdpDiscovery
8 */
9class ilSimpleSAMLphplIdpDiscovery extends SimpleSAML_XHTML_IdPDisco implements ilSamlIdpDiscovery
10{
11    const METADATA_DIRECTORY = 'auth/saml/metadata';
12
13    /**
14     * ilSimpleSAMLphplIdpDiscovery constructor.
15     */
16    public function __construct()
17    {
18        $this->config = SimpleSAML_Configuration::getInstance();
19        $this->metadata = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
20        $this->instance = 'saml';
21        $this->metadataSets = array('saml20-idp-remote');
22        $this->isPassive = false;
23    }
24
25    /**
26     * @return string
27     */
28    public function getMetadataDirectory()
29    {
30        return self::METADATA_DIRECTORY;
31    }
32
33    /**
34     * @inheritdoc
35     */
36    public function getList()
37    {
38        return $this->getIdPList();
39    }
40
41    /**
42     * @param int $idpId
43     * @return string
44     */
45    protected function getMetadataPath($idpId)
46    {
47        return $this->getMetadataDirectory() . '/' . $idpId . '.xml';
48    }
49
50    /**
51     * @inheritdoc
52     */
53    public function storeIdpMetadata($idpId, $metadata)
54    {
55        global $DIC;
56
57        $fs = $DIC->filesystem()->storage();
58
59        $fs->put($this->getMetadataPath($idpId), $metadata);
60    }
61
62    /**
63     * @inheritdoc
64     */
65    public function fetchIdpMetadata($idpId)
66    {
67        global $DIC;
68
69        $fs = $DIC->filesystem()->storage();
70
71        if (!$fs->has($this->getMetadataPath($idpId))) {
72            return '';
73        }
74
75        return $fs->read($this->getMetadataPath($idpId));
76    }
77
78    /**
79     * @inheritdoc
80     */
81    public function deleteIdpMetadata($idpId)
82    {
83        global $DIC;
84
85        $fs = $DIC->filesystem()->storage();
86
87        if ($fs->has($this->getMetadataPath($idpId))) {
88            $fs->delete($this->getMetadataPath($idpId));
89        }
90    }
91}
92