Completed
Pull Request — master (#14)
by William
02:24
created

AppleAccessToken   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 5
dl 0
loc 112
ccs 30
cts 40
cp 0.75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 46 12
A getAppleKey() 0 11 2
A getIdToken() 0 4 1
A getEmail() 0 4 1
A isPrivateEmail() 0 4 1
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 3
    public function __construct(array $options = [])
36
    {
37 3
        if (array_key_exists('refresh_token', $options)) {
38 2
            if (empty($options['id_token'])) {
39
                throw new InvalidArgumentException('Required option not passed: "id_token"');
40
            }
41
42 2
            $decoded = null;
43 2
            $keys = $this->getAppleKey();
44 2
            $last = end($keys);
45 2
            foreach ($keys as $key) {
46
                try {
47 2
                    $decoded = JWT::decode($options['id_token'], $key, ['RS256']);
48 1
                    break;
49 1
                } catch (\Exception $exception) {
50 1
                    if ($last === $key) {
51 1
                        throw $exception;
52
                    }
53
                }
54
            }
55 1
            if (null === $decoded) {
56
                throw new \Exception('Got no data within "id_token"!');
57
            }
58 1
            $payload = json_decode(json_encode($decoded), true);
59
60 1
            $options['resource_owner_id'] = $payload['sub'];
61
62 1
            if (isset($payload['email_verified']) && $payload['email_verified']) {
63
                $options['email'] = $payload['email'];
64
            }
65
66 1
            if (isset($payload['is_private_email'])) {
67
                $this->isPrivateEmail = $payload['is_private_email'];
68
            }
69
        }
70
71 2
        parent::__construct($options);
72
73 2
        if (isset($options['id_token'])) {
74 1
            $this->idToken = $options['id_token'];
75
        }
76
77 2
        if (isset($options['email'])) {
78
            $this->email = $options['email'];
79
        }
80 2
    }
81
82
    /**
83
     * @return array Apple's JSON Web Key
84
     */
85 2
    protected function getAppleKey()
86
    {
87 2
        $client = new \GuzzleHttp\Client();
88 2
        $request = $client->get('https://appleid.apple.com/auth/keys');
89 2
        $response = $request->getBody();
90
91 2
        if ($response) {
92 2
            return JWK::parseKeySet(json_decode($response, true));
93
        }
94
        return false;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 1
    public function getIdToken()
101
    {
102 1
        return $this->idToken;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getEmail()
109
    {
110
        return $this->email;
111
    }
112
113
    /**
114
     * @return boolean
115
     */
116
    public function isPrivateEmail()
117
    {
118
        return $this->isPrivateEmail;
119
    }
120
}
121