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

extractUserAndAttributes()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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