41 global $db, $conf, $langs;
44 $entity = $entitytotest;
45 if (isModEnabled(
'multicompany') &&
getDolGlobalString(
'MULTICOMPANY_TRANSVERSE_MODE')) {
51 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect usertotest=".$usertotest.
" passwordtotest=".preg_replace(
'/./',
'*', $passwordtotest).
" entitytotest=".$entitytotest);
55 if (GETPOSTISSET(
'username')) {
57 $_SESSION[
"dol_loginmesg"] =
"Not an OpenID Connect flow";
58 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect not an OIDC flow");
59 } elseif (!GETPOSTISSET(
'state')) {
61 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (no state received)";
62 } elseif (GETPOSTISSET(
'code')) {
63 $auth_code =
GETPOST(
'code',
'aZ09');
64 $state =
GETPOST(
'state',
'aZ09');
65 dol_syslog(
'functions_openid_connect::check_user_password_openid_connect code='.$auth_code.
' state='.$state);
67 if ($state === hash(
'sha256', session_id())) {
70 'grant_type' =>
'authorization_code',
77 $token_response =
getURLContent($conf->global->MAIN_AUTHENTICATION_OIDC_TOKEN_URL,
'POST', http_build_query($auth_param));
78 $token_content = json_decode($token_response[
'content']);
79 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect /token=".print_r($token_response,
true), LOG_DEBUG);
81 if (property_exists($token_content,
'access_token')) {
83 $userinfo_headers = array(
'Authorization: Bearer '.$token_content->access_token);
84 $userinfo_response =
getURLContent($conf->global->MAIN_AUTHENTICATION_OIDC_USERINFO_URL,
'GET',
'', 1, $userinfo_headers);
85 $userinfo_content = json_decode($userinfo_response[
'content']);
87 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect /userinfo=".print_r($userinfo_response,
true), LOG_DEBUG);
90 $login_claim =
'email';
95 if (property_exists($userinfo_content, $login_claim)) {
97 $sql =
'SELECT login, entity, datestartvalidity, dateendvalidity';
98 $sql .=
' FROM '.MAIN_DB_PREFIX.
'user';
99 $sql .=
" WHERE login = '".$db->escape($userinfo_content->$login_claim).
"'";
100 $sql .=
' AND entity IN (0,'.(array_key_exists(
'dol_entity', $_SESSION) ? ((int) $_SESSION[
"dol_entity"]) : 1).
')';
102 dol_syslog(
"functions_openid::check_user_password_openid", LOG_DEBUG);
104 $resql = $db->query($sql);
106 $obj = $db->fetch_object($resql);
109 $login = $obj->login;
112 } elseif ($userinfo_content->error) {
114 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (".$userinfo_content->error_description.
")";
115 } elseif ($userinfo_response[
'http_code'] == 200) {
117 $_SESSION[
"dol_loginmesg"] =
"OpenID Connect claim not found: ".$login_claim;
118 } elseif ($userinfo_response[
'curl_error_no']) {
120 $_SESSION[
"dol_loginmesg"] =
"Network error: ".$userinfo_response[
'curl_error_msg'].
" (".$userinfo_response[
'curl_error_no'].
")";
123 $_SESSION[
"dol_loginmesg"] =
"Userinfo request error (".$userinfo_response[
'http_code'].
")";
125 } elseif ($token_content->error) {
127 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (".$token_content->error_description.
")";
128 } elseif ($token_response[
'curl_error_no']) {
130 $_SESSION[
"dol_loginmesg"] =
"Network error: ".$token_response[
'curl_error_msg'].
" (".$token_response[
'curl_error_no'].
")";
133 $_SESSION[
"dol_loginmesg"] =
"Token request error (".$token_response[
'http_code'].
")";
137 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (state does not match)";
141 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (no code received)";
144 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect END");
146 return !empty($login) ? $login :
false;
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
check_user_password_openid_connect($usertotest, $passwordtotest, $entitytotest)
Check validity of user/password/entity If test is ko, reason must be filled into $_SESSION["dol_login...
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).