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

ClaimExtractor::getClaimSets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
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 ClaimExtractorIntercace
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
     * {@inheritdoc}
91
     */
92
    public function extract(array $scopes, array $claims): array
93
    {
94
        $claimData  = [];
95
        $keys = \array_keys($claims);
96
97
        foreach ($scopes as $scope) {
98
            $scopeName = ($scope instanceof ScopeEntityInterface) ? $scope->getIdentifier() : $scope;
99
100
            $claimSet = $this->getClaimSet($scopeName);
101
            if (null === $claimSet) {
102
                continue;
103
            }
104
105
            $intersected = \array_intersect($claimSet->getClaims(), $keys);
106
107
            if (empty($intersected)) {
108
                continue;
109
            }
110
111
            $data = \array_filter(
112
                $claims,
113
                function ($key) use ($intersected) {
114
                    return \in_array($key, $intersected);
115
                },
116
                ARRAY_FILTER_USE_KEY
117
            );
118
119
            $claimData = \array_merge($claimData, $data);
120
        }
121
122
        return $claimData;
123
    }
124
125
    /**
126
     * Create a array default openID connect claims
127
     *
128
     * @see http://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims
129
     *
130
     * @return ClaimSetEntry[]
131
     */
132
    public static function getDefaultClaimSetEnties(): array
133
    {
134
        return [
135
            new ClaimSetEntry('profile', [
136
                'name',
137
                'family_name',
138
                'given_name',
139
                'middle_name',
140
                'nickname',
141
                'preferred_username',
142
                'profile',
143
                'picture',
144
                'website',
145
                'gender',
146
                'birthdate',
147
                'zoneinfo',
148
                'locale',
149
                'updated_at',
150
            ]),
151
            new ClaimSetEntry('email', [
152
                'email',
153
                'email_verified',
154
            ]),
155
            new ClaimSetEntry('address', [
156
                'address',
157
            ]),
158
            new ClaimSetEntry('phone', [
159
                'phone_number',
160
                'phone_number_verified',
161
            ]),
162
        ];
163
    }
164
}
165