diff --git a/src/api/api.c b/src/api/api.c index 2c132fc5..61a98455 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -44,6 +44,7 @@ static struct { { "/api/groups", "/{name}", api_list, { false, true, 0 }, true, HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_DELETE }, { "/api/lists", "/{list}", api_list, { false, true, 0 }, true, HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_DELETE }, { "/api/info/client", "", api_info_client, { false, true, 0 }, false, HTTP_GET }, + { "/api/info/login", "", api_info_login, { false, true, 0 }, false, HTTP_GET }, { "/api/info/system", "", api_info_system, { false, true, 0 }, true, HTTP_GET }, { "/api/info/database", "", api_info_database, { false, true, 0 }, true, HTTP_GET }, { "/api/info/sensors", "", api_info_sensors, { false, true, 0 }, true, HTTP_GET }, diff --git a/src/api/api.h b/src/api/api.h index f05045f3..1603ecee 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -60,6 +60,7 @@ int api_info_version(struct ftl_conn *api); int api_info_messages_count(struct ftl_conn *api); int api_info_messages(struct ftl_conn *api); int api_info_metrics(struct ftl_conn *api); +int api_info_login(struct ftl_conn *api); // Config methods int api_config(struct ftl_conn *api); diff --git a/src/api/auth.c b/src/api/auth.c index 3b531476..501f6f94 100644 --- a/src/api/auth.c +++ b/src/api/auth.c @@ -22,8 +22,6 @@ #include "daemon.h" // sha256_raw_to_hex() #include "config/password.h" -// get_https_port() -#include "webserver/webserver.h" // crypto library #include @@ -304,7 +302,6 @@ static int get_all_sessions(struct ftl_conn *api, cJSON *json) static int get_session_object(struct ftl_conn *api, cJSON *json, const int user_id, const time_t now) { cJSON *session = JSON_NEW_OBJECT(); - const bool dns = get_blockingstatus() != DNS_FAILED; // Authentication not needed if(user_id == API_AUTH_LOCALHOST || user_id == API_AUTH_EMPTYPASS) @@ -314,8 +311,6 @@ static int get_session_object(struct ftl_conn *api, cJSON *json, const int user_ JSON_ADD_NULL_TO_OBJECT(session, "sid"); JSON_ADD_NUMBER_TO_OBJECT(session, "validity", -1); JSON_ADD_ITEM_TO_OBJECT(json, "session", session); - JSON_ADD_NUMBER_TO_OBJECT(json, "https_port", get_https_port()); - JSON_ADD_BOOL_TO_OBJECT(json, "dns", dns); return 0; } @@ -328,8 +323,6 @@ static int get_session_object(struct ftl_conn *api, cJSON *json, const int user_ JSON_REF_STR_IN_OBJECT(session, "csrf", auth_data[user_id].csrf); JSON_ADD_NUMBER_TO_OBJECT(session, "validity", auth_data[user_id].valid_until - now); JSON_ADD_ITEM_TO_OBJECT(json, "session", session); - JSON_ADD_NUMBER_TO_OBJECT(json, "https_port", get_https_port()); - JSON_ADD_BOOL_TO_OBJECT(json, "dns", dns); return 0; } @@ -339,8 +332,6 @@ static int get_session_object(struct ftl_conn *api, cJSON *json, const int user_ JSON_ADD_NULL_TO_OBJECT(session, "sid"); JSON_ADD_NUMBER_TO_OBJECT(session, "validity", -1); JSON_ADD_ITEM_TO_OBJECT(json, "session", session); - JSON_ADD_NUMBER_TO_OBJECT(json, "https_port", get_https_port()); - JSON_ADD_BOOL_TO_OBJECT(json, "dns", dns); return 0; } diff --git a/src/api/docs/content/specs/auth.yaml b/src/api/docs/content/specs/auth.yaml index 48a24cbd..2a8df9ce 100644 --- a/src/api/docs/content/specs/auth.yaml +++ b/src/api/docs/content/specs/auth.yaml @@ -26,8 +26,6 @@ components: $ref: 'auth.yaml#/components/examples/no_login_required' login_required: $ref: 'auth.yaml#/components/examples/login_required' - dns_failure: - $ref: 'auth.yaml#/components/examples/dns_failure' post: summary: Submit password for login tags: @@ -245,12 +243,6 @@ components: validity: type: integer description: Remaining lifetime of this session unless refreshed (seconds) - https_port: - type: integer - description: HTTPS port of the Pi-hole webserver (0 if disabled) - dns: - type: boolean - description: Whether the DNS server is up and running. False only in failed state password: type: object @@ -374,8 +366,6 @@ components: sid: null csrf: null validity: 300 - https_port: 443 - dns: true no_login_required: summary: No login required for this client value: @@ -385,8 +375,6 @@ components: sid: null csrf: null validity: -1 - https_port: 443 - dns: true login_required: summary: Login required value: @@ -396,8 +384,6 @@ components: sid: null csrf: null validity: -1 - https_port: 443 - dns: true login_failed: summary: Login failed value: @@ -407,19 +393,6 @@ components: sid: null csrf: null validity: -1 - https_port: 443 - dns: true - dns_failure: - summary: DNS server failure - value: - session: - valid: false - totp: false - sid: null - csrf: null - validity: -1 - https_port: 443 - dns: false errors: no_payload: summary: Bad request (no valid JSON payload) diff --git a/src/api/docs/content/specs/info.yaml b/src/api/docs/content/specs/info.yaml index 08fc9f63..b6d53978 100644 --- a/src/api/docs/content/specs/info.yaml +++ b/src/api/docs/content/specs/info.yaml @@ -278,6 +278,23 @@ components: allOf: - $ref: 'common.yaml#/components/errors/unauthorized' - $ref: 'common.yaml#/components/schemas/took' + login: + get: + summary: Login page related information + tags: + - "FTL information" + operationId: "get_logininfo" + description: | + This API hook returns information used on the login page to possibly display messages/warnings. + responses: + '200': + description: OK + content: + application/json: + schema: + allOf: + - $ref: 'info.yaml#/components/schemas/login' + - $ref: 'common.yaml#/components/schemas/took' schemas: client: @@ -1068,6 +1085,15 @@ components: type: integer description: Number of items example: 42 + login: + type: object + properties: + https_port: + type: integer + description: HTTPS port of the Pi-hole webserver (0 if disabled) + dns: + type: boolean + description: Whether the DNS server is up and running. False only in failed state examples: errors: messages: diff --git a/src/api/docs/content/specs/main.yaml b/src/api/docs/content/specs/main.yaml index 817c0606..9b9121e2 100644 --- a/src/api/docs/content/specs/main.yaml +++ b/src/api/docs/content/specs/main.yaml @@ -190,6 +190,9 @@ paths: /info/metrics: $ref: 'info.yaml#/components/paths/metrics' + /info/login: + $ref: 'info.yaml#/components/paths/login' + /logs/dnsmasq: $ref: 'logs.yaml#/components/paths/logs/dnsmasq' diff --git a/src/api/info.c b/src/api/info.c index 6d9a2b42..312f52d9 100644 --- a/src/api/info.c +++ b/src/api/info.c @@ -49,6 +49,9 @@ #include #include "metrics.h" +// get_https_port() +#include "webserver/webserver.h" + // DIR #include @@ -1032,3 +1035,15 @@ int api_info_metrics(struct ftl_conn *api) JSON_ADD_ITEM_TO_OBJECT(json2, "metrics", json); JSON_SEND_OBJECT(json2); } + +int api_info_login(struct ftl_conn *api) +{ + cJSON *json = JSON_NEW_OBJECT(); + + const bool dns = get_blockingstatus() != DNS_FAILED; + JSON_ADD_BOOL_TO_OBJECT(json, "dns", dns); + + JSON_ADD_NUMBER_TO_OBJECT(json, "https_port", get_https_port()); + + JSON_SEND_OBJECT(json); +} diff --git a/test/test_suite.bats b/test/test_suite.bats index 480c3c3d..7f43d27d 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -1266,7 +1266,7 @@ @test "API authorization (without password): No login required" { run bash -c 'curl -s 127.0.0.1/api/auth' printf "%s\n" "${lines[@]}" - [[ ${lines[0]} == '{"session":{"valid":true,"totp":false,"sid":null,"validity":-1},"https_port":443,"dns":true,"took":'*'}' ]] + [[ ${lines[0]} == '{"session":{"valid":true,"totp":false,"sid":null,"validity":-1},"took":'*'}' ]] } @test "API authorization: Setting password" {