-
Notifications
You must be signed in to change notification settings - Fork 1
/
google.php
176 lines (151 loc) · 6.05 KB
/
google.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
use ohmy\Auth2;
/*auth-oauth: Google: Allow domain whitelisting #122*/
function isEmailAllowed($email, $domains_string) {
$email_domain = end(explode('@', $email, 2));
$domains = explode(',', $domains_string);
foreach ($domains as $domain) {
if (strcasecmp($email_domain, trim($domain)) === 0) {
return TRUE;
}
}
return strlen(trim($domains_string)) === 0;
}
class GoogleAuth {
var $config;
var $access_token;
function __construct($config) {
$this->config = $config;
}
function triggerAuth() {
/*https://github.com/osTicket/osTicket-plugins/issues/52*/
global $ost;
$self = $this;
return Auth2::legs(3)
->set('id', $this->config->get('g-client-id'))
->set('secret', $this->config->get('g-client-secret'))
/*https://github.com/osTicket/osTicket-plugins/issues/52*/
->set('redirect', rtrim($ost->getConfig()->getURL(), '/') . '/api/auth/ext')
->set('scope', 'profile email')
->authorize('https://accounts.google.com/o/oauth2/auth')
->access('https://accounts.google.com/o/oauth2/token')
->finally(function($data) use ($self) {
$self->access_token = $data['access_token'];
});
}
}
class GoogleStaffAuthBackend extends ExternalStaffAuthenticationBackend {
static $id = "google";
static $name = "Google";
static $sign_in_image_url = '';
static $service_name = "Google";
var $config;
function __construct($config) {
$this->config = $config;
GoogleStaffAuthBackend::$sign_in_image_url = $this->config->get('g-agents-button-url');
$this->google = new GoogleAuth($config);
}
function signOn() {
// TODO: Check session for auth token
if (isset($_SESSION[':oauth']['email'])) {
/*auth-oauth: Google: Allow domain whitelisting #122*/
if (!isEmailAllowed($_SESSION[':oauth']['email'], $this->config->get(
'g-allowed-domains-agents')))
$_SESSION['_staff']['auth']['msg'] = 'Login with this email address is not permitted';
else if (($staff = StaffSession::lookup(array('email' => $_SESSION[':oauth']['email'])))
&& $staff->getId()
) {
if (!$staff instanceof StaffSession) {
// osTicket <= v1.9.7 or so
$staff = new StaffSession($user->getId());
}
return $staff;
}
else
$_SESSION['_staff']['auth']['msg'] = 'Have your administrator create a local account';
}
}
static function signOut($user) {
parent::signOut($user);
unset($_SESSION[':oauth']);
}
function triggerAuth() {
parent::triggerAuth();
$google = $this->google->triggerAuth();
$google->GET(
"https://www.googleapis.com/oauth2/v1/tokeninfo?access_token="
. $this->google->access_token)
->then(function($response) {
require_once INCLUDE_DIR . 'class.json.php';
if ($json = JsonDataParser::decode($response->text))
$_SESSION[':oauth']['email'] = $json['email'];
Http::redirect(ROOT_PATH . 'scp');
}
);
}
}
class GoogleClientAuthBackend extends ExternalUserAuthenticationBackend {
static $id = "google.client";
static $name = "Google";
static $sign_in_image_url = '';
static $service_name = "Google";
function __construct($config) {
$this->config = $config;
GoogleClientAuthBackend::$sign_in_image_url = $this->config->get('g-clients-button-url');
$this->google = new GoogleAuth($config);
}
function supportsInteractiveAuthentication() {
return false;
}
function signOn() {
// TODO: Check session for auth token
if (isset($_SESSION[':oauth']['email'])) {
/*auth-oauth: Google: Allow domain whitelisting #122*/
if (!isEmailAllowed($_SESSION[':oauth']['email'], $this->config->get(
'g-allowed-domains-clients')))
$errors['err'] = 'Login with this email address is not permitted';
else if (($acct = ClientAccount::lookupByUsername($_SESSION[':oauth']['email']))
&& $acct->getId()
&& ($client = new ClientSession(new EndUser($acct->getUser()))))
return $client;
elseif (isset($_SESSION[':oauth']['profile'])) {
// TODO: Prepare ClientCreateRequest
$profile = $_SESSION[':oauth']['profile'];
$info = array(
'email' => $_SESSION[':oauth']['email'],
'name' => $profile['displayName'],
);
return new ClientCreateRequest($this, $info['email'], $info);
}
}
}
static function signOut($user) {
parent::signOut($user);
unset($_SESSION[':oauth']);
}
function triggerAuth() {
require_once INCLUDE_DIR . 'class.json.php';
parent::triggerAuth();
$google = $this->google->triggerAuth();
$token = $this->google->access_token;
$google->GET(
"https://www.googleapis.com/oauth2/v1/tokeninfo?access_token="
. $token)
->then(function($response) use ($google, $token) {
if (!($json = JsonDataParser::decode($response->text)))
return;
$_SESSION[':oauth']['email'] = $json['email'];
$google->GET(
"https://www.googleapis.com/plus/v1/people/me?access_token="
. $token)
->then(function($response) {
if (!($json = JsonDataParser::decode($response->text)))
return;
$_SESSION[':oauth']['profile'] = $json;
Http::redirect(ROOT_PATH . 'login.php');
}
);
}
);
}
}