46 $_SESSION[
"dol_loginmesg"] =
"OpenID Connect is disabled";
47 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect Module disabled");
52 $entity = $entitytotest;
53 if (isModEnabled(
'multicompany') &&
getDolGlobalString(
'MULTICOMPANY_TRANSVERSE_MODE')) {
57 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect usertotest=".$usertotest.
" passwordtotest=".preg_replace(
'/./',
'*', $passwordtotest).
" entitytotest=".$entitytotest);
61 if (GETPOSTISSET(
'username')) {
63 $_SESSION[
"dol_loginmesg"] =
"Not an OpenID Connect flow";
64 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::not an OIDC flow");
66 } elseif (!GETPOSTISSET(
'state')) {
68 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (no state received)";
69 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::no state received", LOG_ERR);
71 } elseif (!GETPOSTISSET(
'code')) {
73 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (no code received)";
74 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::no code received", LOG_ERR);
78 $auth_code =
GETPOST(
'code',
'aZ09');
79 $state =
GETPOST(
'state',
'aZ09');
80 dol_syslog(
'functions_openid_connect::check_user_password_openid_connect code='.$auth_code.
' state='.$state);
82 if ($state !== openid_connect_get_state()) {
84 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (state does not match)";
85 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::state does not match", LOG_ERR);
91 'grant_type' =>
'authorization_code',
95 'redirect_uri' => openid_connect_get_redirect_url()
98 $token_response =
getURLContent(
getDolGlobalString(
'MAIN_AUTHENTICATION_OIDC_TOKEN_URL'),
'POST', http_build_query($auth_param), 1, array(), array(
'https'), 2);
99 $token_content = json_decode($token_response[
'content']);
100 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect /token=".print_r($token_response,
true), LOG_DEBUG);
102 if ($token_response[
'curl_error_no']) {
104 $_SESSION[
"dol_loginmesg"] =
"Network error: ".$token_response[
'curl_error_msg'].
" (".$token_response[
'curl_error_no'].
")";
105 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::".$_SESSION[
"dol_loginmesg"], LOG_ERR);
107 } elseif ($token_response[
'http_code'] >= 400 && $token_response[
'http_code'] < 500) {
109 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (".$token_response[
'content'].
")";
110 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::".$token_response[
'content'], LOG_ERR);
112 } elseif ($token_content->error) {
114 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (".$token_content->error_description.
")";
115 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::".$token_content->error_description, LOG_ERR);
117 } elseif (!property_exists($token_content,
'access_token')) {
119 $_SESSION[
"dol_loginmesg"] =
"Token request error (".$token_response[
'http_code'].
")";
120 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::".$_SESSION[
"dol_loginmesg"], LOG_ERR);
125 $userinfo_headers = array(
'Authorization: Bearer '.$token_content->access_token);
127 $userinfo_content = json_decode($userinfo_response[
'content']);
129 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect /userinfo=".print_r($userinfo_response,
true), LOG_DEBUG);
132 $login_claim =
'email';
137 if ($userinfo_response[
'curl_error_no']) {
139 $_SESSION[
"dol_loginmesg"] =
"Network error: ".$userinfo_response[
'curl_error_msg'].
" (".$userinfo_response[
'curl_error_no'].
")";
140 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::".$_SESSION[
"dol_loginmesg"], LOG_ERR);
142 } elseif ($userinfo_response[
'http_code'] >= 400 && $userinfo_response[
'http_code'] < 500) {
144 $_SESSION[
"dol_loginmesg"] =
"OpenID Connect user info error: " . $userinfo_response[
'content'];
145 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::".$userinfo_response[
'content'], LOG_ERR);
147 } elseif ($userinfo_content->error) {
149 $_SESSION[
"dol_loginmesg"] =
"Error in OAuth 2.0 flow (".$userinfo_content->error_description.
")";
150 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::".$userinfo_content->error_description, LOG_ERR);
152 } elseif (!property_exists($userinfo_content, $login_claim)) {
154 $_SESSION[
"dol_loginmesg"] =
"Userinfo request error (".$userinfo_response[
'http_code'].
")";
155 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::".$_SESSION[
"dol_loginmesg"], LOG_ERR);
160 $sql =
'SELECT login, entity, datestartvalidity, dateendvalidity';
161 $sql .=
' FROM '.MAIN_DB_PREFIX.
'user';
162 $sql .=
" WHERE login = '".$db->escape($userinfo_content->$login_claim).
"'";
163 $sql .=
' AND entity IN (0,'.(array_key_exists(
'dol_entity', $_SESSION) ? ((int) $_SESSION[
"dol_entity"]) : 1).
')';
165 dol_syslog(
"functions_openid::check_user_password_openid", LOG_DEBUG);
167 $resql = $db->query($sql);
169 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::Error with sql query (".$db->error().
")");
172 $obj = $db->fetch_object($resql);
174 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect::Error no result from the query");
178 $_SESSION[
'OPENID_CONNECT'] =
true;
181 dol_syslog(
"functions_openid_connect::check_user_password_openid_connect END");
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).