diff --git a/rest/index.php b/rest/index.php index 148d1032198208ef7c0f3f11ca13d7b4dc017a4f..653b80ddecf77fcdf1a097eb21ae03d5dd7df544 100755 --- a/rest/index.php +++ b/rest/index.php @@ -45,7 +45,10 @@ $app->add(function (\Slim\Http\Request $request, \Slim\Http\Response $response, } } } else { - return $response->withStatus(401)->withJson(['errors' => 'Authentication Failed']); + $response = \SrcCore\controllers\AuthenticationController::authenticate($request, $response); + if ($response->getStatusCode() != 204) { + return $response; + } } } } diff --git a/src/core/controllers/AuthenticationController.php b/src/core/controllers/AuthenticationController.php index a5424378dfaf9c3510d9513e02adc4bdb19335fc..88b133dc9710a7294f821030cf7a5a5de2d601ee 100755 --- a/src/core/controllers/AuthenticationController.php +++ b/src/core/controllers/AuthenticationController.php @@ -256,7 +256,7 @@ class AuthenticationController return true; } - public function authenticate(Request $request, Response $response) + public static function authenticate(Request $request, Response $response) { $body = $request->getParsedBody(); @@ -340,8 +340,7 @@ class AuthenticationController UserController::setAbsences(); $user = UserModel::getByLowerLogin(['login' => $login, 'select' => ['id', 'refresh_token', 'user_id']]); - $GLOBALS['id'] = $user['id']; - $GLOBALS['login'] = $user['user_id']; + \SrcCore\controllers\CoreController::setGlobals(['userId' => $user['id']]); $user['refresh_token'] = json_decode($user['refresh_token'], true); foreach ($user['refresh_token'] as $key => $refreshToken) { @@ -567,11 +566,8 @@ class AuthenticationController return ['errors' => 'Sso configuration missing : no login mapping']; } - if (in_array(strtoupper($mapping['login']), ['REMOTE_USER', 'PHP_AUTH_USER'])) { - $login = $_SERVER[strtoupper($mapping['login'])] ?? null; - } else { - $login = $_SERVER['HTTP_' . strtoupper($mapping['login'])] ?? null; - } + $headers = apache_request_headers(); + $login = $headers[$mapping['login']] ?? ''; if (empty($login)) { return ['errors' => 'Authentication Failed : login not present in header']; }