Completed
Push — master ( 0c132b...2e079d )
by Patrick
02:37
created

AppleAccessToken::isPrivateEmail()   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 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']);
0 ignored issues
show
Documentation introduced by
$this->getAppleKey() is of type resource, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 resource Apple 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