Passed
Pull Request — master (#5)
by Tim
01:53
created

invokeAuthProc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
use SimpleSAML\Configuration;
4
5
/**
6
 * Extract the user and any mapped attributes from the AuthSource attributes
7
 */
8
class sspmod_casserver_Cas_AttributeExtractor
9
{
10
11
    /**
12
     * Determine the user and any CAS attributes based on the attributes from the
13
     * authsource and the CAS configuration.
14
     *
15
     * The result is an array
16
     * [
17
     *   'user' => 'user_value',
18
     *   'attributes' => [
19
     *    // any attributes
20
     * ]
21
     *
22
     * If no CAS attributes are configured then the attributes array is empty
23
     * @param array $attributes
24
     * @param \SimpleSAML\Configuration $casconfig
25
     * @return array
26
     */
27
    public function extractUserAndAttributes(array $attributes, Configuration $casconfig)
28
    {
29
        if ($casconfig->hasValue('authproc')) {
30
            $attributes = $this->invokeAuthProc($attributes, $casconfig);
31
        }
32
33
        $casUsernameAttribute = $casconfig->getValue('attrname', 'eduPersonPrincipalName');
34
35
        //TODO: how should a missing userName be handled?
36
        $userName = $attributes[$casUsernameAttribute][0];
37
38
        if ($casconfig->getValue('attributes', true)) {
39
            $attributesToTransfer = $casconfig->getValue('attributes_to_transfer', []);
40
41
            if (sizeof($attributesToTransfer) > 0) {
42
                $casAttributes = array();
43
44
                foreach ($attributesToTransfer as $key) {
45
                    if (array_key_exists($key, $attributes)) {
46
                        $casAttributes[$key] = $attributes[$key];
47
                    }
48
                }
49
            } else {
50
                $casAttributes = $attributes;
51
            }
52
        } else {
53
            $casAttributes = [];
54
        }
55
56
        return array(
57
            'user' => $userName,
58
            'attributes' => $casAttributes
59
        );
60
    }
61
62
    /**
63
     * Process any authproc filters defined in the configuration. The Authproc filters must only
64
     * rely on 'Attributes' being available and not on additional SAML state
65
     * @param array $attributes The current attributes
66
     * @param \SimpleSAML\Configuration $casconfig The cas configuration
67
     * @return array The attributes post processing.
68
     */
69
    private function invokeAuthProc(array $attributes, Configuration $casconfig)
70
    {
71
        $filters = $casconfig->getArray('authproc', []);
72
73
        $state = array(
74
            'Attributes' => $attributes
75
        );
76
        foreach ($filters as $config) {
77
            $className = SimpleSAML_Module::resolveClass(
0 ignored issues
show
Bug introduced by
The type SimpleSAML_Module was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
                $config['class'],
79
                'Auth_Process',
80
                'SimpleSAML_Auth_ProcessingFilter'
81
            );
82
            $filter = new $className($config, null);
83
            $filter->process($state);
84
        }
85
86
        return $state['Attributes'];
87
    }
88
}
89