0

I am currently working on an api which contains repositories. And I'm stuck on a PHP feature.

Overall, we have tables that are open for everyone to read so no worries and other tables that contain sensitive information and therefore require authentication. We went through basic authentication. When a table is closed, the browser will ask for a username and password. The first time it works fine.

However when the same person wants to go to another table that requires a different username and password, the browser does not offer to enter a username and password again. As a result, it does not work because it will use the username and password provided the first time and since it is not the correct ones, the API will refuse its display. Which is normal but annoying.

If the person wants to access another resource, they are obliged to either close their browser entirely and then reopen it or open a private browsing page. So I wanted to know if there was a way to tell the browser not to store login information.

I already tried header(Cache-Control: no-cache); but it doesn't work. Here is part of the code in question:

// We check the authentication
     if (!isset($_SERVER['PHP_AUTH_USER'])) {
         // If no user specified, authentication is requested
         header('WWW-Authenticate: Basic realm="API"');
         // the user will be prompted to enter his username and password from a pop-up
         // If he clicks on Cancel, he will see this access denied message.
         sendErrorResponse(401,"Unauthorized","Access denied. You did not enter a password");
     } else {
         // We retrieve the authentication information from the database
         $username = pg_escape_string($_SERVER['PHP_AUTH_USER']);
         $query = "SELECT password, token, role FROM api_information.users WHERE username = '$username'";
         $result = pg_query($conn, $query);
         if ($result === false) {
             sendErrorResponse(500,"InternalServerError","Error connecting to database");
         }

I hope my explanation is understandable enough. Good day.

Onyx
  • 35
  • 6
  • cache has nothing to do with basic auth – Your Common Sense May 23 '23 at 14:22
  • Your API needs to send the correct response to trigger the browser to ask for credentials, again, when you have determined that the current username is _not_ authorized to access the data. So you need to send the same response then, as you already did in your if-branch - a `WWW-Authenticate` header, and status code 401. – CBroe May 24 '23 at 06:41

0 Answers0