|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace cvweiss\projectbase\Controller\auth\google; |
|
4
|
|
|
|
|
5
|
|
|
use cvweiss\projectbase\Config; |
|
6
|
|
|
use cvweiss\projectbase\Mongo; |
|
7
|
|
|
use cvweiss\projectbase\Session; |
|
8
|
|
|
|
|
9
|
|
|
class callback |
|
10
|
|
|
{ |
|
11
|
|
|
public function doGet($view, $params) |
|
12
|
|
|
{ |
|
13
|
|
|
unset($params); |
|
14
|
|
|
$this->validate($view); |
|
15
|
|
|
|
|
16
|
|
|
$ownerDetails = $this->getOwnerDetails(); |
|
17
|
|
|
|
|
18
|
|
|
$id = $ownerDetails->getID(); |
|
19
|
|
|
$user = Mongo::get()->findDoc("users", ['id' => $id], null, true); |
|
20
|
|
|
|
|
21
|
|
|
$user->setAll([ |
|
22
|
|
|
"id" => $id, |
|
23
|
|
|
"name" => $ownerDetails->getName(), |
|
24
|
|
|
"email" => $ownerDetails->getEmail(), |
|
25
|
|
|
"image" => $ownerDetails->getAvatar(), |
|
26
|
|
|
"oauth2" => "google" |
|
27
|
|
|
]); |
|
28
|
|
|
$user->save(); |
|
29
|
|
|
|
|
30
|
|
|
Session::getSession()->set("userID", $id); |
|
31
|
|
|
$view->redirect('/', 302); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function validate($view) |
|
35
|
|
|
{ |
|
36
|
|
|
$session = Session::getSession(); |
|
37
|
|
|
$error = filter_input(INPUT_GET, 'error'); |
|
38
|
|
|
$state = filter_input(INPUT_GET, 'state'); |
|
39
|
|
|
if (!empty($error) || (empty($state) || ($state !== $session->get('oauth2state')))) { |
|
40
|
|
|
$view->redirect('/logout/'); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function getProvider() |
|
45
|
|
|
{ |
|
46
|
|
|
$auth = Config::getInstance()->get("oauth2"); |
|
47
|
|
|
$google = $auth['google']; |
|
48
|
|
|
$provider = new \League\OAuth2\Client\Provider\Google([ |
|
49
|
|
|
'clientId' => $google['client_id'], |
|
50
|
|
|
'clientSecret' => $google['client_secret'], |
|
51
|
|
|
'redirectUri' => $google['redirect_uris'][0] |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
return $provider; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function getOwnerDetails() |
|
58
|
|
|
{ |
|
59
|
|
|
$code = filter_input(INPUT_GET, 'code'); |
|
60
|
|
|
$provider = $this->getProvider(); |
|
61
|
|
|
$token = $provider->getAccessToken('authorization_code', ['code' => $code]); |
|
62
|
|
|
|
|
63
|
|
|
return $provider->getResourceOwner($token); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|