123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- <?php
- /* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
- *
- * 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 <https://www.gnu.org/licenses/>.
- */
- // This page is used as callback for token generation of an OAUTH request.
- // This page can also be used to make the process to login and get token as described here:
- // https://developers.google.com/identity/protocols/oauth2/openid-connect#server-flow
- /**
- * \file htdocs/core/modules/oauth/google_oauthcallback.php
- * \ingroup oauth
- * \brief Page to get oauth callback
- */
- // Force keyforprovider
- $forlogin = 0;
- if (!empty($_GET['state']) && preg_match('/^forlogin-/', $_GET['state'])) {
- $forlogin = 1;
- $_GET['keyforprovider'] = 'Login';
- }
- if (!defined('NOLOGIN') && $forlogin) {
- define("NOLOGIN", 1); // This means this output page does not require to be logged.
- }
- // Load Dolibarr environment
- require '../../../main.inc.php';
- require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
- use OAuth\Common\Storage\DoliStorage;
- use OAuth\Common\Consumer\Credentials;
- use OAuth\OAuth2\Service\Google;
- // Define $urlwithroot
- global $dolibarr_main_url_root;
- $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
- $langs->load("oauth");
- $action = GETPOST('action', 'aZ09');
- $backtourl = GETPOST('backtourl', 'alpha');
- $keyforprovider = GETPOST('keyforprovider', 'aZ09');
- if (!GETPOSTISSET('keyforprovider') && !empty($_SESSION["oauthkeyforproviderbeforeoauthjump"]) && (GETPOST('code') || $action == 'delete')) {
- // If we are coming from the Oauth page
- $keyforprovider = $_SESSION["oauthkeyforproviderbeforeoauthjump"];
- }
- /**
- * Create a new instance of the URI class with the current URI, stripping the query string
- */
- $uriFactory = new \OAuth\Common\Http\Uri\UriFactory();
- //$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
- //$currentUri->setQuery('');
- $currentUri = $uriFactory->createFromAbsolute($urlwithroot.'/core/modules/oauth/google_oauthcallback.php');
- /**
- * Load the credential for the service
- */
- /** @var $serviceFactory \OAuth\ServiceFactory An OAuth service factory. */
- $serviceFactory = new \OAuth\ServiceFactory();
- $httpClient = new \OAuth\Common\Http\Client\CurlClient();
- // TODO Set options for proxy and timeout
- // $params=array('CURLXXX'=>value, ...)
- //$httpClient->setCurlParameters($params);
- $serviceFactory->setHttpClient($httpClient);
- // Setup the credentials for the requests
- $keyforparamid = 'OAUTH_GOOGLE'.($keyforprovider ? '-'.$keyforprovider : '').'_ID';
- $keyforparamsecret = 'OAUTH_GOOGLE'.($keyforprovider ? '-'.$keyforprovider : '').'_SECRET';
- $credentials = new Credentials(
- getDolGlobalString($keyforparamid),
- getDolGlobalString($keyforparamsecret),
- $currentUri->getAbsoluteUri()
- );
- $state = GETPOST('state');
- $statewithscopeonly = '';
- $statewithanticsrfonly = '';
- $requestedpermissionsarray = array();
- if ($state) {
- // 'state' parameter is standard to store a hash value and can be used to retrieve some parameters back
- $statewithscopeonly = preg_replace('/\-.*$/', '', preg_replace('/^forlogin-/', '', $state));
- $requestedpermissionsarray = explode(',', $statewithscopeonly); // Example: 'userinfo_email,userinfo_profile,openid,email,profile,cloud_print'.
- $statewithanticsrfonly = preg_replace('/^.*\-/', '', $state);
- }
- if ($action != 'delete' && (empty($statewithscopeonly) || empty($requestedpermissionsarray))) {
- setEventMessages($langs->trans('ScopeUndefined'), null, 'errors');
- header('Location: '.$backtourl);
- exit();
- }
- //var_dump($requestedpermissionsarray);exit;
- // Dolibarr storage
- $storage = new DoliStorage($db, $conf, $keyforprovider);
- // Instantiate the Api service using the credentials, http client and storage mechanism for the token
- // $requestedpermissionsarray contains list of scopes.
- // Conversion into URL is done by Reflection on constant with name SCOPE_scope_in_uppercase
- $apiService = $serviceFactory->createService('Google', $credentials, $storage, $requestedpermissionsarray);
- // access type needed to have oauth provider refreshing token
- // also note that a refresh token is sent only after a prompt
- $apiService->setAccessType('offline');
- if (!getDolGlobalString($keyforparamid)) {
- accessforbidden('Setup of service '.$keyforparamid.' is not complete. Customer ID is missing');
- }
- if (!getDolGlobalString($keyforparamsecret)) {
- accessforbidden('Setup of service '.$keyforparamid.' is not complete. Secret key is missing');
- }
- /*
- * Actions
- */
- if ($action == 'delete') {
- $storage->clearToken('Google');
- setEventMessages($langs->trans('TokenDeleted'), null, 'mesgs');
- header('Location: '.$backtourl);
- exit();
- }
- if (!GETPOST('code')) {
- // If we enter this page without 'code' parameter, it means we click on the link from login page and we want to get the redirect
- // to the OAuth provider login page.
- $_SESSION["backtourlsavedbeforeoauthjump"] = $backtourl;
- $_SESSION["oauthkeyforproviderbeforeoauthjump"] = $keyforprovider;
- $_SESSION['oauthstateanticsrf'] = $state;
- // Save more data into session
- // Not required. All data are savec into $_SESSION['datafromloginform'] when form is posted with a click on Login with
- // Google with param actionlogin=login and beforeoauthloginredirect=1, by the functions_googleoauth.php.
- /*
- if (!empty($_POST["tz"])) {
- $_SESSION["tz"] = $_POST["tz"];
- }
- if (!empty($_POST["tz_string"])) {
- $_SESSION["tz_string"] = $_POST["tz_string"];
- }
- if (!empty($_POST["dst_first"])) {
- $_SESSION["dst_first"] = $_POST["dst_first"];
- }
- if (!empty($_POST["dst_second"])) {
- $_SESSION["dst_second"] = $_POST["dst_second"];
- }
- */
- if ($forlogin) {
- $apiService->setApprouvalPrompt('force');
- }
- // This may create record into oauth_state before the header redirect.
- // Creation of record with state in this tables depend on the Provider used (see its constructor).
- if ($state) {
- $url = $apiService->getAuthorizationUri(array('state' => $state));
- } else {
- $url = $apiService->getAuthorizationUri(); // Parameter state will be randomly generated
- }
- // The redirect_uri is included into this $url
- // Add more param
- $url .= '&nonce='.bin2hex(random_bytes(64/8));
- if ($forlogin) {
- // TODO Add param hd. What is it for ?
- //$url .= 'hd=xxx';
- if (GETPOST('username')) {
- $url .= '&login_hint='.urlencode(GETPOST('username'));
- }
- // Check that the redirect_uri that wil be used is same than url of current domain
- // Define $urlwithroot
- global $dolibarr_main_url_root;
- $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
- include DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
- $currentrooturl = getRootURLFromURL(DOL_MAIN_URL_ROOT);
- $externalrooturl = getRootURLFromURL($urlwithroot);
- if ($currentrooturl != $externalrooturl) {
- $langs->load("errors");
- setEventMessages($langs->trans("ErrorTheUrlOfYourDolInstanceDoesNotMatchURLIntoOAuthSetup", $currentrooturl, $externalrooturl), null, 'errors');
- $url = DOL_URL_ROOT;
- }
- }
- // we go on oauth provider authorization page
- header('Location: '.$url);
- exit();
- } else {
- // We are coming from the return of an OAuth2 provider page.
- dol_syslog("We are coming from the oauth provider page keyforprovider=".$keyforprovider." code=".dol_trunc(GETPOST('code'), 5));
- // We must validate that the $state is the same than the one into $_SESSION['oauthstateanticsrf'], return error if not.
- if (isset($_SESSION['oauthstateanticsrf']) && $state != $_SESSION['oauthstateanticsrf']) {
- //var_dump($_SESSION['oauthstateanticsrf']);exit;
- print 'Value for state='.dol_escape_htmltag($state).' differs from value in $_SESSION["oauthstateanticsrf"]. Code is refused.';
- unset($_SESSION['oauthstateanticsrf']);
- } else {
- // This was a callback request from service, get the token
- try {
- //var_dump($state);
- //var_dump($apiService); // OAuth\OAuth2\Service\Google
- //dol_syslog("_GET=".var_export($_GET, true));
- $errorincheck = 0;
- $db->begin();
- // This requests the token from the received OAuth code (call of the https://oauth2.googleapis.com/token endpoint)
- // Result is stored into object managed by class DoliStorage into includes/OAuth/Common/Storage/DoliStorage.php, so into table llx_oauth_token
- $token = $apiService->requestAccessToken(GETPOST('code'), $state);
- // Note: The extraparams has the 'id_token' than contains a lot of information about the user.
- $extraparams = $token->getExtraParams();
- $jwt = explode('.', $extraparams['id_token']);
- $username = '';
- $useremail = '';
- // Extract the middle part, base64 decode, then json_decode it
- if (!empty($jwt[1])) {
- $userinfo = json_decode(base64_decode($jwt[1]), true);
- dol_syslog("userinfo=".var_export($userinfo, true));
- $useremail = $userinfo['email'];
- /*
- $useremailverified = $userinfo['email_verified'];
- $useremailuniq = $userinfo['sub'];
- $username = $userinfo['name'];
- $userfamilyname = $userinfo['family_name'];
- $usergivenname = $userinfo['given_name'];
- $hd = $userinfo['hd'];
- */
- // We should make the steps of validation of id_token
- // Verify that the state is the one expected
- // TODO
- // Verify that the ID token is properly signed by the issuer. Google-issued tokens are signed using one of the certificates found at the URI specified in the jwks_uri metadata value of the Discovery document.
- // TODO
- // Verify that the value of the iss claim in the ID token is equal to https://accounts.google.com or accounts.google.com.
- if ($userinfo['iss'] != 'accounts.google.com' && $userinfo['iss'] != 'https://accounts.google.com') {
- setEventMessages($langs->trans('Bad value for returned userinfo[iss]'), null, 'errors');
- $errorincheck++;
- }
- // Verify that the value of the aud claim in the ID token is equal to your app's client ID.
- if ($userinfo['aud'] != getDolGlobalString($keyforparamid)) {
- setEventMessages($langs->trans('Bad value for returned userinfo[aud]'), null, 'errors');
- $errorincheck++;
- }
- // Verify that the expiry time (exp claim) of the ID token has not passed.
- if ($userinfo['exp'] <= dol_now()) {
- setEventMessages($langs->trans('Bad value for returned userinfo[exp]. Token expired.'), null, 'errors');
- $errorincheck++;
- }
- // If you specified a hd parameter value in the request, verify that the ID token has a hd claim that matches an accepted G Suite hosted domain.
- // TODO
- }
- if (!$errorincheck) {
- // If call back to url for a OAUTH2 login
- if ($forlogin) {
- dol_syslog("we received the login/email to log to, it is ".$useremail);
- $tmparray = (empty($_SESSION['datafromloginform']) ? array() : $_SESSION['datafromloginform']);
- $entitytosearchuser = (isset($tmparray['entity']) ? $tmparray['entity'] : -1);
- // Delete the token
- $storage->clearToken('Google');
- $tmpuser = new User($db);
- $res = $tmpuser->fetch(0, '', '', 0, $entitytosearchuser, $useremail);
- if ($res > 0) {
- $username = $tmpuser->login;
- $_SESSION['googleoauth_receivedlogin'] = dol_hash($conf->file->instance_unique_id.$username, '0');
- dol_syslog('$_SESSION[\'googleoauth_receivedlogin\']='.$_SESSION['googleoauth_receivedlogin']);
- } else {
- $errormessage = "Failed to login using Google. User with the Email '".$useremail."' was not found";
- if ($entitytosearchuser > 0) {
- $errormessage .= ' ('.$langs->trans("Entity").' '.$entitytosearchuser.')';
- }
- $_SESSION["dol_loginmesg"] = $errormessage;
- $errorincheck++;
- }
- }
- } else {
- // If call back to url for a OAUTH2 login
- if ($forlogin) {
- $_SESSION["dol_loginmesg"] = "Failed to login using Google. OAuth callback URL retreives a token with non valid data";
- $errorincheck++;
- }
- }
- if (!$errorincheck) {
- $db->commit();
- } else {
- $db->rollback();
- }
- $backtourl = $_SESSION["backtourlsavedbeforeoauthjump"];
- unset($_SESSION["backtourlsavedbeforeoauthjump"]);
- if (empty($backtourl)) {
- $backtourl = DOL_URL_ROOT;
- }
- // If call back to url for a OAUTH2 login
- if ($forlogin) {
- $backtourl .= '?actionlogin=login&afteroauthloginreturn=1'.($username ? '&username='.urlencode($username) : '').'&token='.newToken();
- if (!empty($tmparray['entity'])) {
- $backtourl .= '&entity='.$tmparray['entity'];
- }
- }
- dol_syslog("Redirect now on backtourl=".$backtourl);
- header('Location: '.$backtourl);
- exit();
- } catch (Exception $e) {
- print $e->getMessage();
- }
- }
- }
- /*
- * View
- */
- // No view at all, just actions, so we reach this line only on error.
- $db->close();
|