Completed
Push — 3.x ( 7d1111...e04e11 )
by Дмитрий
03:24
created

Apple::extractIdentity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 20
ccs 7
cts 9
cp 0.7778
crap 2.0438
rs 9.9332
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 * @author Alexander Fedyashov <[email protected]>
6
 */
7
declare(strict_types=1);
8
9
namespace SocialConnect\OpenIDConnect\Provider;
10
11
use SocialConnect\Common\ArrayHydrator;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Exception\InvalidArgumentException;
14
use SocialConnect\Common\Exception\Unsupported;
15
use SocialConnect\OpenIDConnect\AccessToken;
16
use SocialConnect\Provider\AccessTokenInterface;
17
use SocialConnect\OpenIDConnect\AbstractProvider;
18
19
/**
20
 * @link https://developer.apple.com/sign-in-with-apple/get-started/
21
 */
22
class Apple extends AbstractProvider
23
{
24
    const NAME = 'apple';
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getOpenIdUrl()
30
    {
31
        throw new Unsupported('Apple does not support openid-configuration url');
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function discover(): array
38
    {
39
        return [
40
            'jwks_uri' => 'https://appleid.apple.com/auth/keys'
41
        ];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function getBaseUri()
48
    {
49 1
        return 'https://appleid.apple.com/';
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function getAuthorizeUri()
56
    {
57 1
        return 'https://appleid.apple.com/auth/authorize';
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function getRequestTokenUri()
64
    {
65 1
        return 'https://appleid.apple.com/auth/token';
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function getName()
72
    {
73 1
        return self::NAME;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function extractIdentity(AccessTokenInterface $accessToken)
80
    {
81 1
        if (!$accessToken instanceof AccessToken) {
82
            throw new InvalidArgumentException(
83
                '$accessToken must be instance AccessToken'
84
            );
85
        }
86
87 1
        $jwt = $accessToken->getJwt();
88
89 1
        $hydrator = new ArrayHydrator([
90 1
            'sub' => 'id',
91
            'email' => 'email',
92
            'email_verified' => 'emailVerified '
93
        ]);
94
95
        /** @var User $user */
96 1
        $user = $hydrator->hydrate(new User(), $jwt->getPayload());
97
98 1
        return $user;
99
    }
100
101
    /**
102
     * I didnt find any API
103
     *
104
     * {@inheritdoc}
105
     */
106 1
    public function getIdentity(AccessTokenInterface $accessToken)
107
    {
108 1
        return $this->extractIdentity($accessToken);
109
    }
110
}
111