Completed
Push — master ( 9945e0...74c6f6 )
by Patrick
09:00 queued 07:59
created

AppleAccessToken::__construct()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 33.2824

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 3
cts 16
cp 0.1875
rs 8.5226
c 0
b 0
f 0
cc 7
nc 17
nop 1
crap 33.2824
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 1
    public function __construct(array $options = [])
34
    {
35 1
        if (empty($options['id_token'])) {
36
            throw new InvalidArgumentException('Required option not passed: "id_token"');
37
        }
38
39 1
        $decoded = JWT::decode($options['id_token'], $this->getAppleKey(), ['RS256']);
40
        $payload = json_decode(json_encode($decoded), true);
41
42
        $options['resource_owner_id'] = $payload['sub'];
43
44
        if (isset($payload['email_verified']) && $payload['email_verified']) {
45
            $options['email'] = $payload['email'];
46
        }
47
48
        if (isset($payload['is_private_email'])) {
49
            $this->isPrivateEmail = $payload['is_private_email'];
50
        }
51
52
        parent::__construct($options);
53
54
        if (isset($options['id_token'])) {
55
            $this->idToken = $options['id_token'];
56
        }
57
58
        if (isset($options['email'])) {
59
            $this->email = $options['email'];
60
        }
61
    }
62
63
    /**
64
     * @return array Apple's JSON Web Key
65
     */
66 1
    protected function getAppleKey()
67
    {
68 1
        return JWK::parseKeySet(file_get_contents('https://appleid.apple.com/auth/keys'))['AIDOPK1'];
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getIdToken()
75
    {
76
        return $this->idToken;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getEmail()
83
    {
84
        return $this->email;
85
    }
86
87
    /**
88
     * @return boolean
89
     */
90
    public function isPrivateEmail()
91
    {
92
        return $this->isPrivateEmail;
93
    }
94
}
95