Skip to content

Commit

Permalink
Merge pull request #3 from PavloMalko/master
Browse files Browse the repository at this point in the history
Add auth by API key in header
  • Loading branch information
smotornyuk authored Dec 9, 2024
2 parents e10b266 + 48254e6 commit 42db9b7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ckanext/drupal_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
}
6 changes: 6 additions & 0 deletions ckanext/drupal_api/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions ckanext/drupal_api/config_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions ckanext/drupal_api/logic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
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)
Expand Down

0 comments on commit 42db9b7

Please sign in to comment.