File: MultiAuthTest.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (234 lines) | stat: -rw-r--r-- 7,166 bytes parent folder | download | duplicates (3)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Module\multiauth\Auth\Source;

use Error;
use Exception;
use SimpleSAML\Test\Utils\ClearStateTestCase;
use SimpleSAML\Configuration;
use SimpleSAML\Module\multiauth\Auth\Source\MultiAuth;

class MultiAuthTest extends ClearStateTestCase
{
    /** @var Configuration */
    private $config;

    /** @var Configuration */
    private $sourceConfig;


    /**
     * @return void
     */
    public function setUp()
    {
        $this->config = Configuration::loadFromArray(
            ['module.enable' => ['multiauth' => true]],
            '[ARRAY]',
            'simplesaml'
        );
        Configuration::setPreLoadedConfig($this->config, 'config.php');

        $this->sourceConfig = Configuration::loadFromArray(array(
            'example-multi' => array(
                'multiauth:MultiAuth',

                /*
                 * The available authentication sources.
                 * They must be defined in this authsources.php file.
                 */
                'sources' => array(
                    'example-saml' => array(
                        'text' => array(
                            'en' => 'Log in using a SAML SP',
                            'es' => 'Entrar usando un SP SAML',
                        ),
                        'css-class' => 'SAML',
                    ),
                    'example-admin' => array(
                        'text' => array(
                            'en' => 'Log in using the admin password',
                            'es' => 'Entrar usando la contraseña de administrador',
                        ),
                    ),
                ),
                'preselect' => 'example-saml',
            ),

            'example-saml' => array(
                'saml:SP',
                'entityId' => 'my-entity-id',
                'idp' => 'my-idp',
            ),

            'example-admin' => array(
                'core:AdminPassword',
            ),
        ));
        Configuration::setPreLoadedConfig($this->sourceConfig, 'authsources.php');
    }


    /**
     * @return void
     */
    public function testSourcesMustBePresent(): void
    {
        $this->expectException(Exception::class);
        $this->expectExceptionMessage('The required "sources" config option was not found');
        $sourceConfig = Configuration::loadFromArray(array(
            'example-multi' => array(
                'multiauth:MultiAuth',
            ),
        ));

        Configuration::setPreLoadedConfig($sourceConfig, 'authsources.php');

        new MultiAuth(['AuthId' => 'example-multi'], $sourceConfig->getArray('example-multi'));
    }


    /**
     * @return void
     */
    public function testPreselectMustBeValid(): void
    {
        $this->expectException(Exception::class);
        $this->expectExceptionMessage('The optional "preselect" config option must be present in "sources"');
        $sourceConfig = Configuration::loadFromArray(array(
            'example-multi' => array(
                'multiauth:MultiAuth',

                /*
                 * The available authentication sources.
                 * They must be defined in this authsources.php file.
                 */
                'sources' => array(
                    'example-saml' => array(
                        'text' => array(
                            'en' => 'Log in using a SAML SP',
                            'es' => 'Entrar usando un SP SAML',
                        ),
                        'css-class' => 'SAML',
                    ),
                    'example-admin' => array(
                        'text' => array(
                            'en' => 'Log in using the admin password',
                            'es' => 'Entrar usando la contraseña de administrador',
                        ),
                    ),
                ),
                'preselect' => 'other',
            ),

            'example-saml' => array(
                'saml:SP',
                'entityId' => 'my-entity-id',
                'idp' => 'my-idp',
            ),

            'example-admin' => array(
                'core:AdminPassword',
            ),
        ));

        Configuration::setPreLoadedConfig($sourceConfig, 'authsources.php');
        new MultiAuth(['AuthId' => 'example-multi'], $sourceConfig->getArray('example-multi'));
    }


    /**
     * @return void
     */
    public function testPreselectIsOptional(): void
    {
        $sourceConfig = Configuration::loadFromArray(array(
            'example-multi' => array(
                'multiauth:MultiAuth',

                /*
                 * The available authentication sources.
                 * They must be defined in this authsources.php file.
                 */
                'sources' => array(
                    'example-saml' => array(
                        'text' => array(
                            'en' => 'Log in using a SAML SP',
                            'es' => 'Entrar usando un SP SAML',
                        ),
                        'css-class' => 'SAML',
                    ),
                    'example-admin' => array(
                        'text' => array(
                            'en' => 'Log in using the admin password',
                            'es' => 'Entrar usando la contraseña de administrador',
                        ),
                    ),
                ),
            ),

            'example-saml' => array(
                'saml:SP',
                'entityId' => 'my-entity-id',
                'idp' => 'my-idp',
            ),

            'example-admin' => array(
                'core:AdminPassword',
            ),
        ));

        Configuration::setPreLoadedConfig($sourceConfig, 'authsources.php');

        $state = [];
        $source = new MultiAuth(['AuthId' => 'example-multi'], $sourceConfig->getArray('example-multi'));

        try {
            $source->authenticate($state);
        } catch (Error $e) {
        } catch (Exception $e) {
        }

        $this->assertArrayNotHasKey('multiauth:preselect', $state);
    }


    /**
     * @return void
     */
    public function testPreselectCanBeConfigured(): void
    {
        $state = [];

        $source = new MultiAuth(['AuthId' => 'example-multi'], $this->sourceConfig->getArray('example-multi'));

        try {
            $source->authenticate($state);
        } catch (Exception $e) {
        }

        $this->assertArrayHasKey('multiauth:preselect', $state);
        $this->assertEquals('example-saml', $state['multiauth:preselect']);
    }


    /**
     * @return void
     */
    public function testStatePreselectHasPriority(): void
    {
        $state = ['multiauth:preselect' => 'example-admin'];

        $source = new MultiAuth(['AuthId' => 'example-multi'], $this->sourceConfig->getArray('example-multi'));

        try {
            $source->authenticate($state);
        } catch (Exception $e) {
        }

        $this->assertArrayHasKey('multiauth:preselect', $state);
        $this->assertEquals('example-admin', $state['multiauth:preselect']);
    }
}