From d75b69a39f403b8e8a69b876236e76bee44490df Mon Sep 17 00:00:00 2001 From: Pavel Malko Date: Mon, 9 Dec 2024 03:49:50 +0200 Subject: [PATCH 1/2] add api key auth in header --- ckanext/drupal_api/config.py | 23 ++++++++++++++++++++++ ckanext/drupal_api/config_declaration.yaml | 6 ++++++ ckanext/drupal_api/config_schema.yaml | 9 +++++++++ ckanext/drupal_api/logic/api.py | 5 ++++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/ckanext/drupal_api/config.py b/ckanext/drupal_api/config.py index 5fd2857..77d4fcd 100644 --- a/ckanext/drupal_api/config.py +++ b/ckanext/drupal_api/config.py @@ -17,6 +17,10 @@ CONFIG_REQUEST_HTTP_USER = "ckanext.drupal_api.request.user" CONFIG_REQUEST_HTTP_PASS = "ckanext.drupal_api.request.pass" +CONFIG_REQUEST_HTTP_HEADER_KEY = "ckanext.drupal_api.request.header.key" +CONFIG_REQUEST_HTTP_HEADER_VALUE = "ckanext.drupal_api.request.header.value" +DEFAULT_REQUEST_HEADER_KEY = "X-CKAN-API-Key" + CONFIG_DRUPAL_API_VERSION = "ckanext.drupal_api.api_version" JSON_API = "json" CORE_API = "core" @@ -57,6 +61,11 @@ def get_http_user() -> str | None: def get_http_pass() -> str | None: return tk.config[CONFIG_REQUEST_HTTP_PASS] +def get_http_header_key() -> str: + return tk.config[CONFIG_REQUEST_HTTP_HEADER_KEY] or DEFAULT_REQUEST_HEADER_KEY + +def get_http_header_value() -> str | None: + return tk.config[CONFIG_REQUEST_HTTP_HEADER_VALUE] def get_config_options() -> dict[str, dict[str, Any]]: """Defines how we are going to render the global configuration @@ -127,4 +136,18 @@ def get_config_options() -> dict[str, dict[str, Any]]: "validators": [unicode_safe], "type": "password", }, + "http_header_key": { + "key": CONFIG_REQUEST_HTTP_HEADER_KEY, + "label": tk._("HTTP header auth key"), + "value": get_http_header_key(), + "validators": [default(DEFAULT_REQUEST_HEADER_KEY), unicode_safe], + "type": "text", + }, + "http_header_value": { + "key": CONFIG_REQUEST_HTTP_HEADER_VALUE, + "label": tk._("HTTP header auth key value"), + "value": get_http_header_value(), + "validators": [unicode_safe], + "type": "password", + }, } diff --git a/ckanext/drupal_api/config_declaration.yaml b/ckanext/drupal_api/config_declaration.yaml index 99d4e1e..398dd10 100644 --- a/ckanext/drupal_api/config_declaration.yaml +++ b/ckanext/drupal_api/config_declaration.yaml @@ -21,6 +21,12 @@ groups: - key: ckanext.drupal_api.request.pass editable: true + + - key: ckanext.drupal_api.request.header.key + editable: true + + - key: ckanext.drupal_api.request.header.value + editable: true - key: ckanext.drupal_api.api_version default: core diff --git a/ckanext/drupal_api/config_schema.yaml b/ckanext/drupal_api/config_schema.yaml index 1bf4ea8..402c2aa 100644 --- a/ckanext/drupal_api/config_schema.yaml +++ b/ckanext/drupal_api/config_schema.yaml @@ -47,3 +47,12 @@ fields: label: HTTP auth password validators: unicode_safe input_type: password + + - field_name: ckanext.drupal_api.request.header.key + label: HTTP header auth key + validators: unicode_safe + + - field_name: ckanext.drupal_api.request.header.value + label: HTTP header auth key value + validators: unicode_safe + input_type: password diff --git a/ckanext/drupal_api/logic/api.py b/ckanext/drupal_api/logic/api.py index 7965223..1a03935 100644 --- a/ckanext/drupal_api/logic/api.py +++ b/ckanext/drupal_api/logic/api.py @@ -17,16 +17,19 @@ def make_request(url: str) -> dict: http_user = da_conf.get_http_user() http_pass = da_conf.get_http_pass() + http_header_key = da_conf.get_http_header_key() + http_header_value = da_conf.get_http_header_value() timeout = da_conf.get_request_timeout() session = requests.Session() if http_user and http_pass: session.auth = (http_user, http_pass) + if http_header_key and http_header_value: + session.headers[http_header_key] = http_header_value _add_drupal_session(session) req = session.get(url, timeout=timeout) - req.raise_for_status() return req.json() From 48254e6c1667b10de417f8cc2707a97dcd06492c Mon Sep 17 00:00:00 2001 From: Pavlo Malko Date: Mon, 9 Dec 2024 03:58:17 +0200 Subject: [PATCH 2/2] fix make_request --- ckanext/drupal_api/logic/api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ckanext/drupal_api/logic/api.py b/ckanext/drupal_api/logic/api.py index 1a03935..41b97ab 100644 --- a/ckanext/drupal_api/logic/api.py +++ b/ckanext/drupal_api/logic/api.py @@ -30,6 +30,7 @@ def make_request(url: str) -> dict: _add_drupal_session(session) req = session.get(url, timeout=timeout) + req.raise_for_status() return req.json()