1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Google |
4
|
|
|
* |
5
|
|
|
* @created 09.08.2018 |
6
|
|
|
* @author smiley <[email protected]> |
7
|
|
|
* @copyright 2018 smiley |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chillerlan\OAuth\Providers; |
12
|
|
|
|
13
|
|
|
use chillerlan\HTTP\Utils\MessageUtil; |
14
|
|
|
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, ProviderException}; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use function sprintf; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @see https://developers.google.com/identity/protocols/OAuth2WebServer |
20
|
|
|
* @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount |
21
|
|
|
* @see https://developers.google.com/oauthplayground/ |
22
|
|
|
*/ |
23
|
|
|
class Google extends OAuth2Provider implements CSRFToken{ |
24
|
|
|
|
25
|
|
|
public const SCOPE_EMAIL = 'email'; |
26
|
|
|
public const SCOPE_PROFILE = 'profile'; |
27
|
|
|
public const SCOPE_USERINFO_EMAIL = 'https://www.googleapis.com/auth/userinfo.email'; |
28
|
|
|
public const SCOPE_USERINFO_PROFILE = 'https://www.googleapis.com/auth/userinfo.profile'; |
29
|
|
|
|
30
|
|
|
protected string $authURL = 'https://accounts.google.com/o/oauth2/auth'; |
31
|
|
|
protected string $accessTokenURL = 'https://accounts.google.com/o/oauth2/token'; |
32
|
|
|
protected string $apiURL = 'https://www.googleapis.com'; |
33
|
|
|
protected ?string $userRevokeURL = 'https://myaccount.google.com/permissions'; |
34
|
|
|
protected ?string $apiDocs = 'https://developers.google.com/oauthplayground/'; |
35
|
|
|
protected ?string $applicationURL = 'https://console.developers.google.com/apis/credentials'; |
36
|
|
|
|
37
|
|
|
protected array $defaultScopes = [ |
38
|
|
|
self::SCOPE_EMAIL, |
39
|
|
|
self::SCOPE_PROFILE, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
public function me():ResponseInterface{ |
46
|
|
|
$response = $this->request('/userinfo/v2/me'); |
47
|
|
|
$status = $response->getStatusCode(); |
48
|
|
|
|
49
|
|
|
if($status === 200){ |
50
|
|
|
return $response; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$json = MessageUtil::decodeJSON($response); |
54
|
|
|
|
55
|
|
|
if(isset($json->error, $json->error->message)){ |
56
|
|
|
throw new ProviderException($json->error->message); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw new ProviderException(sprintf('user info error error HTTP/%s', $status)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|