Completed
Push — master ( 754769...dfa1ae )
by Patrick
02:24
created

AppleAccessToken   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 74.36%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 118
ccs 29
cts 39
cp 0.7436
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 48 12
A getAppleKey() 0 10 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 GuzzleHttp\ClientInterface;
8
use InvalidArgumentException;
9
10
class AppleAccessToken extends AccessToken
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $idToken;
16
17
    /**
18
     * @var string
19
     */
20
    protected $email;
21
22
    /**
23
     * @var boolean
24
     */
25
    protected $isPrivateEmail;
26
27
    /**
28
     * @var ClientInterface
29
     */
30
    protected $httpClient;
31
32
    /**
33
     * Constructs an access token.
34
     *
35
     * @param array $options An array of options returned by the service provider
36
     *     in the access token request. The `access_token` option is required.
37
     * @throws InvalidArgumentException if `access_token` is not provided in `$options`.
38
     *
39
     * @throws \Exception
40
     */
41 3
    public function __construct(array $options = [], $httpClient)
42
    {
43 3
        $this->httpClient = $httpClient;
44
45 3
        if (array_key_exists('refresh_token', $options)) {
46 2
            if (empty($options['id_token'])) {
47
                throw new InvalidArgumentException('Required option not passed: "id_token"');
48
            }
49
50 2
            $decoded = null;
51 2
            $keys = $this->getAppleKey();
52 2
            $last = end($keys);
53 2
            foreach ($keys as $key) {
54
                try {
55 2
                    $decoded = JWT::decode($options['id_token'], $key, ['RS256']);
56 1
                    break;
57 1
                } catch (\Exception $exception) {
58 1
                    if ($last === $key) {
59 1
                        throw $exception;
60
                    }
61
                }
62
            }
63 1
            if (null === $decoded) {
64
                throw new \Exception('Got no data within "id_token"!');
65
            }
66 1
            $payload = json_decode(json_encode($decoded), true);
67
68 1
            $options['resource_owner_id'] = $payload['sub'];
69
70 1
            if (isset($payload['email_verified']) && $payload['email_verified']) {
71
                $options['email'] = $payload['email'];
72
            }
73
74 1
            if (isset($payload['is_private_email'])) {
75
                $this->isPrivateEmail = $payload['is_private_email'];
76
            }
77
        }
78
79 2
        parent::__construct($options);
80
81 2
        if (isset($options['id_token'])) {
82 1
            $this->idToken = $options['id_token'];
83
        }
84
85 2
        if (isset($options['email'])) {
86
            $this->email = $options['email'];
87
        }
88 2
    }
89
90
    /**
91
     * @return array Apple's JSON Web Key
92
     */
93 2
    protected function getAppleKey()
94
    {
95 2
        $response = $this->httpClient->request('GET', 'https://appleid.apple.com/auth/keys');
96
97 2
        if ($response) {
98 2
            return JWK::parseKeySet(json_decode($response->getBody()->getContents(), true));
99
        }
100
101
        return false;
102
    }
103
104
    /**
105
     * @return string
106
     */
107 1
    public function getIdToken()
108
    {
109 1
        return $this->idToken;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getEmail()
116
    {
117
        return $this->email;
118
    }
119
120
    /**
121
     * @return boolean
122
     */
123
    public function isPrivateEmail()
124
    {
125
        return $this->isPrivateEmail;
126
    }
127
}
128