diff --git a/src/api/auth.c b/src/api/auth.c index 5b535b72..dbb21b6c 100644 --- a/src/api/auth.c +++ b/src/api/auth.c @@ -358,24 +358,6 @@ void delete_all_sessions(void) static int send_api_auth_status(struct ftl_conn *api, const int user_id, const time_t now) { - if(user_id == API_AUTH_LOCALHOST) - { - log_debug(DEBUG_API, "API Auth status: OK (localhost does not need auth)"); - - cJSON *json = JSON_NEW_OBJECT(); - get_session_object(api, json, user_id, now); - JSON_SEND_OBJECT(json); - } - - if(user_id == API_AUTH_EMPTYPASS) - { - log_debug(DEBUG_API, "API Auth status: OK (empty password)"); - - cJSON *json = JSON_NEW_OBJECT(); - get_session_object(api, json, user_id, now); - JSON_SEND_OBJECT(json); - } - if(user_id > API_AUTH_UNAUTHORIZED && (api->method == HTTP_GET || api->method == HTTP_POST)) { log_debug(DEBUG_API, "API Auth status: OK"); @@ -392,18 +374,45 @@ static int send_api_auth_status(struct ftl_conn *api, const int user_id, const t get_session_object(api, json, user_id, now); JSON_SEND_OBJECT(json); } - else if(user_id > API_AUTH_UNAUTHORIZED && api->method == HTTP_DELETE) + else if(api->method == HTTP_DELETE) { - log_debug(DEBUG_API, "API Auth status: Logout, asking to delete cookie"); + if(user_id > API_AUTH_UNAUTHORIZED) + { + log_debug(DEBUG_API, "API Auth status: Logout, asking to delete cookie"); - strncpy(pi_hole_extra_headers, FTL_DELETE_COOKIE, sizeof(pi_hole_extra_headers)); + strncpy(pi_hole_extra_headers, FTL_DELETE_COOKIE, sizeof(pi_hole_extra_headers)); - // Revoke client authentication. This slot can be used by a new client afterwards. - const int code = delete_session(user_id) ? 204 : 404; + // Revoke client authentication. This slot can be used by a new client afterwards. + const int code = delete_session(user_id) ? 204 : 404; - // Send empty reply with appropriate HTTP status code - send_http_code(api, "application/json; charset=utf-8", code, ""); - return code; + // Send empty reply with appropriate HTTP status code + send_http_code(api, "application/json; charset=utf-8", code, ""); + return code; + } + else + { + log_debug(DEBUG_API, "API Auth status: Logout, but not authenticated"); + + cJSON *json = JSON_NEW_OBJECT(); + get_session_object(api, json, user_id, now); + JSON_SEND_OBJECT_CODE(json, 401); // 401 Unauthorized + } + } + else if(user_id == API_AUTH_LOCALHOST) + { + log_debug(DEBUG_API, "API Auth status: OK (localhost does not need auth)"); + + cJSON *json = JSON_NEW_OBJECT(); + get_session_object(api, json, user_id, now); + JSON_SEND_OBJECT(json); + } + else if(user_id == API_AUTH_EMPTYPASS) + { + log_debug(DEBUG_API, "API Auth status: OK (empty password)"); + + cJSON *json = JSON_NEW_OBJECT(); + get_session_object(api, json, user_id, now); + JSON_SEND_OBJECT(json); } else {