File: AuthorizeTest.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 (162 lines) | stat: -rw-r--r-- 5,638 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
<?php
/**
 * Test for the authorize:Authorize authproc filter.
 */

namespace SimpleSAML\Module\authorize\Auth\Process;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Module\Authorize\Tests\Utils\TestableAuthorize;
use SimpleSAML\Utils\Attributes;

class AuthorizeTest extends TestCase
{
    /**
     * Helper function to run the filter with a given configuration.
     *
     * @param array $config The filter configuration.
     * @param array $request The request state.
     * @return array  The state array after processing.
     */
    private function processFilter(array $config, array $request)
    {
        $filter = new TestableAuthorize($config, null);
        $filter->process($request);
        return $request;
    }

    /**
     * Test that having a matching attribute grants access
     * @dataProvider allowScenarioProvider
     * @param array $userAttributes The attributes to test
     * @param bool $isAuthorized Should the user be authorized
     */
    public function testAllowScenarios($userAttributes, $isAuthorized)
    {
        $userAttributes = Attributes::normalizeAttributesArray($userAttributes);
        $config = [
            'uid' => [
                '/^.*@example.com$/',
                '/^(user1|user2|user3)@example.edu$/',
            ],
            'schacUserStatus' => '@urn:mace:terena.org:userStatus:example.org:service:active.*@',
        ];

        $resultState = $this->processFilter($config, ['Attributes' => $userAttributes]);

        $resultAuthorized = isset($resultState['NOT_AUTHORIZED']) ? false : true;
        $this->assertEquals($isAuthorized, $resultAuthorized);
    }

    public function allowScenarioProvider()
    {
        return [
            // Should be allowed
            [['uid' => 'anything@example.com'], true],
            [['uid' => 'user2@example.edu'], true],
            [['schacUserStatus' => 'urn:mace:terena.org:userStatus:example.org:service:active.my.service'], true],
            [
                [
                    'uid' => ['wrongValue', 'user2@example.edu', 'wrongValue2'],
                    'schacUserStatus' => 'incorrectstatus'
                ],
                true
            ],

            //Should be denied
            [['wrongAttributes' => ['abc']], false],
            [
                [
                    'uid' => [
                        'anything@example.com.wrong',
                        'wronguser@example.edu',
                        'user2@example.edu.wrong',
                        'prefixuser2@example.edu'
                    ]
                ],
                false
            ],
        ];
    }

    /**
     * Test that having a matching attribute prevents access
     * @dataProvider invertScenarioProvider
     * @param array $userAttributes The attributes to test
     * @param bool $isAuthorized Should the user be authorized
     */
    public function testInvertAllowScenarios($userAttributes, $isAuthorized)
    {
        $userAttributes = Attributes::normalizeAttributesArray($userAttributes);
        $config = [
            'deny' => true,
            'uid' => [
                '/.*@students.example.edu$/',
                '/^(stu1|stu2|stu3)@example.edu$/',
            ],
            'schacUserStatus' => '@urn:mace:terena.org:userStatus:example.org:service:blocked.*@',
        ];

        $resultState = $this->processFilter($config, ['Attributes' => $userAttributes]);

        $resultAuthorized = isset($resultState['NOT_AUTHORIZED']) ? false : true;
        $this->assertEquals($isAuthorized, $resultAuthorized);
    }

    public function invertScenarioProvider()
    {
        return [
            // Should be allowed
            [['noMatch' => 'abc'], true],
            [['uid' => 'anything@example.edu'], true],

            //Should be denied
            [['uid' => 'anything@students.example.edu'], false],
            [['uid' => 'stu3@example.edu'], false],
            [['schacUserStatus' => 'urn:mace:terena.org:userStatus:example.org:service:blocked'], false],
            // Matching any of the attributes results in denial
            [
                [
                    'uid' => ['noMatch', 'abc@students.example.edu', 'noMatch2'],
                    'schacUserStatus' => 'noMatch'
                ],
                false
            ],
        ];
    }

    /**
     * Test that having a matching attribute prevents access
     * @dataProvider noregexScenarioProvider
     * @param array $userAttributes The attributes to test
     * @param bool $isAuthorized Should the user be authorized
     */
    public function testDisableRegex($userAttributes, $isAuthorized)
    {
        $userAttributes = Attributes::normalizeAttributesArray($userAttributes);
        $config = [
            'regex' => false,
            'group' => [
                'CN=SimpleSAML Students,CN=Users,DC=example,DC=edu',
                'CN=All Teachers,OU=Staff,DC=example,DC=edu',
            ],
        ];

        $resultState = $this->processFilter($config, ['Attributes' => $userAttributes]);

        $resultAuthorized = isset($resultState['NOT_AUTHORIZED']) ? false : true;
        $this->assertEquals($isAuthorized, $resultAuthorized);
    }

    public function noregexScenarioProvider()
    {
        return [
            // Should be allowed
            [['group' => 'CN=SimpleSAML Students,CN=Users,DC=example,DC=edu'], true],

            //Should be denied
            [['wrongAttribute' => 'CN=SimpleSAML Students,CN=Users,DC=example,DC=edu'], false],
            [['group' => 'CN=wrongCN=SimpleSAML Students,CN=Users,DC=example,DC=edu'], false],
        ];
    }
}