1 | <?php |
||
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 | { |
||
39 | 2 | if (empty($options['id_token'])) { |
|
40 | throw new InvalidArgumentException('Required option not passed: "id_token"'); |
||
41 | } |
||
42 | |||
43 | 2 | $decoded = null; |
|
44 | 2 | $keys = $this->getAppleKey(); |
|
45 | 2 | $last = end($keys); |
|
46 | 2 | foreach ($keys as $key) { |
|
47 | try { |
||
48 | 2 | $decoded = JWT::decode($options['id_token'], $key, ['RS256']); |
|
49 | 1 | break; |
|
50 | 1 | } catch (\Exception $exception) { |
|
51 | 1 | if ($last === $key) { |
|
52 | 1 | throw $exception; |
|
53 | } |
||
54 | } |
||
55 | } |
||
56 | 1 | if (null === $decoded) { |
|
57 | throw new \Exception('Got no data within "id_token"!'); |
||
58 | } |
||
59 | 1 | $payload = json_decode(json_encode($decoded), true); |
|
60 | |||
61 | 1 | $options['resource_owner_id'] = $payload['sub']; |
|
62 | |||
63 | 1 | if (isset($payload['email_verified']) && $payload['email_verified']) { |
|
64 | $options['email'] = $payload['email']; |
||
65 | } |
||
66 | |||
67 | 1 | if (isset($payload['is_private_email'])) { |
|
68 | $this->isPrivateEmail = $payload['is_private_email']; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | 2 | parent::__construct($options); |
|
73 | |||
74 | 2 | if (isset($options['id_token'])) { |
|
75 | 1 | $this->idToken = $options['id_token']; |
|
76 | } |
||
77 | |||
78 | 2 | if (isset($options['email'])) { |
|
79 | $this->email = $options['email']; |
||
80 | } |
||
81 | 2 | } |
|
82 | |||
83 | /** |
||
84 | * @return array Apple's JSON Web Key |
||
85 | */ |
||
86 | 2 | protected function getAppleKey() |
|
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | 1 | public function getIdToken() |
|
95 | { |
||
96 | 1 | return $this->idToken; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getEmail() |
||
106 | |||
107 | /** |
||
108 | * @return boolean |
||
109 | */ |
||
110 | public function isPrivateEmail() |
||
114 | } |
||
115 |