Passed
Pull Request — master (#12)
by Tim
02:01
created

MetaRefresh::setAuthUtils()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\metarefresh\Controller;
6
7
use Exception;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Error;
11
use SimpleSAML\Logger;
12
use SimpleSAML\Module\metarefresh\MetaLoader;
13
use SimpleSAML\Session;
14
use SimpleSAML\Utils;
15
use SimpleSAML\XHTML\Template;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Controller class for the metarefresh module.
21
 *
22
 * This class serves the different views available in the module.
23
 *
24
 * @package SimpleSAML\Module\metarefresh
25
 */
26
27
class MetaRefresh
28
{
29
    /** @var \SimpleSAML\Configuration */
30
    protected $config;
31
32
    /** @var \SimpleSAML\Session */
33
    protected $session;
34
35
    /** @var \SimpleSAML\Configuration */
36
    protected $module_config;
37
38
    /**
39
     * @var \SimpleSAML\Utils\Auth|string
40
     * @psalm-var \SimpleSAML\Utils\Auth|class-string
41
     */
42
    protected $authUtils = Utils\Auth::class;
43
44
45
    /**
46
     * Controller constructor.
47
     *
48
     * It initializes the global configuration and auth source configuration for the controllers implemented here.
49
     *
50
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
51
     * @param \SimpleSAML\Session                    $session The session to use by the controllers.
52
     *
53
     * @throws \Exception
54
     */
55
    public function __construct(
56
        Configuration $config,
57
        Session $session
58
    ) {
59
        $this->config = $config;
60
        $this->session = $session;
61
        $this->module_config = Configuration::getOptionalConfig('module_metarefresh.php');
62
    }
63
64
65
    /**
66
     * Inject the \SimpleSAML\Utils\Auth dependency.
67
     *
68
     * @param \SimpleSAML\Utils\Auth $authUtils
69
     */
70
    public function setAuthUtils(Utils\Auth $authUtils): void
71
    {
72
        $this->authUtils = $authUtils;
73
    }
74
75
76
    /**
77
     * @return \SimpleSAML\XHTML\Template
78
     */
79
    public function main(): Template
80
    {
81
        $this->authUtils::requireAdmin();
82
83
        Logger::setCaptureLog(true);
84
        $sets = $this->module_config->getArray('sets', []);
85
86
        foreach ($sets as $setkey => $set) {
87
            $set = Configuration::loadFromArray($set);
88
89
            Logger::info('[metarefresh]: Executing set [' . $setkey . ']');
90
91
            try {
92
                $expireAfter = $set->getInteger('expireAfter', null);
93
                if ($expireAfter !== null) {
94
                    $expire = time() + $expireAfter;
95
                } else {
96
                    $expire = null;
97
                }
98
                $metaloader = new MetaLoader($expire);
99
100
                // Get global black/whitelists
101
                $blacklist = $this->module_config->getArray('blacklist', []);
102
                $whitelist = $this->module_config->getArray('whitelist', []);
103
104
                // get global type filters
105
                $available_types = [
106
                    'saml20-idp-remote',
107
                    'saml20-sp-remote',
108
                    'attributeauthority-remote'
109
                ];
110
                $set_types = $set->getArrayize('types', $available_types);
111
112
                foreach ($set->getArray('sources') as $source) {
113
                    // filter metadata by type of entity
114
                    if (isset($source['types'])) {
115
                        $metaloader->setTypes($source['types']);
116
                    } else {
117
                        $metaloader->setTypes($set_types);
118
                    }
119
120
                    // Merge global and src specific blacklists
121
                    if (isset($source['blacklist'])) {
122
                        $source['blacklist'] = array_unique(array_merge($source['blacklist'], $blacklist));
123
                    } else {
124
                        $source['blacklist'] = $blacklist;
125
                    }
126
127
                    // Merge global and src specific whitelists
128
                    if (isset($source['whitelist'])) {
129
                        $source['whitelist'] = array_unique(array_merge($source['whitelist'], $whitelist));
130
                    } else {
131
                        $source['whitelist'] = $whitelist;
132
                    }
133
134
                    Logger::debug(
135
                        '[metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']'
136
                    );
137
                    $metaloader->loadSource($source);
138
                }
139
140
                $outputDir = $set->getString('outputDir');
141
                $outputDir = Utils\System::resolvePath($outputDir);
142
143
                $outputFormat = $set->getValueValidate('outputFormat', ['flatfile', 'serialize'], 'flatfile');
144
                switch ($outputFormat) {
145
                    case 'flatfile':
146
                        $metaloader->writeMetadataFiles($outputDir);
147
                        break;
148
                    case 'serialize':
149
                        $metaloader->writeMetadataSerialize($outputDir);
150
                        break;
151
                }
152
            } catch (Exception $e) {
153
                $e = Error\Exception::fromException($e);
154
                $e->logWarning();
155
            }
156
        }
157
158
        $logentries = Logger::getCapturedLog();
159
160
        $t = new Template($this->config, 'metarefresh:fetch.twig');
161
        $t->data['logentries'] = $logentries;
162
        return $t;
163
    }
164
}
165