Passed
Pull Request — master (#1316)
by
unknown
62:15 queued 27:15
created

ClaimExtractor::getClaimSet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
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
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
        $scope = $claimSetEntry->getScope();
51
52
        if (\in_array($scope, $this->protectedClaims) && !empty($this->claimSets[$scope])) {
53
            throw new \InvalidArgumentException(
54
                \sprintf('%s is a protected scope and is pre-defined by the OpenID Connect specification.', $scope)
55
            );
56
        }
57
58
        $this->claimSets[$scope] = $claimSetEntry->getClaims();
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $scope
65
     *
66
     * @return ClaimSetEntryInterface|null
67
     */
68
    public function getClaimSet(string $scope): ?ClaimSetEntryInterface
69
    {
70
        foreach ($this->claimSets as $set) {
71
            if ($set->getScope() === $scope) {
72
                return $set;
73
            }
74
        }
75
76
        return null;
77
    }
78
79
    /**
80
     * Get claimSets
81
     *
82
     * @return array
83
     */
84
    public function getClaimSets(): array
85
    {
86
        return $this->claimSets;
87
    }
88
89
    /**
90
     * For given scopes and aggregated claims get all claims that have been configured on the extractor.
91
     *
92
     * @param array $scopes
93
     * @param array $claims
94
     *
95
     * @return array
96
     */
97
    public function extract(array $scopes, array $claims): array
98
    {
99
        $claimData  = [];
100
        $keys = \array_keys($claims);
101
102
        foreach ($scopes as $scope) {
103
            $scopeName = ($scope instanceof ScopeEntityInterface) ? $scope->getIdentifier() : $scope;
104
105
            $claimSet = $this->getClaimSet($scopeName);
106
            if (null === $claimSet) {
107
                continue;
108
            }
109
110
            $intersected = \array_intersect($claimSet->getClaims(), $keys);
111
112
            if (empty($intersected)) {
113
                continue;
114
            }
115
116
            $data = \array_filter(
117
                $claims,
118
                function ($key) use ($intersected) {
119
                    return \in_array($key, $intersected);
120
                },
121
                ARRAY_FILTER_USE_KEY
122
            );
123
124
            $claimData = \array_merge($claimData, $data);
125
        }
126
127
        return $claimData;
128
    }
129
130
    /**
131
     * Create a array default openID connect claims
132
     *
133
     * @see http://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims
134
     *
135
     * @return ClaimSetEntry[]
136
     */
137
    public static function getDefaultClaimSetEnties(): array
138
    {
139
        return [
140
            new ClaimSetEntry('profile', [
141
                'name',
142
                'family_name',
143
                'given_name',
144
                'middle_name',
145
                'nickname',
146
                'preferred_username',
147
                'profile',
148
                'picture',
149
                'website',
150
                'gender',
151
                'birthdate',
152
                'zoneinfo',
153
                'locale',
154
                'updated_at',
155
            ]),
156
            new ClaimSetEntry('email', [
157
                'email',
158
                'email_verified',
159
            ]),
160
            new ClaimSetEntry('address', [
161
                'address',
162
            ]),
163
            new ClaimSetEntry('phone', [
164
                'phone_number',
165
                'phone_number_verified',
166
            ]),
167
        ];
168
    }
169
}
170