This repository has been archived by the owner on May 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoauth2callback.php
180 lines (168 loc) · 6.09 KB
/
oauth2callback.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
175
176
177
178
179
180
<?php
/*
* ZenFusion OAuth - A Google OAuth authentication module for Dolibarr
* Copyright (C) 2011-2016 Raphaël Doursenaud <[email protected]>
* Copyright (C) 2012-2013 Cédric Salvador <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file callback.php
* Callback page for OAuth
*
* \ingroup zenfusionoauth
* \authors Raphaël Doursenaud <[email protected]>
* \authors Cédric Salvador <[email protected]>
*/
use \zenfusion\oauth\Oauth2Client;
use \zenfusion\oauth\Oauth2Exception;
use \zenfusion\oauth\TokenStorage;
/**
*
* Handles GET requests
*
* @param string $uri
* @param Oauth2Client $client
*
* @return string|null Server reply
*/
// FIXME: Factorize with requests.lib.php
function getRequest($uri, $client)
{
$get = new Google_Http_Request($uri, 'GET');
$val = $client->getAuth()->authenticatedRequest($get);
dol_syslog('GET response code: ' . $val->getResponseHttpCode(), LOG_INFO);
if ($val->getResponseHttpCode() == 401) {
$_SESSION['warning'] = 'HTTP401Unauthorized';
return null;
} elseif ($val->getResponseHttpCode() == 404) {
//404, no error message
return null;
} else {
if ($val->getResponseHttpCode() != 401
&& $val->getResponseHttpCode() != 200
&& $val->getResponseHttpCode() != 404
) {
// FIXME: use a library to handle these errors separetely
$_SESSION['warning'] = 'UnknownHTTPError';
return null;
}
}
$rep = $val->getResponseBody();
// FIXME: validate response, it might not be what we expect
return $rep;
}
// Load Dolibarr environment
if (false === (@include '../../main.inc.php')) { // From htdocs directory
require '../../../main.inc.php'; // From "custom" directory
}
require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php';
require_once DOL_DOCUMENT_ROOT . '/core/lib/usergroups.lib.php';
require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php';
require_once './class/TokenStorage.class.php';
require_once './class/Oauth2Client.class.php';
require_once './lib/scopes.lib.php';
require_once './inc/oauth.inc.php';
global $db, $langs, $user;
$mesg = ""; // User message
$dolibarr_version = versiondolibarrarray();
$langs->load('zenfusionoauth@zenfusionoauth');
$langs->load('admin');
$langs->load('users');
// Defini si peux lire/modifier permisssions
//$canreaduser = ($user->admin || $user->rights->user->user->lire);
$state = GETPOST('state', 'int');
$callback_error = GETPOST('error', 'alpha');
$code = GETPOST('code', 'alpha');
$retry = false; // Do we have an error ?
// On callback, the state is the user id
if ((!$state || !$code || !$user->rights->zenfusionoauth->use) && !$user->admin) {
accessforbidden();
} else {
/*
* Controller
*/
// Create a new User instance to display tabs
$doluser = new User($db);
// Load current user's informations
$doluser->fetch($state);
// Create an object to use llx_zenfusion_oauth table
$tokenstorage = new TokenStorage($db);
$tokenstorage->fetch($state);
// Google API client
try {
$client = new Oauth2Client();
} catch (Oauth2Exception $e) {
// Ignore
}
if ($callback_error) {
$tokenstorage->delete($state);
header(
'refresh:0;url=' . dol_buildpath(
'/zenfusionoauth/initoauth.php',
1
) . '?id=' . $state
);
} else {
try {
$cback = dol_buildpath('/zenfusionoauth/oauth2callback.php', 2);
$client->setRedirectUri($cback);
$client->authenticate($_GET['code']);
} catch (Google_Auth_Exception $e) {
dol_syslog("Access token " . $e->getMessage());
$retry = true;
}
$token = $client->getAccessToken();
// Save the access token into database
dol_syslog($script_file . " CREATE", LOG_DEBUG);
$tokenstorage->setTokenFromBundle($token);
$tokenstorage->oauth_id = null;
$access_token = $tokenstorage->token->getAccessToken();
$info = getRequest(GOOGLE_TOKEN_INFO . $access_token, $client);
$info = json_decode($info);
$tokenstorage->oauth_id = $info->user_id;
$ok = false;
if ($info->verified_email && $info->email == $doluser->email) {
$db_id = $tokenstorage->update($doluser);
if ($db_id < 0) {
dol_print_error($db, $tokenstorage->error);
} else {
$ok = true;
}
} else {
if (($dolibarr_version[0] == 3 && $dolibarr_version[1] >= 7) || $dolibarr_version[0] > 3) { // DOL_VERSION >= 3.7
setEventMessages($langs->trans('NotSameEmail'), '', 'errors');
} elseif ($dolibarr_version[0] == 3 && $dolibarr_version[1] >= 3) { // DOL_VERSION >= 3.3
/** @noinspection PhpDeprecationInspection */
setEventMessage($langs->trans('NotSameEmail'), 'errors');
} else {
$mesg = '&mesg=' . urlencode(
'<div class="error">' .
$langs->trans('NotSameEmail') . '</div>'
);
}
$tokenstorage->delete($state);
}
// Refresh the page to prevent multiple insertions
header(
'refresh:0;url=' . dol_buildpath(
'/zenfusionoauth/initoauth.php',
1
) . '?id=' . $state . '&ok=' . (int)$ok . $mesg
);
exit;
}
}
$db->close();
llxFooter();