Completed
Push — master ( 519e67...4a45cd )
by Patrick
02:11
created

AppleAccessToken::getIdToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace League\OAuth2\Client\Token;
4
5
use Firebase\JWT\JWK;
6
use Firebase\JWT\JWT;
7
use InvalidArgumentException;
8
9
class AppleAccessToken extends AccessToken
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $idToken;
15
16
    /**
17
     * @var string
18
     */
19
    protected $email;
20
21
    /**
22
     * @var boolean
23
     */
24
    protected $isPrivateEmail;
25
26
    /**
27
     * Constructs an access token.
28
     *
29
     * @param array $options An array of options returned by the service provider
30
     *     in the access token request. The `access_token` option is required.
31
     * @throws InvalidArgumentException if `access_token` is not provided in `$options`.
32
     *
33
     * @throws \Exception
34
     */
35 1
    public function __construct(array $options = [])
36
    {
37 1
        if (empty($options['id_token'])) {
38
            throw new InvalidArgumentException('Required option not passed: "id_token"');
39
        }
40
41 1
        $decoded = null;
42 1
        $keys = $this->getAppleKey();
43 1
        $last = end($keys);
44 1
        foreach ($keys as $key)
45
        {
46
            try {
47 1
                $decoded = JWT::decode($options['id_token'], $key, ['RS256']);
48
                break;
49 1
            } catch (\Exception $exception) {
50 1
                if ($last === $key) {
51 1
                    throw $exception;
52
                }
53
            }
54
        }
55
        if (null === $decoded)
56
        {
57
            throw new \Exception('Got no data within "id_token"!');
58
        }
59
        $payload = json_decode(json_encode($decoded), true);
60
61
        $options['resource_owner_id'] = $payload['sub'];
62
63
        if (isset($payload['email_verified']) && $payload['email_verified']) {
64
            $options['email'] = $payload['email'];
65
        }
66
67
        if (isset($payload['is_private_email'])) {
68
            $this->isPrivateEmail = $payload['is_private_email'];
69
        }
70
71
        parent::__construct($options);
72
73
        if (isset($options['id_token'])) {
74
            $this->idToken = $options['id_token'];
75
        }
76
77
        if (isset($options['email'])) {
78
            $this->email = $options['email'];
79
        }
80
    }
81
82
    /**
83
     * @return array Apple's JSON Web Key
84
     */
85 1
    protected function getAppleKey()
86
    {
87 1
        return JWK::parseKeySet(file_get_contents('https://appleid.apple.com/auth/keys'));
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getIdToken()
94
    {
95
        return $this->idToken;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getEmail()
102
    {
103
        return $this->email;
104
    }
105
106
    /**
107
     * @return boolean
108
     */
109
    public function isPrivateEmail()
110
    {
111
        return $this->isPrivateEmail;
112
    }
113
}
114