1
|
|
|
<?php
|
2
|
|
|
namespace Auth;
|
3
|
|
|
require dirname(__FILE__).'/../libs/google/src/Google/autoload.php';
|
4
|
|
|
|
5
|
|
|
class GoogleAuthenticator extends Authenticator
|
6
|
|
|
{
|
7
|
|
|
protected $client;
|
8
|
|
|
protected $token = null;
|
9
|
|
|
|
10
|
|
|
public function __construct($params)
|
11
|
|
|
{
|
12
|
|
|
parent::__construct($params);
|
13
|
|
|
if(!isset($params['client_secrets_path']))
|
14
|
|
|
{
|
15
|
|
|
throw new \Exception('Missing required parameter client_secrets_path!');
|
16
|
|
|
}
|
17
|
|
|
if(!isset($params['redirect_url']))
|
18
|
|
|
{
|
19
|
|
|
$params['redirect_url'] = 'https://'.$_SERVER['HTTP_HOST'].'/oauth2callback.php?src=google';
|
20
|
|
|
}
|
21
|
|
|
$this->token = \FlipSession::getVar('GoogleToken', null);
|
|
|
|
|
22
|
|
|
$this->client = new \Google_Client();
|
23
|
|
|
$this->client->setAuthConfigFile($params['client_secrets_path']);
|
24
|
|
|
$this->client->addScope(array(\Google_Service_Oauth2::USERINFO_PROFILE, \Google_Service_Oauth2::USERINFO_EMAIL));
|
25
|
|
|
$this->client->setRedirectUri($params['redirect_url']);
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* Get the link to login using this method
|
30
|
|
|
*
|
31
|
|
|
* @return string The link to login using this method
|
32
|
|
|
*/
|
33
|
|
|
public function getSupplementLink()
|
34
|
|
|
{
|
35
|
|
|
$authUrl = $this->client->createAuthUrl();
|
36
|
|
|
return '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"><img src="/img/common/google_sign_in.png" style="width: 2em;"/></a>';
|
|
|
|
|
37
|
|
|
}
|
38
|
|
|
|
39
|
|
|
public function authenticate($code, &$currentUser = false)
|
40
|
|
|
{
|
41
|
|
|
$googleUser = false;
|
42
|
|
|
try
|
43
|
|
|
{
|
44
|
|
|
$this->client->authenticate($code);
|
45
|
|
|
$this->token = $this->client->getAccessToken();
|
46
|
|
|
\FlipSession::setVar('GoogleToken', $this->token);
|
47
|
|
|
$oauth2Service = new \Google_Service_Oauth2($this->client);
|
48
|
|
|
$googleUser = $oauth2Service->userinfo->get();
|
49
|
|
|
}
|
50
|
|
|
catch(\Exception $ex)
|
51
|
|
|
{
|
52
|
|
|
return self::LOGIN_FAILED;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
$auth = \AuthProvider::getInstance();
|
56
|
|
|
$localUsers = $auth->getUsersByFilter(new \Data\Filter('mail eq '.$googleUser->email));
|
57
|
|
|
if($localUsers !== false && isset($localUsers[0]))
|
58
|
|
|
{
|
59
|
|
|
if($localUsers[0]->canLoginWith('google.com'))
|
60
|
|
|
{
|
61
|
|
|
$auth->impersonateUser($localUsers[0]);
|
62
|
|
|
return self::SUCCESS;
|
63
|
|
|
}
|
64
|
|
|
$currentUser = $localUsers[0];
|
65
|
|
|
return self::ALREADY_PRESENT;
|
66
|
|
|
}
|
67
|
|
|
else
|
68
|
|
|
{
|
69
|
|
|
$user = new PendingUser();
|
70
|
|
|
$user->setEmail($googleUser->email);
|
71
|
|
|
$user->setGivenName($googleUser->givenName);
|
72
|
|
|
$user->setLastName($googleUser->familyName);
|
73
|
|
|
$user->addLoginProvider('google.com');
|
74
|
|
|
$ret = $auth->activatePendingUser($user);
|
75
|
|
|
if($ret === false)
|
76
|
|
|
{
|
77
|
|
|
throw new \Exception('Unable to create user! '.$res);
|
78
|
|
|
}
|
79
|
|
|
return self::SUCCESS;
|
80
|
|
|
}
|
81
|
|
|
}
|
82
|
|
|
|
83
|
|
|
public function getUser($data = false)
|
84
|
|
|
{
|
85
|
|
|
if($data === false)
|
86
|
|
|
{
|
87
|
|
|
$data = $this->token;
|
88
|
|
|
}
|
89
|
|
|
try
|
90
|
|
|
{
|
91
|
|
|
$this->client->setAccessToken($data);
|
92
|
|
|
$oauth2Service = new \Google_Service_Oauth2($this->client);
|
93
|
|
|
$googleUser = $oauth2Service->userinfo->get();
|
94
|
|
|
$profileUser = array();
|
95
|
|
|
$profileUser['mail'] = $googleUser->email;
|
96
|
|
|
$profileUser['sn'] = $googleUser->familyName;
|
97
|
|
|
$profileUser['givenName'] = $googleUser->givenName;
|
98
|
|
|
$profileUser['displayName'] = $googleUser->name;
|
99
|
|
|
$profileUser['jpegPhoto'] = base64_encode(file_get_contents($googleUser->picture));
|
100
|
|
|
return $profileUser;
|
|
|
|
|
101
|
|
|
}
|
102
|
|
|
catch(\Exception $e)
|
103
|
|
|
{
|
104
|
|
|
return false;
|
|
|
|
|
105
|
|
|
}
|
106
|
|
|
}
|
107
|
|
|
}
|
108
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
|
109
|
|
|
?>
|
|
|
|
|
110
|
|
|
|
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: