Passed
Push — master ( c1d85e...fd816d )
by
unknown
01:48
created

AttributeExtractor::extractUserAndAttributes()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 6
nop 2
dl 0
loc 32
rs 9.0444
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\casserver\Cas;
4
5
use SimpleSAML\Configuration;
6
use SimpleSAML\Module;
7
8
/**
9
 * Extract the user and any mapped attributes from the AuthSource attributes
10
 */
11
class AttributeExtractor
12
{
13
14
    /**
15
     * Determine the user and any CAS attributes based on the attributes from the
16
     * authsource and the CAS configuration.
17
     *
18
     * The result is an array
19
     * [
20
     *   'user' => 'user_value',
21
     *   'attributes' => [
22
     *    // any attributes
23
     * ]
24
     *
25
     * If no CAS attributes are configured then the attributes array is empty
26
     * @param array $attributes
27
     * @param \SimpleSAML\Configuration $casconfig
28
     * @return array
29
     */
30
    public function extractUserAndAttributes(array $attributes, Configuration $casconfig)
31
    {
32
        if ($casconfig->hasValue('authproc')) {
33
            $attributes = $this->invokeAuthProc($attributes, $casconfig);
34
        }
35
36
        $casUsernameAttribute = $casconfig->getValue('attrname', 'eduPersonPrincipalName');
37
38
        //TODO: how should a missing userName be handled?
39
        $userName = $attributes[$casUsernameAttribute][0];
40
41
        if ($casconfig->getValue('attributes', true)) {
42
            $attributesToTransfer = $casconfig->getValue('attributes_to_transfer', []);
43
44
            if (sizeof($attributesToTransfer) > 0) {
45
                $casAttributes = [];
46
47
                foreach ($attributesToTransfer as $key) {
48
                    if (array_key_exists($key, $attributes)) {
49
                        $casAttributes[$key] = $attributes[$key];
50
                    }
51
                }
52
            } else {
53
                $casAttributes = $attributes;
54
            }
55
        } else {
56
            $casAttributes = [];
57
        }
58
59
        return [
60
            'user' => $userName,
61
            'attributes' => $casAttributes
62
        ];
63
    }
64
65
    /**
66
     * Process any authproc filters defined in the configuration. The Authproc filters must only
67
     * rely on 'Attributes' being available and not on additional SAML state
68
     * @param array $attributes The current attributes
69
     * @param \SimpleSAML\Configuration $casconfig The cas configuration
70
     * @return array The attributes post processing.
71
     */
72
    private function invokeAuthProc(array $attributes, Configuration $casconfig)
73
    {
74
        $filters = $casconfig->getArray('authproc', []);
75
76
        $state = [
77
            'Attributes' => $attributes
78
        ];
79
        foreach ($filters as $config) {
80
            $className = Module::resolveClass(
81
                $config['class'],
82
                'Auth\Process',
83
                \SimpleSAML\Auth\ProcessingFilter::class
84
            );
85
            /** @psalm-suppress InvalidStringClass */
86
            $filter = new $className($config, null);
87
            $filter->process($state);
88
        }
89
90
        return $state['Attributes'];
91
    }
92
}
93