Passed
Pull Request — master (#1316)
by
unknown
33:27
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\ClaimSet;
6
use League\OAuth2\Server\Entities\ClaimSetEntry;
7
use League\OAuth2\Server\Entities\ClaimSetEntryInterface;
8
use League\OAuth2\Server\Entities\ClaimSetInterface;
9
use League\OAuth2\Server\Entities\ScopeEntityInterface;
10
11
/**
12
 * ClaimExtractor
13
 * 
14
 * @link https://github.com/steverhoades/oauth2-openid-connect-server
15
 * @author Steve Rhoades <[email protected]>
16
 * @author Marc Riemer <[email protected]>
17
 */
18
class ClaimExtractor
19
{
20
    /**
21
     * claimSets
22
     *
23
     * @var ClaimSetEntryInterface[]
24
     */
25
    protected $claimSets = [];
26
27
    protected $protectedClaims = ['profile', 'email', 'address', 'phone'];
28
29
    /**
30
     * ClaimExtractor constructor
31
     * 
32
     * @param ClaimSetEntryInterface[] $claimSets
33
     */
34
    public function __construct(array $claimSets = [])
35
    {
36
        $this->claimSets = self::getDefaultClaimSetEnties();        
37
        foreach ($claimSets as $claimSet) {
38
            $this->addClaimSet($claimSet);
39
        }
40
    }
41
42
    /**
43
     * @param ClaimSetEntryInterface $claimSetEntry
44
     * @return $this
45
     * @throws \InvalidArgumentException
46
     */
47
    public function addClaimSet(ClaimSetEntryInterface $claimSetEntry): ClaimExtractor
48
    {
49
        $scope = $claimSetEntry->getScope();
50
51
        if (in_array($scope, $this->protectedClaims) && !empty($this->claimSets[$scope])) {
52
            throw new \InvalidArgumentException(
53
                sprintf("%s is a protected scope and is pre-defined by the OpenID Connect specification.", $scope)
54
            );
55
        }
56
57
        $this->claimSets[$scope] = $claimSetEntry->getClaims();
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param string $scope
64
     * 
65
     * @return ClaimSetEntryInterface|null
66
     */
67
    public function getClaimSet(string $scope): ?ClaimSetEntryInterface
68
    {
69
        foreach($this->claimSets as $set) {
70
            if ($set->getScope() === $scope) {
71
                return $set;
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
     * For given scopes and aggregated claims get all claims that have been configured on the extractor.
89
     *
90
     * @param array $scopes
91
     * @param array $claims
92
     * @return array
93
     */
94
    public function extract(array $scopes, array $claims): array
95
    {
96
        $claimData  = [];
97
        $keys = array_keys($claims);
98
99
        foreach ($scopes as $scope) {
100
            $scopeName = ($scope instanceof ScopeEntityInterface) ? $scope->getIdentifier() : $scope;
101
102
            $claimSet = $this->getClaimSet($scopeName);
103
            if (null === $claimSet) {
104
                continue;
105
            }
106
107
            $intersected = array_intersect($claimSet->getClaims(), $keys);
108
109
            if (empty($intersected)) {
110
                continue;
111
            }
112
113
            $data = array_filter($claims,
114
                function($key) use ($intersected) {
115
                    return in_array($key, $intersected);
116
                },
117
                ARRAY_FILTER_USE_KEY
118
            );
119
120
            $claimData = array_merge($claimData, $data);
121
        }
122
123
        return $claimData;
124
    }
125
126
    /**
127
     * Create a array default openID connect claims
128
     *
129
     * @see http://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims
130
     * 
131
     * @return ClaimSetEntry[]
132
     */
133
    public static function getDefaultClaimSetEnties(): array
134
    {
135
        return [
136
            new ClaimSetEntry('profile', [
137
                'name',
138
                'family_name',
139
                'given_name',
140
                'middle_name',
141
                'nickname',
142
                'preferred_username',
143
                'profile',
144
                'picture',
145
                'website',
146
                'gender',
147
                'birthdate',
148
                'zoneinfo',
149
                'locale',
150
                'updated_at'
151
            ]),
152
            new ClaimSetEntry('email', [
153
                'email',
154
                'email_verified'
155
            ]),
156
            new ClaimSetEntry('address', [
157
                'address'
158
            ]),
159
            new ClaimSetEntry('phone', [
160
                'phone_number',
161
                'phone_number_verified'
162
            ])
163
        ];
164
    }
165
}
166