Passed
Push — master ( 52914c...5ecbc9 )
by Tim
03:03
created

SmartID::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\smartattributes\Auth\Process;
4
5
use Webmozart\Assert\Assert;
6
7
class SmartID extends \SimpleSAML\Auth\ProcessingFilter
8
{
9
    /**
10
     * Which attributes to use as identifiers?
11
     *
12
     * IMPORTANT: If you use the (default) attributemaps (twitter2name, facebook2name,
13
     * etc., be sure to comment out the entries that map xxx_targetedID to
14
     * eduPersonTargetedID, or there will be no way to see its origin any more.
15
     *
16
     * @var array
17
     */
18
    private $candidates = [
19
        'eduPersonTargetedID',
20
        'eduPersonPrincipalName',
21
        'pairwise-id',
22
        'subject-id',
23
        'openid',
24
        'facebook_targetedID',
25
        'twitter_targetedID',
26
        'windowslive_targetedID',
27
        'linkedin_targetedID',
28
    ];
29
30
    /**
31
     * @var string The name of the generated ID attribute.
32
     */
33
    private $id_attribute = 'smart_id';
34
35
    /**
36
     * Whether to append the AuthenticatingAuthority, separated by '!'
37
     * This only works when SSP is used as a gateway.
38
     * @var bool
39
     */
40
    private $add_authority = true;
41
42
    /**
43
     * Whether to prepend the CandidateID, separated by ':'
44
     * @var bool
45
     */
46
    private $add_candidate = true;
47
48
    /**
49
     * Attributes which should be added/appended.
50
     *
51
     * @var array Associative array of arrays.
52
     */
53
    private $attributes = [];
0 ignored issues
show
introduced by
The private property $attributes is not used, and could be removed.
Loading history...
54
55
56
    /**
57
     * @param array $config
58
     * @param mixed $reserved
59
     * @throws \Exception
60
     */
61
    public function __construct($config, $reserved)
62
    {
63
        parent::__construct($config, $reserved);
64
65
        Assert::isArray($config);
66
67
        if (array_key_exists('candidates', $config)) {
68
            $this->candidates = $config['candidates'];
69
            if (!is_array($this->candidates)) {
70
                throw new \Exception('SmartID authproc configuration error: \'candidates\' should be an array.');
71
            }
72
        }
73
74
        if (array_key_exists('id_attribute', $config)) {
75
            $this->id_attribute = $config['id_attribute'];
76
            if (!is_string($this->id_attribute)) {
77
                throw new \Exception('SmartID authproc configuration error: \'id_attribute\' should be a string.');
78
            }
79
        }
80
81
        if (array_key_exists('add_authority', $config)) {
82
            $this->add_authority = $config['add_authority'];
83
            if (!is_bool($this->add_authority)) {
84
                throw new \Exception('SmartID authproc configuration error: \'add_authority\' should be a boolean.');
85
            }
86
        }
87
88
        if (array_key_exists('add_candidate', $config)) {
89
            $this->add_candidate = $config['add_candidate'];
90
            if (!is_bool($this->add_candidate)) {
91
                throw new \Exception('SmartID authproc configuration error: \'add_candidate\' should be a boolean.');
92
            }
93
        }
94
    }
95
96
97
    /**
98
     * @param array $attributes
99
     * @param array $request
100
     * @return string
101
     * @throws \SimpleSAML\Error\Exception
102
     */
103
    private function addID($attributes, $request)
104
    {
105
        $state = $request['saml:sp:State'];
106
        foreach ($this->candidates as $idCandidate) {
107
            if (isset($attributes[$idCandidate][0])) {
108
                if (($this->add_authority) && (isset($state['saml:AuthenticatingAuthority'][0]))) {
109
                    return ($this->add_candidate ? $idCandidate.':' : '').$attributes[$idCandidate][0].'!'.
110
                        $state['saml:AuthenticatingAuthority'][0];
111
                } else {
112
                    return ($this->add_candidate ? $idCandidate.':' : '').$attributes[$idCandidate][0];
113
                }
114
            }
115
        }
116
        /*
117
         * At this stage no usable id_candidate has been detected.
118
         */
119
        throw new \SimpleSAML\Error\Exception('This service needs at least one of the following
120
            attributes to identity users: '.implode(', ', $this->candidates).'. Unfortunately not
121
            one of them was detected. Please ask your institution administrator to release one of
122
            them, or try using another identity provider.');
123
    }
124
125
126
    /**
127
     * Apply filter to add or replace attributes.
128
     *
129
     * Add or replace existing attributes with the configured values.
130
     *
131
     * @param array &$request  The current request
132
     * @return void
133
     */
134
    public function process(&$request)
135
    {
136
        Assert::isArray($request);
137
        Assert::keyExists($request, 'Attributes');
138
139
        $id = $this->addID($request['Attributes'], $request);
140
141
        if (isset($id)) {
142
            $request['Attributes'][$this->id_attribute] = [$id];
143
        }
144
    }
145
}
146