Passed
Pull Request — master (#1316)
by
unknown
34:12
created

ClaimExtractor::addClaimSet()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace League\OAuth2\Server;
4
5
use League\OAuth2\Server\Entities\ClaimSetEntry;
6
use League\OAuth2\Server\Entities\ClaimSetEntryInterface;
7
use League\OAuth2\Server\Entities\ScopeEntityInterface;
8
9
/**
10
 * ClaimExtractor
11
 *
12
 * @link https://github.com/steverhoades/oauth2-openid-connect-server
13
 *
14
 * @author Steve Rhoades <[email protected]>
15
 * @author Marc Riemer <[email protected]>
16
 */
17
class ClaimExtractor implements ClaimExtractorInterface
18
{
19
    /**
20
     * claimSets
21
     *
22
     * @var ClaimSetEntryInterface[]
23
     */
24
    protected $claimSets = [];
25
26
    protected $protectedClaims = ['profile', 'email', 'address', 'phone'];
27
28
    /**
29
     * ClaimExtractor constructor
30
     *
31
     * @param ClaimSetEntryInterface[] $claimSets
32
     */
33
    public function __construct(array $claimSets = [])
34
    {
35
        $this->claimSets = self::getDefaultClaimSetEnties();
36
        foreach ($claimSets as $claimSet) {
37
            $this->addClaimSet($claimSet);
38
        }
39
    }
40
41
    /**
42
     * @param ClaimSetEntryInterface $claimSetEntry
43
     *
44
     * @return $this
45
     *
46
     * @throws \InvalidArgumentException
47
     */
48
    public function addClaimSet(ClaimSetEntryInterface $claimSetEntry): ClaimExtractor
49
    {
50
        if (\in_array($claimSetEntry->getScope(), $this->protectedClaims) && !$this->getClaimSet($claimSetEntry->getScope())) {
51
            throw new \InvalidArgumentException(
52
                \sprintf('%s is a protected scope and is pre-defined by the OpenID Connect specification.', $claimSetEntry->getScope())
53
            );
54
        }
55
56
        $this->claimSets[] = $claimSetEntry;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $scope
63
     *
64
     * @return ClaimSetEntryInterface|null
65
     */
66
    public function getClaimSet(string $scope): ?ClaimSetEntryInterface
67
    {
68
        foreach ($this->claimSets as $set) {
69
            if ($set->getScope() === $scope) {
70
                return $set;
71
            }
72
        }
73
74
        return null;
75
    }
76
77
    /**
78
     * Get claimSets
79
     *
80
     * @return array
81
     */
82
    public function getClaimSets(): array
83
    {
84
        return $this->claimSets;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function extract(array $scopes, array $claims): array
91
    {
92
        $claimData  = [];
93
        $keys = \array_keys($claims);
94
95
        foreach ($scopes as $scope) {
96
            $scopeName = ($scope instanceof ScopeEntityInterface) ? $scope->getIdentifier() : $scope;
97
98
            $claimSet = $this->getClaimSet($scopeName);
99
            if (null === $claimSet) {
100
                continue;
101
            }
102
103
            $intersected = \array_intersect($claimSet->getClaims(), $keys);
104
105
            if (empty($intersected)) {
106
                continue;
107
            }
108
109
            $data = \array_filter(
110
                $claims,
111
                function ($key) use ($intersected) {
112
                    return \in_array($key, $intersected);
113
                },
114
                ARRAY_FILTER_USE_KEY
115
            );
116
117
            $claimData = \array_merge($claimData, $data);
118
        }
119
120
        return $claimData;
121
    }
122
123
    /**
124
     * Create a array default openID connect claims
125
     *
126
     * @see http://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims
127
     *
128
     * @return ClaimSetEntry[]
129
     */
130
    public static function getDefaultClaimSetEnties(): array
131
    {
132
        return [
133
            new ClaimSetEntry('profile', [
134
                'name',
135
                'family_name',
136
                'given_name',
137
                'middle_name',
138
                'nickname',
139
                'preferred_username',
140
                'profile',
141
                'picture',
142
                'website',
143
                'gender',
144
                'birthdate',
145
                'zoneinfo',
146
                'locale',
147
                'updated_at',
148
            ]),
149
            new ClaimSetEntry('email', [
150
                'email',
151
                'email_verified',
152
            ]),
153
            new ClaimSetEntry('address', [
154
                'address',
155
            ]),
156
            new ClaimSetEntry('phone', [
157
                'phone_number',
158
                'phone_number_verified',
159
            ]),
160
            new ClaimSetEntry('openid', [
161
                'nonce',
162
                'auth_time',
163
                'acr',
164
                'amr',
165
                'azp',
166
            ]),
167
        ];
168
    }
169
}
170