-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleOAuth.class.php
executable file
·154 lines (133 loc) · 4.32 KB
/
GoogleOAuth.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_Oauth2Service.php';
class GoogleOAuth {
private $client;
private $oauth;
private $accessToken;
private $user;
public static function validateConfig($config){
$notOK = 5;
if(is_array($config)){
while(list($key,$value) = each($config)){
switch($key){
case 'app_name':
case 'client_id':
case 'client_secret':
case 'redirect_url':
case 'developer_key':
$notOK--;
break;
}
}
}
return ($notOK == 0);
}
public function __construct($config){
if(!self::validateConfig($config)){
throw new Exception("Missing config");
}
$client = new Google_Client();
$client->setApplicationName($config['app_name']);
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
$client->setRedirectUri($config['redirect_url']);
$client->setDeveloperKey($config['developer_key']);
$client->setApprovalPrompt($config['approval_prompt']);
$client->setAccessType($config['access_type']);
$this->client = $client;
$this->oauth2 = new Google_Oauth2Service($client);
}
public function getAuthURL($scope,$state){
if(trim(state)){
$this->client->setState($state);
}
if(!trim($scope)){
$scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
}
$this->client->setScopes($scope);
return $this->client->createAuthUrl();
}
public function getAccessToken(){
$this->accessToken = $this->client->getAccessToken();
return $this->accessToken;
}
public function setAccessToken($token){
$this->client->setAccessToken($token);
$this->accessToken = $token;
}
public function revokeToken(){
return $this->client->revokeToken();
}
public function authenticate($code){
$token = $this->client->authenticate($code);
if(!empty($token)){
$this->setAccessToken($token);
return $token;
}
}
public function getUserInfo(){
error_log("getting user info",3,"/tmp/google_oauth.debug.log");
if(!is_array($this->user)){
$this->user = $this->oauth2->userinfo->get();
}
return $this->user;
}
public function getEmailAddress(){
if(!is_array($this->user)){
$this->getUserInfo();
}
return filter_var($this->user['email'], FILTER_SANITIZE_EMAIL);
}
public function getID(){
if(!is_array($this->user)){
$this->getUserInfo();
}
return filter_var($this->user['id'], FILTER_SANITIZE_NUMBER_INT);
}
public function getUserDomain(){
if(!is_array($this->user)){
$this->getUserInfo();
}
$domain = $this->user['hd'];
if(!$domain){
$domain = substr($this->getEmailAddress(),strpos($this->getEmailAddress(),"@")+1);
}
return $domain;
}
private function getTokenVariable($var){
$token = json_decode($this->accessToken, true);
if(isset($token[$var])){
return $token[$var];
}
return null;
}
public function getTokenCreatedTime(){
return $this->getTokenVariable("created");
}
public function getTokenExpiresIn(){
return $this->getTokenVariable("expires_in");
}
public function getRefreshToken(){
error_log("Refresh TOKEN");
error_log($this->getTokenVariable("refresh_token"));
error_log($this->accessToken);
return $this->getTokenVariable("refresh_token");
}
public function validateToken(){
return !$this->client->isAccessTokenExpired();
}
public function refreshAccessToken($token){
if(empty($token)){
return false;
}
try{
$this->client->refreshToken($token);
$this->getAccessToken();
return true;
}catch(Google_AuthException $e){
error_log($e);
return false;
}
}
}