diff --git a/htdocs/admin/oauthlogintokens.php b/htdocs/admin/oauthlogintokens.php index b41ae78d4c7..93306e8d13c 100644 --- a/htdocs/admin/oauthlogintokens.php +++ b/htdocs/admin/oauthlogintokens.php @@ -104,6 +104,11 @@ if ($action == 'setvalue' && $user->admin) * View */ +// Define $urlwithroot +$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root)); +$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file +//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current + $form = new Form($db); llxHeader('',$langs->trans("PrintingSetup")); @@ -127,70 +132,191 @@ if ($mode == 'setup' && $user->admin) if (in_array($key[0], array_keys($supportedoauth2array))) $supported=1; if (! $supported) continue; // show only supported + + $OAUTH_SERVICENAME='Unknown'; + if ($key[0] == 'OAUTH_GITHUB_NAME') + { + $OAUTH_SERVICENAME='GitHub'; + $urltorenew=$urlwithroot.'/core/modules/oauth/github_oauthcallback.php?state=user,public_repo&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php'); + $urltodelete=$urlwithroot.'/core/modules/oauth/github_oauthcallback.php?action=delete&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php'); + $urltocheckperms='https://github.com/settings/applications/'; + } + if ($key[0] == 'OAUTH_GOOGLE_NAME') + { + $OAUTH_SERVICENAME='Google'; + $urltorenew=$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?state=userinfo_email,userinfo_profile,cloud_print&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php'); + $urltodelete=$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php'); + $urltocheckperms='https://security.google.com/settings/security/permissions'; + } + + // Show value of token + $tokenobj=null; + // Token + require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php'; + require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php'; + // Dolibarr storage + $storage = new DoliStorage($db, $conf); + try + { + $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME); + } + catch(Exception $e) + { + // Return an error if token not found + } + + // Set other properties + $refreshtoken=false; + $expiredat=''; + + $expire = false; + // Is token expired or will token expire in the next 30 seconds + if (is_object($tokenobj)) { + $expire = ($tokenobj->getEndOfLife() !== $tokenobj::EOL_NEVER_EXPIRES && $tokenobj->getEndOfLife() !== $tokenobj::EOL_UNKNOWN && time() > ($tokenobj->getEndOfLife() - 30)); + } + + // Token expired so we refresh it + if (is_object($tokenobj) && $expire) { + try { + // il faut sauvegarder le refresh token car le provider ne le donne qu'une seule fois + $refreshtoken = $tokenobj->getRefreshToken(); + $tokenobj = $apiService->refreshAccessToken($tokenobj); + $tokenobj->setRefreshToken($refreshtoken); + $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj); + } catch (Exception $e) { + setEventMessages($e->getMessage(), null, 'errors'); + } + } + if ($key[1] != '' && $key[2] != '') { + if (is_object($tokenobj)) { + $refreshtoken = $tokenobj->getRefreshToken(); + + $endoflife = $tokenobj->getEndOfLife(); + if ($endoflife == $tokenobj::EOL_NEVER_EXPIRES) + { + $expiredat = $langs->trans("Never"); + } + elseif ($endoflife == $tokenobj::EOL_UNKNOWN) + { + $expiredat = $langs->trans("Unknown"); + } + else + { + $expiredat=dol_print_date($endoflife, "dayhour"); + } + } + } + + $submit_enabled=0; + print '
'; } diff --git a/htdocs/core/lib/oauth.lib.php b/htdocs/core/lib/oauth.lib.php index 6f47ef6e62b..0149b581fa2 100644 --- a/htdocs/core/lib/oauth.lib.php +++ b/htdocs/core/lib/oauth.lib.php @@ -26,9 +26,13 @@ // Supported OAUTH (a provider is supported when a file xxx_oauthcallback.php is available into htdocs/core/modules/oauth) $supportedoauth2array=array( 'OAUTH_GOOGLE_NAME'=>'google', - 'OAUTH_GITHUB_NAME'=>'github' ); +if ($conf->global->MAIN_FEATURES_LEVEL >= 2) +{ + $supportedoauth2array['OAUTH_GITHUB_NAME']='github'; +} +$supportedoauth2array['OAUTH_GITHUB_NAME']='github'; // API access parameters OAUTH $list = array ( array( @@ -264,7 +268,7 @@ function oauthadmin_prepare_head() $h++; $head[$h][0] = dol_buildpath('/admin/oauthlogintokens.php', 1); - $head[$h][1] = $langs->trans("ManualTokenGeneration"); + $head[$h][1] = $langs->trans("TokenManager"); $head[$h][2] = 'tokengeneration'; $h++; diff --git a/htdocs/core/modules/oauth/github_oauthcallback.php b/htdocs/core/modules/oauth/github_oauthcallback.php new file mode 100644 index 00000000000..83c3da66a47 --- /dev/null +++ b/htdocs/core/modules/oauth/github_oauthcallback.php @@ -0,0 +1,171 @@ + + * + * 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| '.$langs->trans($key['varname']).' | '; + print ''; + if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') + { + print $langs->trans("IsTokenGenerated"); + } + else + { + print $langs->trans($key['varname']); + } + print ' | '; print ''.$langs->trans($key['info']).' | '; print ''; + //var_dump($key); if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') { // Delete remote tokens @@ -187,7 +197,8 @@ if ($mode == 'setup' && $user->admin) // Token print ' | |
| '.$langs->trans("Token").' | '; - print ''; + print ' | '; + $tokenobj=null; // Dolibarr storage $storage = new DoliStorage($db, $conf); try @@ -210,8 +221,6 @@ if ($mode == 'setup' && $user->admin) print '';*/ } print ' | '; - print ''; - print ' | '; print '|