|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright 2015 Alexey Maslov <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace alxmsl\Google\OAuth2; |
|
19
|
|
|
|
|
20
|
|
|
use alxmsl\Google\OAuth2\Response\Error; |
|
21
|
|
|
use alxmsl\Google\OAuth2\Response\Token; |
|
22
|
|
|
use alxmsl\Network\Exception\HttpClientErrorCodeException; |
|
23
|
|
|
use alxmsl\Network\Exception\HttpCodeException; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class for login via web server applications |
|
27
|
|
|
* @author alxmsl |
|
28
|
|
|
* @date 1/13/13 |
|
29
|
|
|
*/ |
|
30
|
|
|
class WebServerApplication extends Client { |
|
31
|
|
|
/** |
|
32
|
|
|
* Response type constants |
|
33
|
|
|
*/ |
|
34
|
|
|
const RESPONSE_TYPE_CODE = 'code'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Access type constants |
|
38
|
|
|
*/ |
|
39
|
|
|
const ACCESS_TYPE_ONLINE = 'online', |
|
40
|
|
|
ACCESS_TYPE_OFFLINE = 'offline'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Approval constants |
|
44
|
|
|
*/ |
|
45
|
|
|
const APPROVAL_PROMPT_AUTO = 'auto', |
|
46
|
|
|
APPROVAL_PROMPT_FORCE = 'force'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Grant type constants |
|
50
|
|
|
*/ |
|
51
|
|
|
const GRANT_TYPE_AUTHORIZATION = 'authorization_code', |
|
52
|
|
|
GRANT_TYPE_REFRESH = 'refresh_token'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Google Api endpoints |
|
56
|
|
|
*/ |
|
57
|
|
|
const ENDPOINT_INITIAL_REQUEST = 'https://accounts.google.com/o/oauth2/auth', |
|
58
|
|
|
ENDPOINT_ACCESS_TOKEN_REQUEST = 'https://accounts.google.com/o/oauth2/token', |
|
59
|
|
|
ENDPOINT_REVOKE_TOKEN = 'https://accounts.google.com/o/oauth2/revoke'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var Token access token |
|
63
|
|
|
*/ |
|
64
|
|
|
private $Token = null; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Setter for token value |
|
68
|
|
|
* @param Token $Token token object |
|
69
|
|
|
* @return Client self |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function setToken(Token $Token) { |
|
72
|
1 |
|
$this->Token = $Token; |
|
73
|
1 |
|
$this->setAccessToken($this->Token->getAccessToken()); |
|
74
|
1 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Getter for token |
|
79
|
|
|
* @return Token access token value |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function getToken() { |
|
82
|
2 |
|
return $this->Token; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Method for create authorization url |
|
87
|
|
|
* @param string[] $scopes set of permissions |
|
88
|
|
|
* @param string $state something state |
|
89
|
|
|
* @param string $responseType type of the response |
|
90
|
|
|
* @param string $accessType type of the access. Online or offline |
|
91
|
|
|
* @param string $approvalPrompt type of re-prompted user consent |
|
92
|
|
|
* @return string url string for user authorization |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function createAuthUrl(array $scopes, $state = '', $responseType = self::RESPONSE_TYPE_CODE, $accessType = self::ACCESS_TYPE_ONLINE, $approvalPrompt = self::APPROVAL_PROMPT_AUTO, $loginHint = '') { |
|
95
|
|
|
$parameters = [ |
|
96
|
1 |
|
'response_type=' . $responseType, |
|
97
|
1 |
|
'client_id=' . $this->getClientId(), |
|
98
|
1 |
|
'redirect_uri=' . urlencode($this->getRedirectUri()), |
|
99
|
1 |
|
'scope=' . urlencode(implode(' ', $scopes)), |
|
100
|
1 |
|
'access_type=' . $accessType, |
|
101
|
1 |
|
'approval_prompt=' . $approvalPrompt, |
|
102
|
1 |
|
]; |
|
103
|
1 |
|
if (!empty($state)) { |
|
104
|
1 |
|
$parameters[] = sprintf('state=%s', $state); |
|
105
|
1 |
|
} |
|
106
|
1 |
|
if (!empty($loginHint)) { |
|
107
|
1 |
|
$parameters[] = sprintf('login_hint=%s', $loginHint); |
|
108
|
1 |
|
} |
|
109
|
1 |
|
return self::ENDPOINT_INITIAL_REQUEST . '?' . implode('&', $parameters); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get access token by user authorization code |
|
114
|
|
|
* @param string $code user authorization code |
|
115
|
|
|
* @return Error|Token Google Api response object |
|
116
|
|
|
*/ |
|
117
|
|
|
public function authorizeByCode($code) { |
|
118
|
|
|
$Request = $this->getRequest(self::ENDPOINT_ACCESS_TOKEN_REQUEST); |
|
119
|
|
|
$Request->addPostField('code', $code) |
|
120
|
|
|
->addPostField('client_id', $this->getClientId()) |
|
121
|
|
|
->addPostField('client_secret', $this->getClientSecret()) |
|
122
|
|
|
->addPostField('redirect_uri', $this->getRedirectUri()) |
|
123
|
|
|
->addPostField('grant_type', self::GRANT_TYPE_AUTHORIZATION); |
|
124
|
|
|
try { |
|
125
|
|
|
$Token = Token::initializeByString($Request->send()); |
|
126
|
|
|
$this->setToken($Token); |
|
127
|
|
|
return $Token; |
|
128
|
|
|
} catch (HttpClientErrorCodeException $ex) { |
|
129
|
|
|
return Error::initializeByString($ex->getMessage()); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get access by refresh token |
|
135
|
|
|
* @param string $refreshToken refresh token |
|
136
|
|
|
* @return Error|Token Google Api response object |
|
137
|
|
|
*/ |
|
138
|
|
|
public function refresh($refreshToken) { |
|
139
|
|
|
$Request = $this->getRequest(self::ENDPOINT_ACCESS_TOKEN_REQUEST); |
|
140
|
|
|
$Request->addPostField('client_id', $this->getClientId()) |
|
141
|
|
|
->addPostField('client_secret', $this->getClientSecret()) |
|
142
|
|
|
->addPostField('refresh_token', $refreshToken) |
|
143
|
|
|
->addPostField('grant_type', self::GRANT_TYPE_REFRESH); |
|
144
|
|
|
try { |
|
145
|
|
|
$Token = Token::initializeByString($Request->send()); |
|
146
|
|
|
$this->setToken($Token); |
|
147
|
|
|
return $Token; |
|
148
|
|
|
} catch (HttpClientErrorCodeException $ex) { |
|
149
|
|
|
return Error::initializeByString($ex->getMessage()); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Revoke access or refresh token |
|
155
|
|
|
* If the token is an access token and it has a corresponding refresh token, the refresh token will also be revoked |
|
156
|
|
|
* @param string $token user access or refresh token |
|
157
|
|
|
* @return bool revoke token result |
|
158
|
|
|
*/ |
|
159
|
|
|
public function revoke($token) { |
|
160
|
|
|
$Request = $this->getRequest(self::ENDPOINT_REVOKE_TOKEN); |
|
161
|
|
|
$Request->addGetField('token', (string) $token); |
|
162
|
|
|
try { |
|
163
|
|
|
$Request->send(); |
|
164
|
|
|
return true; |
|
165
|
|
|
} catch (HttpCodeException $Ex) { |
|
166
|
|
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|