Passed
Pull Request — master (#5)
by Tim
02:10
created

sspmod_casserver_Cas_AttributeExtractor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 79
rs 10
c 1
b 0
f 0
wmc 8

2 Methods

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