Issues (1)

src/Auth.php (1 issue)

1
<?php
2
3
namespace LeandroFerreiraMa\GoogleAuth;
4
5
class Auth
6
{
7
    private string $apiUrl;
8
    private string $endpoint;
9
    private array $fields;
10
    private string $method;
11
    protected ?object $response;
12
13
    public function __construct()
14
    {
15
        $this->apiUrl = 'https://www.googleapis.com/oauth2/v4';
16
        $this->method = 'POST';
17
        $this->endpoint = 'token';
18
    }
19
20
    public function url(string $clientId, string $uriRedirect, string $scope = 'https://www.googleapis.com/auth/calendar'): string
21
    {
22
        $param = [
23
            'scope' => $scope,
24
            'redirect_uri' => $uriRedirect,
25
            'response_type' => 'code',
26
            'access_type' => 'offline',
27
            'client_id' => $clientId,
28
            'prompt' => 'select_account consent'
29
        ];
30
31
        $param = array_map("strip_tags", $param);
32
33
        return "https://accounts.google.com/o/oauth2/auth?".http_build_query($param);
34
    }
35
36
    public function accessToken(string $clientId, string $uriRedirect, string $clientSecret, string $code): ?object
37
    {
38
        $this->fields = array_map("strip_tags", [
39
            'client_id' => $clientId,
40
            'redirect_uri' => $uriRedirect,
41
            'client_secret' => $clientSecret,
42
            'code' => $code,
43
            'grant_type' => 'authorization_code'
44
        ]);
45
46
        $this->dispatch();
47
        return $this->response;
48
    }
49
50
    public function refreshToken(string $clientId, string $clientSecret, string $refreshToken): ?object
51
    {
52
        $this->fields = array_map("strip_tags", [
53
            'client_id' => $clientId,
54
            'client_secret' => $clientSecret, 
55
            'refresh_token' => $refreshToken,
56
            'access_type' => 'consent',
57
            'grant_type' => 'refresh_token'
58
        ]);
59
60
        $this->dispatch();
61
        return $this->response;
62
    }
63
64
    private function dispatch(): void
65
    {
66
        $curl = curl_init();
67
        $fields = http_build_query($this->fields);
68
69
        curl_setopt_array($curl, array(
70
            CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}",
71
            CURLOPT_RETURNTRANSFER => true,
72
            CURLOPT_MAXREDIRS => 10,
73
            CURLOPT_TIMEOUT => 30,
74
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
75
            CURLOPT_CUSTOMREQUEST => $this->method,
76
            CURLOPT_POSTFIELDS => $fields,
77
            CURLOPT_HTTPHEADER => [],
78
        ));
79
80
        $this->response = json_decode(curl_exec($curl));
0 ignored issues
show
It seems like curl_exec($curl) can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $this->response = json_decode(/** @scrutinizer ignore-type */ curl_exec($curl));
Loading history...
81
        curl_close($curl);
82
    }
83
}