2015-10-21 16:23:35 +00:00
< ? php
2022-08-04 13:58:00 +00:00
/* Copyright ( C ) 2022 Laurent Destailleur < eldy @ users . sourceforge . net >
2024-09-19 19:07:50 +00:00
* Copyright ( C ) 2015 - 2024 Frédéric France < frederic . france @ free . fr >
2024-08-19 00:05:27 +00:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2015-10-21 16:23:35 +00:00
*
* 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
2019-09-23 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2015-10-21 16:23:35 +00:00
*/
2023-05-14 01:07:09 +00:00
// 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:
2022-01-19 19:33:20 +00:00
// https://developers.google.com/identity/protocols/oauth2/openid-connect#server-flow
2015-10-21 16:23:35 +00:00
/**
2017-01-06 18:58:38 +00:00
* \file htdocs / core / modules / oauth / google_oauthcallback . php
2015-10-21 16:23:35 +00:00
* \ingroup oauth
* \brief Page to get oauth callback
*/
2023-05-13 20:09:19 +00:00
// Force keyforprovider
2023-05-14 01:07:09 +00:00
$forlogin = 0 ;
2023-06-05 11:58:48 +00:00
if ( ! empty ( $_GET [ 'state' ]) && preg_match ( '/^forlogin-/' , $_GET [ 'state' ])) {
2023-05-14 01:07:09 +00:00
$forlogin = 1 ;
2023-05-13 20:09:19 +00:00
$_GET [ 'keyforprovider' ] = 'Login' ;
}
2023-05-14 01:07:09 +00:00
if ( ! defined ( 'NOLOGIN' ) && $forlogin ) {
2023-05-13 18:16:57 +00:00
define ( " NOLOGIN " , 1 ); // This means this output page does not require to be logged.
}
2022-09-07 18:08:59 +00:00
// Load Dolibarr environment
2015-10-21 16:23:35 +00:00
require '../../../main.inc.php' ;
require_once DOL_DOCUMENT_ROOT . '/includes/OAuth/bootstrap.php' ;
2024-11-06 18:39:46 +00:00
/**
* @ var Conf $conf
* @ var DoliDB $db
* @ var Translate $langs
* @ var User $user
*
2024-12-01 22:15:27 +00:00
* @ var string $dolibarr_main_url_root
2024-11-06 18:39:46 +00:00
*/
2015-10-21 16:23:35 +00:00
use OAuth\Common\Storage\DoliStorage ;
use OAuth\Common\Consumer\Credentials ;
2015-11-06 00:33:49 +00:00
// Define $urlwithroot
2023-05-14 15:38:08 +00:00
global $dolibarr_main_url_root ;
2020-02-18 22:47:25 +00:00
$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
2015-11-06 00:33:49 +00:00
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
2023-05-14 15:38:08 +00:00
$langs -> load ( " oauth " );
2015-11-06 00:33:49 +00:00
2020-09-16 17:39:50 +00:00
$action = GETPOST ( 'action' , 'aZ09' );
2015-11-06 00:33:49 +00:00
$backtourl = GETPOST ( 'backtourl' , 'alpha' );
2022-06-27 20:35:58 +00:00
$keyforprovider = GETPOST ( 'keyforprovider' , 'aZ09' );
2023-01-03 13:06:25 +00:00
if ( ! GETPOSTISSET ( 'keyforprovider' ) && ! empty ( $_SESSION [ " oauthkeyforproviderbeforeoauthjump " ]) && ( GETPOST ( 'code' ) || $action == 'delete' )) {
2022-06-27 20:35:58 +00:00
// If we are coming from the Oauth page
$keyforprovider = $_SESSION [ " oauthkeyforproviderbeforeoauthjump " ];
}
2015-11-06 00:33:49 +00:00
2015-11-05 17:04:00 +00:00
2015-10-21 16:23:35 +00:00
/**
* Create a new instance of the URI class with the current URI , stripping the query string
*/
$uriFactory = new \OAuth\Common\Http\Uri\UriFactory ();
2015-11-06 00:33:49 +00:00
//$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
//$currentUri->setQuery('');
2016-02-09 13:05:05 +00:00
$currentUri = $uriFactory -> createFromAbsolute ( $urlwithroot . '/core/modules/oauth/google_oauthcallback.php' );
2015-11-06 00:33:49 +00:00
2015-10-21 16:23:35 +00:00
/**
* Load the credential for the service
*/
2023-12-07 19:03:24 +00:00
/** @var \OAuth\ServiceFactory $serviceFactory An OAuth service factory. */
2015-10-21 16:23:35 +00:00
$serviceFactory = new \OAuth\ServiceFactory ();
2015-11-06 00:33:49 +00:00
$httpClient = new \OAuth\Common\Http\Client\CurlClient ();
// TODO Set options for proxy and timeout
// $params=array('CURLXXX'=>value, ...)
//$httpClient->setCurlParameters($params);
$serviceFactory -> setHttpClient ( $httpClient );
2015-10-21 16:23:35 +00:00
// Setup the credentials for the requests
2022-06-27 20:35:58 +00:00
$keyforparamid = 'OAUTH_GOOGLE' . ( $keyforprovider ? '-' . $keyforprovider : '' ) . '_ID' ;
$keyforparamsecret = 'OAUTH_GOOGLE' . ( $keyforprovider ? '-' . $keyforprovider : '' ) . '_SECRET' ;
2015-10-21 16:23:35 +00:00
$credentials = new Credentials (
2022-06-27 20:35:58 +00:00
getDolGlobalString ( $keyforparamid ),
getDolGlobalString ( $keyforparamsecret ),
2020-10-31 13:32:18 +00:00
$currentUri -> getAbsoluteUri ()
2015-10-21 16:23:35 +00:00
);
2022-01-19 19:33:20 +00:00
$state = GETPOST ( 'state' );
2022-06-27 20:35:58 +00:00
$statewithscopeonly = '' ;
$statewithanticsrfonly = '' ;
2022-01-19 19:33:20 +00:00
2020-02-18 22:47:25 +00:00
$requestedpermissionsarray = array ();
2022-01-19 19:33:20 +00:00
if ( $state ) {
// 'state' parameter is standard to store a hash value and can be used to retrieve some parameters back
2023-05-13 18:16:57 +00:00
$statewithscopeonly = preg_replace ( '/\-.*$/' , '' , preg_replace ( '/^forlogin-/' , '' , $state ));
2022-01-19 19:33:20 +00:00
$requestedpermissionsarray = explode ( ',' , $statewithscopeonly ); // Example: 'userinfo_email,userinfo_profile,openid,email,profile,cloud_print'.
2022-06-27 20:35:58 +00:00
$statewithanticsrfonly = preg_replace ( '/^.*\-/' , '' , $state );
2021-02-23 21:03:23 +00:00
}
2022-09-18 00:21:46 +00:00
2023-06-05 13:20:59 +00:00
// Add a test to check that the state parameter is provided into URL when we make the first call to ask the redirect or when we receive the callback
// but not when callback was ok and we recall the page
2025-03-10 00:13:08 +00:00
if ( $action != 'delete' && ! GETPOST ( 'afteroauthloginreturn' ) && ( empty ( $statewithscopeonly ) || empty ( $requestedpermissionsarray ))) {
2023-06-05 12:52:02 +00:00
dol_syslog ( " state or statewithscopeonly and/or requestedpermissionsarray are empty " );
2022-09-18 00:21:46 +00:00
setEventMessages ( $langs -> trans ( 'ScopeUndefined' ), null , 'errors' );
2023-06-05 12:52:02 +00:00
if ( empty ( $backtourl )) {
$backtourl = DOL_URL_ROOT . '/' ;
}
2022-09-18 00:21:46 +00:00
header ( 'Location: ' . $backtourl );
exit ();
2015-11-13 01:11:47 +00:00
}
2022-09-18 00:21:46 +00:00
2015-11-13 01:11:47 +00:00
//var_dump($requestedpermissionsarray);exit;
2015-11-06 00:33:49 +00:00
2022-01-19 19:33:20 +00:00
2022-06-27 20:35:58 +00:00
// Dolibarr storage
$storage = new DoliStorage ( $db , $conf , $keyforprovider );
2022-01-19 19:33:20 +00:00
2015-10-21 16:23:35 +00:00
// Instantiate the Api service using the credentials, http client and storage mechanism for the token
2019-12-16 20:06:32 +00:00
// $requestedpermissionsarray contains list of scopes.
// Conversion into URL is done by Reflection on constant with name SCOPE_scope_in_uppercase
2026-02-25 18:38:55 +00:00
$apiService = null ;
$nameofservice = 'Google' ;
try {
//$nameofservice = ucfirst(strtolower($genericstring));
$apiService = $serviceFactory -> createService ( $nameofservice , $credentials , $storage , $requestedpermissionsarray );
'@phan-var-force OAuth\OAuth2\Service\Google $apiService' ; // createService is only ServiceInterface
} catch ( Exception $e ) {
print 'Error, failed to create service for provider ' . $nameofservice . ( $keyforprovider ? '-' . $keyforprovider : '' ) . '. Message was: ' . $e -> getMessage ();
exit ;
}
2017-01-06 18:58:38 +00:00
// access type needed to have oauth provider refreshing token
2019-12-16 20:06:32 +00:00
// also note that a refresh token is sent only after a prompt
2015-10-21 16:23:35 +00:00
$apiService -> setAccessType ( 'offline' );
2022-01-19 19:33:20 +00:00
2022-06-27 20:35:58 +00:00
if ( ! getDolGlobalString ( $keyforparamid )) {
2023-05-13 18:16:57 +00:00
accessforbidden ( 'Setup of service ' . $keyforparamid . ' is not complete. Customer ID is missing' );
2022-06-27 20:35:58 +00:00
}
if ( ! getDolGlobalString ( $keyforparamsecret )) {
2023-05-13 18:16:57 +00:00
accessforbidden ( 'Setup of service ' . $keyforparamid . ' is not complete. Secret key is missing' );
2022-06-27 20:35:58 +00:00
}
2015-11-06 00:33:49 +00:00
/*
* Actions
*/
2024-08-31 17:26:46 +00:00
if ( $action == 'delete' && ( ! empty ( $user -> admin ) || $user -> id == GETPOSTINT ( 'userid' ))) {
$storage -> userid = GETPOSTINT ( 'userid' );
2020-10-31 13:32:18 +00:00
$storage -> clearToken ( 'Google' );
2017-11-27 12:45:59 +00:00
2020-10-31 13:32:18 +00:00
setEventMessages ( $langs -> trans ( 'TokenDeleted' ), null , 'mesgs' );
2017-11-27 12:45:59 +00:00
2020-10-31 13:32:18 +00:00
header ( 'Location: ' . $backtourl );
exit ();
2017-11-27 12:45:59 +00:00
}
2015-11-05 17:04:00 +00:00
2024-08-01 14:31:53 +00:00
2023-05-14 15:38:08 +00:00
if ( ! GETPOST ( 'code' )) {
2024-04-11 12:32:02 +00:00
dol_syslog ( " Page is called without the 'code' parameter defined " );
2023-06-05 12:52:02 +00:00
2024-08-01 14:31:53 +00:00
// If we enter this page without 'code' parameter, it means we click on the link from login page ($forlogin is set) or from setup page and we want to get the redirect
2023-05-14 15:38:08 +00:00
// to the OAuth provider login page.
2026-05-29 23:44:23 +00:00
// $backtourl should be a relative url like /mypage.php?param1=value1 but without param token and action. Part after the # should also have been removed by caller.
// Clean the backtourl we can use after an OAuth authentication
$backtourl = preg_replace ( '/token=[^&]+/' , '' , $backtourl ); // We remove any token into url so we are sure only url with no action are qualified as call back urls.
$backtourl = preg_replace ( '/action=[a-z0-9]+/i' , '' , $backtourl ); // We remove any token into url so we are sure only url with no action are qualified as call back urls.
$backtourl = preg_replace ( '/save_lastsearch_values=[a-z0-9]+/i' , '' , $backtourl );
$backtourl = preg_replace ( '/mainmenu=[a-z0-9]+/i' , '' , $backtourl );
$backtourl = preg_replace ( '/leftmenu=[a-z0-9]+/i' , '' , $backtourl );
$backtourl = preg_replace ( '/#.*$/i' , '' , $backtourl ); // We remove part after the #...
2023-05-14 15:38:08 +00:00
$_SESSION [ " backtourlsavedbeforeoauthjump " ] = $backtourl ;
$_SESSION [ " oauthkeyforproviderbeforeoauthjump " ] = $keyforprovider ;
$_SESSION [ 'oauthstateanticsrf' ] = $state ;
2023-06-05 11:58:48 +00:00
// Save more data into session
2024-03-28 20:29:02 +00:00
// No need to save more data in sessions. We have several info into $_SESSION['datafromloginform'], saved when form is posted with a click
// on "Login with Google" with param actionlogin=login and beforeoauthloginredirect=google, by the functions_googleoauth.php.
2023-06-05 11:58:48 +00:00
2024-08-01 14:31:53 +00:00
// Set approval_prompt. Note: A refresh token will be provided only if prompt is done.
2023-05-14 15:38:08 +00:00
if ( $forlogin ) {
2024-05-01 14:53:13 +00:00
$approval_prompt = getDolGlobalString ( 'OAUTH_GOOGLE_FORCE_PROMPT_ON_LOGIN' , 'auto' ); // Can be 'force'
$apiService -> setApprouvalPrompt ( $approval_prompt );
2024-08-01 14:31:53 +00:00
} else {
$apiService -> setApprouvalPrompt ( 'force' );
2023-05-14 15:38:08 +00:00
}
// This may create record into oauth_state before the header redirect.
2024-08-01 14:31:53 +00:00
// Creation of record with state, create record or just update column state of table llx_oauth_token (and create/update entry in llx_oauth_state) depending on the Provider used (see its constructor).
2023-05-14 15:38:08 +00:00
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
2024-08-19 00:05:27 +00:00
$url .= '&nonce=' . bin2hex ( random_bytes ( 64 / 8 ));
2023-05-14 15:38:08 +00:00
if ( $forlogin ) {
// TODO Add param hd. What is it for ?
//$url .= 'hd=xxx';
if ( GETPOST ( 'username' )) {
$url .= '&login_hint=' . urlencode ( GETPOST ( 'username' ));
}
2024-01-13 18:48:20 +00:00
// Check that the redirect_uri that will be used is same than url of current domain
2023-05-14 15:38:08 +00:00
// 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 ;
}
}
2024-08-01 14:31:53 +00:00
//var_dump($url);exit;
// we go on oauth provider authorization page, we will then go back on this page but into the other branch of the if (!GETPOST('code'))
2023-05-14 15:38:08 +00:00
header ( 'Location: ' . $url );
exit ();
} else {
// We are coming from the return of an OAuth2 provider page.
2024-10-01 19:53:45 +00:00
dol_syslog ( basename ( __FILE__ ) . " We are coming from the oauth provider page keyforprovider= " . $keyforprovider . " code= " . dol_trunc ( GETPOST ( 'code' ), 5 ));
2015-11-05 17:04:00 +00:00
2022-01-19 22:01:50 +00:00
// 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' ]) {
2023-05-13 20:09:19 +00:00
//var_dump($_SESSION['oauthstateanticsrf']);exit;
2023-05-14 15:38:08 +00:00
print 'Value for state=' . dol_escape_htmltag ( $state ) . ' differs from value in $_SESSION["oauthstateanticsrf"]. Code is refused.' ;
2022-01-19 22:01:50 +00:00
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
2023-05-13 20:09:19 +00:00
//dol_syslog("_GET=".var_export($_GET, true));
$errorincheck = 0 ;
$db -> begin ();
2022-01-19 22:01:50 +00:00
2026-01-13 14:55:56 +00:00
$token = null ;
2026-01-13 14:24:26 +00:00
try {
// 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 and into database table llx_oauth_token
$token = $apiService -> requestAccessToken ( GETPOST ( 'code' ), $state );
} catch ( Exception $e ) {
dol_syslog ( " Failed to get token with requestAccessToken: " . $e -> getMessage (), LOG_ERR );
setEventMessages ( " Failed to get token with requestAccessToken: " . $e -> getMessage (), null , 'errors' );
$errorincheck ++ ;
}
2022-01-19 22:01:50 +00:00
2024-08-01 14:31:53 +00:00
// The refresh token is inside the object token if the prompt was forced only.
//$refreshtoken = $token->getRefreshToken();
//var_dump($refreshtoken);
2026-01-13 14:24:26 +00:00
dol_syslog ( " requestAccessToken complete " );
2024-08-01 14:31:53 +00:00
2022-01-19 22:01:50 +00:00
// Note: The extraparams has the 'id_token' than contains a lot of information about the user.
2026-01-13 14:24:26 +00:00
$extraparams = array ();
if ( $token ) {
$extraparams = $token -> getExtraParams ();
}
2022-01-19 22:01:50 +00:00
$jwt = explode ( '.' , $extraparams [ 'id_token' ]);
2023-06-05 11:58:48 +00:00
$username = '' ;
2023-05-14 01:07:09 +00:00
$useremail = '' ;
2022-01-19 22:01:50 +00:00
// Extract the middle part, base64 decode, then json_decode it
if ( ! empty ( $jwt [ 1 ])) {
$userinfo = json_decode ( base64_decode ( $jwt [ 1 ]), true );
2026-02-10 14:01:34 +00:00
dol_syslog ( " userinfo= " . formatLogObject ( $userinfo ));
2023-05-14 01:07:09 +00:00
$useremail = $userinfo [ 'email' ];
2023-07-10 02:03:57 +00:00
2023-05-13 20:09:19 +00:00
/*
$useremailverified = $userinfo [ 'email_verified' ];
2023-05-14 01:07:09 +00:00
$useremailuniq = $userinfo [ 'sub' ];
2023-05-13 20:09:19 +00:00
$username = $userinfo [ 'name' ];
$userfamilyname = $userinfo [ 'family_name' ];
$usergivenname = $userinfo [ 'given_name' ];
$hd = $userinfo [ 'hd' ];
*/
2023-05-14 01:07:09 +00:00
// We should make the steps of validation of id_token
// Verify that the state is the one expected
// TODO
2023-05-13 20:09:19 +00:00
2022-01-19 22:01:50 +00:00
// 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.
2023-05-13 20:09:19 +00:00
// TODO
2023-07-10 02:03:57 +00:00
// Verify that email is a verified email
/* if ( empty ( $userinfo [ 'email_verified' ])) {
2024-09-19 19:07:50 +00:00
setEventMessages ( $langs -> trans ( 'Bad value for email, email was not verified by Google' ), null , 'errors' );
2023-07-10 02:03:57 +00:00
$errorincheck ++ ;
} */
2022-01-19 22:01:50 +00:00
// Verify that the value of the iss claim in the ID token is equal to https://accounts.google.com or accounts.google.com.
2023-05-13 20:09:19 +00:00
if ( $userinfo [ 'iss' ] != 'accounts.google.com' && $userinfo [ 'iss' ] != 'https://accounts.google.com' ) {
setEventMessages ( $langs -> trans ( 'Bad value for returned userinfo[iss]' ), null , 'errors' );
$errorincheck ++ ;
}
2022-01-19 22:01:50 +00:00
// Verify that the value of the aud claim in the ID token is equal to your app's client ID.
2023-05-13 20:09:19 +00:00
if ( $userinfo [ 'aud' ] != getDolGlobalString ( $keyforparamid )) {
setEventMessages ( $langs -> trans ( 'Bad value for returned userinfo[aud]' ), null , 'errors' );
$errorincheck ++ ;
}
2022-01-19 22:01:50 +00:00
// Verify that the expiry time (exp claim) of the ID token has not passed.
2023-05-13 20:09:19 +00:00
if ( $userinfo [ 'exp' ] <= dol_now ()) {
setEventMessages ( $langs -> trans ( 'Bad value for returned userinfo[exp]. Token expired.' ), null , 'errors' );
$errorincheck ++ ;
}
2022-01-19 22:01:50 +00:00
// 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.
2023-06-05 13:20:59 +00:00
// $userinfo['hd'] is the domain name of Gmail account.
2023-05-13 20:09:19 +00:00
// TODO
}
2022-01-19 22:01:50 +00:00
2023-05-13 20:09:19 +00:00
if ( ! $errorincheck ) {
2023-05-14 01:07:09 +00:00
// If call back to url for a OAUTH2 login
if ( $forlogin ) {
dol_syslog ( " we received the login/email to log to, it is " . $useremail );
2023-05-14 15:38:08 +00:00
$tmparray = ( empty ( $_SESSION [ 'datafromloginform' ]) ? array () : $_SESSION [ 'datafromloginform' ]);
2024-12-04 09:31:48 +00:00
$entitytosearchuser = (( isset ( $tmparray [ 'entity' ]) && $tmparray [ 'entity' ] != '' ) ? $tmparray [ 'entity' ] : - 1 );
2023-05-14 15:38:08 +00:00
2024-08-01 14:31:53 +00:00
// Delete the old token
$storage -> clearToken ( 'Google' ); // Delete the token called ("Google-".$storage->keyforprovider)
2023-05-14 01:07:09 +00:00
$tmpuser = new User ( $db );
2024-12-04 09:22:28 +00:00
$res = $tmpuser -> fetch ( 0 , '' , '' , 0 , $entitytosearchuser , $useremail , 0 , 1 ); // Load user. Can load with email_oauth2.
2023-05-13 20:09:19 +00:00
2023-05-14 01:07:09 +00:00
if ( $res > 0 ) {
$username = $tmpuser -> login ;
$_SESSION [ 'googleoauth_receivedlogin' ] = dol_hash ( $conf -> file -> instance_unique_id . $username , '0' );
2023-06-05 12:16:32 +00:00
dol_syslog ( 'We set $_SESSION[\'googleoauth_receivedlogin\']=' . $_SESSION [ 'googleoauth_receivedlogin' ]);
2023-05-14 01:07:09 +00:00
} else {
2023-05-14 15:38:08 +00:00
$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 ;
2023-05-14 01:07:09 +00:00
$errorincheck ++ ;
2023-06-05 12:16:32 +00:00
dol_syslog ( $errormessage );
2023-05-14 01:07:09 +00:00
}
2026-01-13 14:24:26 +00:00
} else {
setEventMessages ( " TokenSaved " , null );
2023-05-14 01:07:09 +00:00
}
} else {
// If call back to url for a OAUTH2 login
if ( $forlogin ) {
2024-01-13 18:48:20 +00:00
$_SESSION [ " dol_loginmesg " ] = " Failed to login using Google. OAuth callback URL retrieves a token with non valid data " ;
2023-05-14 01:07:09 +00:00
$errorincheck ++ ;
}
}
2023-05-13 20:09:19 +00:00
2023-05-14 01:07:09 +00:00
if ( ! $errorincheck ) {
2023-05-13 20:09:19 +00:00
$db -> commit ();
} else {
$db -> rollback ();
2022-01-19 22:01:50 +00:00
}
$backtourl = $_SESSION [ " backtourlsavedbeforeoauthjump " ];
unset ( $_SESSION [ " backtourlsavedbeforeoauthjump " ]);
2023-05-13 20:09:19 +00:00
if ( empty ( $backtourl )) {
2023-06-05 13:20:59 +00:00
$backtourl = DOL_URL_ROOT . '/' ;
2023-05-13 20:09:19 +00:00
}
2023-05-14 15:38:08 +00:00
2023-06-05 13:20:59 +00:00
// If call back to this url was for a OAUTH2 login
2023-05-14 01:07:09 +00:00
if ( $forlogin ) {
2023-06-05 13:20:59 +00:00
// _SESSION['googleoauth_receivedlogin'] has been set to the key to validate the next test by function_googleoauth(), so we can make the redirect
2026-05-29 23:44:23 +00:00
// $backtourl is a relative url like /mypage.php?param1=value1 but without param token and action. Part after the # should also have been removed when saving it.
$backtourl = DOL_MAIN_URL_ROOT . $backtourl ;
2026-05-29 23:24:23 +00:00
$backtourl .= ( preg_match ( '/\?/' , $backtourl ) ? '&' : '?' ) . 'actionlogin=login&afteroauthloginreturn=google&mainmenu=home' . ( $username ? '&username=' . urlencode ( $username ) : '' ) . '&token=' . newToken ();
2023-05-14 15:38:08 +00:00
if ( ! empty ( $tmparray [ 'entity' ])) {
$backtourl .= '&entity=' . $tmparray [ 'entity' ];
}
2023-05-14 01:07:09 +00:00
}
2023-05-13 20:09:19 +00:00
dol_syslog ( " Redirect now on backtourl= " . $backtourl );
2022-01-19 22:01:50 +00:00
header ( 'Location: ' . $backtourl );
exit ();
} catch ( Exception $e ) {
print $e -> getMessage ();
2022-01-19 21:26:31 +00:00
}
2020-10-31 13:32:18 +00:00
}
2015-10-21 16:23:35 +00:00
}
2015-11-06 00:33:49 +00:00
/*
* View
*/
2023-05-14 15:38:08 +00:00
// No view at all, just actions, so we reach this line only on error.
2015-10-21 16:23:35 +00:00
$db -> close ();