dolibarr 18.0.6
oauthlogintokens.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014-2018 Frederic France <frederic.france@netlogic.fr>
4 * Copyright (C) 2020 Nicolas ZABOURI <info@inovea-conseil.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // This define $list and $supportedoauth2array
30require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
31use OAuth\Common\Storage\DoliStorage;
32
33// Load translation files required by the page
34$langs->loadLangs(array('admin', 'printing', 'oauth'));
35
36$action = GETPOST('action', 'aZ09');
37$mode = GETPOST('mode', 'alpha');
38$value = GETPOST('value', 'alpha');
39$varname = GETPOST('varname', 'alpha');
40$driver = GETPOST('driver', 'alpha');
41
42if (!empty($driver)) {
43 $langs->load($driver);
44}
45
46if (!$mode) {
47 $mode = 'setup';
48}
49
50if (!$user->admin) {
52}
53
54
55/*
56 * Action
57 */
58
59/*if (($mode == 'test' || $mode == 'setup') && empty($driver))
60{
61 setEventMessages($langs->trans('PleaseSelectaDriverfromList'), null);
62 header("Location: ".$_SERVER['PHP_SELF'].'?mode=config');
63 exit;
64}*/
65
66if ($action == 'setconst' && $user->admin) {
67 $error = 0;
68 $db->begin();
69
70 $setupconstarray = GETPOST('setupdriver', 'array');
71
72 foreach ($setupconstarray as $setupconst) {
73 //print '<pre>'.print_r($setupconst, true).'</pre>';
74
75 $constname = dol_escape_htmltag($setupconst['varname']);
76 $constvalue = dol_escape_htmltag($setupconst['value']);
77 $consttype = dol_escape_htmltag($setupconst['type']);
78 $constnote = dol_escape_htmltag($setupconst['note']);
79
80 $result = dolibarr_set_const($db, $constname, $constvalue, $consttype, 0, $constnote, $conf->entity);
81 if (!($result > 0)) {
82 $error++;
83 }
84 }
85
86 if (!$error) {
87 $db->commit();
88 setEventMessages($langs->trans("SetupSaved"), null);
89 } else {
90 $db->rollback();
91 dol_print_error($db);
92 }
93 $action = '';
94}
95
96if ($action == 'setvalue' && $user->admin) {
97 $db->begin();
98
99 $result = dolibarr_set_const($db, $varname, $value, 'chaine', 0, '', $conf->entity);
100 if (!($result > 0)) {
101 $error++;
102 }
103
104 if (!$error) {
105 $db->commit();
106 setEventMessages($langs->trans("SetupSaved"), null);
107 } else {
108 $db->rollback();
109 dol_print_error($db);
110 }
111 $action = '';
112}
113
114
115/*
116 * View
117 */
118
119// Define $urlwithroot
120$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
121$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
122//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
123
124$form = new Form($db);
125
126llxHeader('', $langs->trans("TokenManager"));
127
128$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
129print load_fiche_titre($langs->trans('ConfigOAuth'), $linkback, 'title_setup');
130
132
133print dol_get_fiche_head($head, 'tokengeneration', '', -1, '');
134
135if (GETPOST('error')) {
136 setEventMessages(GETPOST('error'), null, 'errors');
137}
138
139if ($mode == 'setup' && $user->admin) {
140 print '<span class="opacitymedium">'.$langs->trans("OAuthSetupForLogin")."</span><br><br>\n";
141
142 // Define $listinsetup
143 foreach ($conf->global as $key => $val) {
144 if (!empty($val) && preg_match('/^OAUTH_.*_ID$/', $key)) {
145 $provider = preg_replace('/_ID$/', '', $key);
146 $listinsetup[] = array(
147 $provider.'_NAME',
148 $provider.'_ID',
149 $provider.'_SECRET',
150 $provider.'_URLAUTHORIZE', // For custom oauth links
151 $provider.'_SCOPE' // For custom oauth links
152 );
153 }
154 }
155
156 $oauthstateanticsrf = bin2hex(random_bytes(128/8));
157
158 // $list is defined into oauth.lib.php to the list of supporter OAuth providers.
159 if (!empty($listinsetup)) {
160 foreach ($listinsetup as $key) {
161 $supported = 0;
162 $keyforsupportedoauth2array = $key[0]; // May be OAUTH_GOOGLE_NAME or OAUTH_GOOGLE_xxx_NAME
163 $keyforsupportedoauth2array = preg_replace('/^OAUTH_/', '', $keyforsupportedoauth2array);
164 $keyforsupportedoauth2array = preg_replace('/_NAME$/', '', $keyforsupportedoauth2array);
165 if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
166 $keybeforeprovider = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
167 $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
168 } else {
169 $keybeforeprovider = $keyforsupportedoauth2array;
170 $keyforprovider = '';
171 }
172 $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
173 $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
174
175
176 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : ''));
177
178 $shortscope = '';
179 if (getDolGlobalString($key[4])) {
180 $shortscope = getDolGlobalString($key[4]);
181 }
182 $state = $shortscope; // TODO USe a better state
183
184 // Define $urltorenew, $urltodelete, $urltocheckperms
185 if ($keyforsupportedoauth2array == 'OAUTH_GITHUB_NAME') {
186 // List of keys that will be converted into scopes (from constants 'SCOPE_state_in_uppercase' in file of service).
187 // We pass this param list in to 'state' because we need it before and after the redirect.
188
189 // Note: github does not accept csrf key inside the state parameter (only known values)
190 $urltorenew = $urlwithroot.'/core/modules/oauth/github_oauthcallback.php?shortscope='.urlencode($shortscope).'&state='.urlencode($shortscope).'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
191 $urltodelete = $urlwithroot.'/core/modules/oauth/github_oauthcallback.php?action=delete&token='.newToken().'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
192 $urltocheckperms = 'https://github.com/settings/applications/';
193 } elseif ($keyforsupportedoauth2array == 'OAUTH_GOOGLE_NAME') {
194 // List of keys that will be converted into scopes (from constants 'SCOPE_state_in_uppercase' in file of service).
195 // List of scopes for Google are here: https://developers.google.com/identity/protocols/oauth2/scopes
196 // We pass this key list into the param 'state' because we need it before and after the redirect.
197 $urltorenew = $urlwithroot.'/core/modules/oauth/google_oauthcallback.php?shortscope='.urlencode($shortscope).'&state='.urlencode($state).'-'.$oauthstateanticsrf.'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
198 $urltodelete = $urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&token='.newToken().'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
199 $urltocheckperms = 'https://security.google.com/settings/security/permissions';
200 } elseif (!empty($supportedoauth2array[$keyforsupportedoauth2array]['returnurl'])) {
201 $urltorenew = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array]['returnurl'].'?shortscope='.urlencode($shortscope).'&state='.urlencode($state).'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
202 $urltodelete = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array]['returnurl'].'?action=delete&token='.newToken().'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
203 $urltocheckperms = '';
204 } else {
205 $urltorenew = '';
206 $urltodelete = '';
207 $urltocheckperms = '';
208 }
209
210 if ($urltorenew) {
211 $urltorenew .= '&keyforprovider='.urlencode($keyforprovider);
212 }
213 if ($urltodelete) {
214 $urltodelete .= '&keyforprovider='.urlencode($keyforprovider);
215 }
216
217 // Show value of token
218 $tokenobj = null;
219 // Token
220 require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
221 // Dolibarr storage
222 $storage = new DoliStorage($db, $conf, $keyforprovider);
223 try {
224 // $OAUTH_SERVICENAME is for example 'Google-keyforprovider'
225 print '<!-- '.$OAUTH_SERVICENAME.' -->'."\n";
226 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
227 //print $storage->token.'<br>';
228 //print $tokenobj->getExtraParams()['id_token'].'<br>';
229 //print $tokenobj->getAccessToken().'<br>';
230 } catch (Exception $e) {
231 // Return an error if token not found
232 //print $e->getMessage();
233 }
234
235 // Set other properties
236 $refreshtoken = false;
237 $expiredat = '';
238
239 $expire = false;
240 // Is token expired or will token expire in the next 30 seconds
241 if (is_object($tokenobj)) {
242 $expire = ($tokenobj->getEndOfLife() !== $tokenobj::EOL_NEVER_EXPIRES && $tokenobj->getEndOfLife() !== $tokenobj::EOL_UNKNOWN && time() > ($tokenobj->getEndOfLife() - 30));
243 }
244 if ($key[1] != '' && $key[2] != '') {
245 if (is_object($tokenobj)) {
246 $refreshtoken = $tokenobj->getRefreshToken();
247
248 $endoflife = $tokenobj->getEndOfLife();
249 if ($endoflife == $tokenobj::EOL_NEVER_EXPIRES) {
250 $expiredat = $langs->trans("Never");
251 } elseif ($endoflife == $tokenobj::EOL_UNKNOWN) {
252 $expiredat = $langs->trans("Unknown");
253 } else {
254 $expiredat = dol_print_date($endoflife, "dayhour", 'tzuserrel');
255 }
256 }
257 }
258
259 $submit_enabled = 0;
260
261 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&amp;driver='.$driver.'" autocomplete="off">';
262 print '<input type="hidden" name="token" value="'.newToken().'">';
263 print '<input type="hidden" name="action" value="setconst">';
264
265 print '<div class="div-table-responsive-no-min">';
266 print '<table class="noborder centpercent">'."\n";
267
268 // Api Name
269 $label = $langs->trans($keyforsupportedoauth2array);
270 print '<tr class="liste_titre">';
271 print '<th class="titlefieldcreate">';
272 print img_picto('', $supportedoauth2array[$keyforsupportedoauth2array]['picto'], 'class="pictofixedwidth"');
273 if ($label == $keyforsupportedoauth2array) {
274 print $supportedoauth2array[$keyforsupportedoauth2array]['name'];
275 } else {
276 print $label;
277 }
278 if ($keyforprovider) {
279 print ' (<b>'.$keyforprovider.'</b>)';
280 } else {
281 print ' (<b>'.$langs->trans("NoName").'</b>)';
282 }
283 print '</th>';
284 print '<th></th>';
285 print '<th></th>';
286 print "</tr>\n";
287
288 print '<tr class="oddeven">';
289 print '<td'.(empty($key['required']) ? '' : ' class="required"').'>';
290 //var_dump($key);
291 print $langs->trans("OAuthIDSecret").'</td>';
292 print '<td>';
293 print '<span class="opacitymedium">'.$langs->trans("SeePreviousTab").'</span>';
294 print '</td>';
295 print '<td>';
296 print '</td>';
297 print '</tr>'."\n";
298
299 // Scopes
300 print '<tr class="oddeven">';
301 print '<td>'.$langs->trans("Scopes").'</td>';
302 print '<td colspan="2">';
303 $currentscopes = getDolGlobalString($key[4]);
304 print $currentscopes;
305 print '</td></tr>';
306
307 print '<tr class="oddeven">';
308 print '<td'.(empty($key['required']) ? '' : ' class="required"').'>';
309 //var_dump($key);
310 print $langs->trans("IsTokenGenerated");
311 print '</td>';
312 print '<td>';
313 if (is_object($tokenobj)) {
314 print $form->textwithpicto(yn(1), $langs->trans("HasAccessToken").' : '.dol_print_date($storage->date_modification, 'dayhour').' state='.dol_escape_htmltag($storage->state));
315 } else {
316 print '<span class="opacitymedium">'.$langs->trans("NoAccessToken").'</span>';
317 }
318 print '</td>';
319 print '<td width="50%">';
320 // Links to delete/checks token
321 if (is_object($tokenobj)) {
322 //test on $storage->hasAccessToken($OAUTH_SERVICENAME) ?
323 if ($urltodelete) {
324 print '<a class="button smallpaddingimp" href="'.$urltodelete.'">'.$langs->trans('DeleteAccess').'</a><br>';
325 } else {
326 print '<span class="opacitymedium">'.$langs->trans('GoOnTokenProviderToDeleteToken').'</span><br>';
327 }
328 }
329 // Request remote token
330 if ($urltorenew) {
331 print '<a class="button smallpaddingimp" href="'.$urltorenew.'">'.$langs->trans('GetAccess').'</a>';
332 print $form->textwithpicto('', $langs->trans('RequestAccess'));
333 print '<br>';
334 }
335 // Check remote access
336 if ($urltocheckperms) {
337 print '<br>'.$langs->trans("ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME).': <a href="'.$urltocheckperms.'" target="_'.strtolower($OAUTH_SERVICENAME).'">'.$urltocheckperms.'</a>';
338 }
339 print '</td>';
340 print '</tr>';
341
342 print '<tr class="oddeven">';
343 print '<td'.(empty($key['required']) ? '' : ' class="required"').'>';
344 //var_dump($key);
345 print $langs->trans("Token").'</td>';
346 print '<td colspan="2">';
347
348 if (is_object($tokenobj)) {
349 $tokentoshow = $tokenobj->getAccessToken();
350 print '<span class="" title="'.dol_escape_htmltag($tokentoshow).'">'.showValueWithClipboardCPButton($tokentoshow, 1, dol_trunc($tokentoshow, 32)).'</span><br>';
351 //print 'Refresh: '.$tokenobj->getRefreshToken().'<br>';
352 //print 'EndOfLife: '.$tokenobj->getEndOfLife().'<br>';
353 //var_dump($tokenobj->getExtraParams());
354 /*print '<br>Extra: <br><textarea class="quatrevingtpercent">';
355 print ''.join(',',$tokenobj->getExtraParams());
356 print '</textarea>';*/
357 }
358 print '</td>';
359 print '</tr>'."\n";
360
361 if (is_object($tokenobj)) {
362 // Token refresh
363 print '<tr class="oddeven">';
364 print '<td'.(empty($key['required']) ? '' : ' class="required"').'>';
365 //var_dump($key);
366 print $langs->trans("TOKEN_REFRESH");
367 print '</td>';
368 print '<td colspan="2">';
369 print '<span class="" title="'.dol_escape_htmltag($refreshtoken).'">'.showValueWithClipboardCPButton($refreshtoken, 1, dol_trunc($refreshtoken, 32)).'</span>';
370 print '</td>';
371 print '</tr>';
372
373 // Token expired
374 print '<tr class="oddeven">';
375 print '<td'.(empty($key['required']) ? '' : ' class="required"').'>';
376 //var_dump($key);
377 print $langs->trans("TOKEN_EXPIRED");
378 print '</td>';
379 print '<td colspan="2">';
380 print yn($expire);
381 print '</td>';
382 print '</tr>';
383
384 // Token expired at
385 print '<tr class="oddeven">';
386 print '<td'.(empty($key['required']) ? '' : ' class="required"').'>';
387 //var_dump($key);
388 print $langs->trans("TOKEN_EXPIRE_AT");
389 print '</td>';
390 print '<td colspan="2">';
391 print $expiredat;
392 print '</td>';
393 print '</tr>';
394 }
395
396 print '</table>';
397 print '</div>';
398
399 if (!empty($driver)) {
400 if ($submit_enabled) {
401 print $form->buttonsSaveCancel("Modify", '');
402 }
403 }
404
405 print '</form>';
406 print '<br>';
407 }
408 }
409}
410
411if ($mode == 'test' && $user->admin) {
412 print $langs->trans('PrintTestDesc'.$driver)."<br><br>\n";
413
414 print '<div class="div-table-responsive-no-min">';
415 print '<table class="noborder centpercent">';
416 if (!empty($driver)) {
417 require_once DOL_DOCUMENT_ROOT.'/core/modules/printing/'.$driver.'.modules.php';
418 $classname = 'printing_'.$driver;
419 $langs->load($driver);
420 $printer = new $classname($db);
421 //print '<pre>'.print_r($printer, true).'</pre>';
422 if (count($printer->getlistAvailablePrinters())) {
423 if ($printer->listAvailablePrinters() == 0) {
424 print $printer->resprint;
425 } else {
426 setEventMessages($printer->error, $printer->errors, 'errors');
427 }
428 } else {
429 print $langs->trans('PleaseConfigureDriverfromList');
430 }
431 }
432
433 print '</table>';
434 print '</div>';
435}
436
437if ($mode == 'userconf' && $user->admin) {
438 print $langs->trans('PrintUserConfDesc'.$driver)."<br><br>\n";
439
440 print '<div class="div-table-responsive">';
441 print '<table class="noborder centpercent">';
442 print '<tr class="liste_titre">';
443 print '<th>'.$langs->trans("User").'</th>';
444 print '<th>'.$langs->trans("PrintModule").'</th>';
445 print '<th>'.$langs->trans("PrintDriver").'</th>';
446 print '<th>'.$langs->trans("Printer").'</th>';
447 print '<th>'.$langs->trans("PrinterLocation").'</th>';
448 print '<th>'.$langs->trans("PrinterId").'</th>';
449 print '<th>'.$langs->trans("NumberOfCopy").'</th>';
450 print '<th class="center">'.$langs->trans("Delete").'</th>';
451 print "</tr>\n";
452 $sql = "SELECT p.rowid, p.printer_name, p.printer_location, p.printer_id, p.copy, p.module, p.driver, p.userid, u.login";
453 $sql .= " FROM ".MAIN_DB_PREFIX."printing as p, ".MAIN_DB_PREFIX."user as u WHERE p.userid = u.rowid";
454 $resql = $db->query($sql);
455 while ($obj = $db->fetch_object($resql)) {
456 print '<tr class="oddeven">';
457 print '<td>'.$obj->login.'</td>';
458 print '<td>'.$obj->module.'</td>';
459 print '<td>'.$obj->driver.'</td>';
460 print '<td>'.$obj->printer_name.'</td>';
461 print '<td>'.$obj->printer_location.'</td>';
462 print '<td>'.$obj->printer_id.'</td>';
463 print '<td>'.$obj->copy.'</td>';
464 print '<td class="center">'.img_picto($langs->trans("Delete"), 'delete').'</td>';
465 print "</tr>\n";
466 }
467 print '</table>';
468 print '</div>';
469}
470
471print dol_get_fiche_end();
472
473// End of page
474llxFooter();
475$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:56
llxFooter()
Empty footer.
Definition wrapper.php:70
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
showValueWithClipboardCPButton($valuetocopy, $showonlyonhover=1, $texttoshow='')
Create a button to copy $valuetocopy in the clipboard (for copy and paste feature).
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
oauthadmin_prepare_head()
Return array of tabs to used on pages to setup cron module.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.