From b12a22cf2c1608367a6622ff35559bc0be464580 Mon Sep 17 00:00:00 2001 From: xmapdev <261785740+xmapdev@users.noreply.github.com> Date: Mon, 16 Feb 2026 00:22:04 +0100 Subject: [PATCH] fix: fetch API key from stash server configuration Fixes an issue where the application does not include an API key in the connection fragment given to the plugin upon initialization, but still requires an API key for authentication with the GraphQL endpoint. In this case, a session cookie is provided instead. However, the GraphQL client and wrapper does not support authenticating via cookies. In this scenario, subsequent calls to the GraphQL endpoint fails since no API key is provided. This commit adds a helper function that is used to retrieve the API key if it is missing from the connection fragment upon plugin load. The API key is then used in subsequent calls to the application. --- .../stashdb-tag-sync/src/stashdbTagSync.py | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/plugins/stashdb-tag-sync/src/stashdbTagSync.py b/plugins/stashdb-tag-sync/src/stashdbTagSync.py index 101e697..b6a9772 100644 --- a/plugins/stashdb-tag-sync/src/stashdbTagSync.py +++ b/plugins/stashdb-tag-sync/src/stashdbTagSync.py @@ -12,6 +12,7 @@ logging.getLogger('stash_graphql_client').setLevel(logging.CRITICAL) try: + from stashapi.stashapp import StashInterface from graphql_client import StashDBClient from stash_client import StashClient from core.tag_transfer import transfer_tags_graphql @@ -22,6 +23,41 @@ sys.exit(1) +async def fetch_api_key(server_conn: Dict[str, Any]) -> str: + """Fetch Stash API key from configuration + + Connects to Stash using the configuration fragment received during plugin + initialization, and queries Stash's general configuration to get the server + API key (if any). + + This is required when Stash generates a session cookie to use for + authentication instead of an API key, but the API key is needed for + the GraphQL client as it does not support authentication via cookies. + + Args: + server_conn: The `server_connection` fragment from plugin init + + Returns: + A string with the API key (if found). Otherwise empty string. + """ + stash_client = StashInterface(server_conn) + + config = stash_client.get_configuration() + if not config: + log.error("No configuration was retrieved") + return "" + + if 'general' in config and 'apiKey' in config.get('general', {}): + api_key = config['general']['apiKey'] or "" + logging.debug(f'Fetched API key from configuration: {api_key[0:16]}...') + if not api_key: + log.warning("API key configuration found but empty!") + return api_key + + log.error("No API key configuration found in Stash") + return "" + + async def fetch_stashdb_config(stash_client: StashClient) -> tuple[str, str]: """Fetch StashDB configuration from Stash's configuration. @@ -83,12 +119,15 @@ async def plugin_main(input_data: Dict[str, Any]) -> None: # Extract server connection information server_conn = input_data.get("server_connection", {}) + # Fetch API key from server config if not given in connection fragment + api_key = server_conn.get("ApiKey", await fetch_api_key(server_conn)) + # Create Stash connection to query configuration stash_conn = StashConnection( scheme=server_conn.get("Scheme", "http"), host=server_conn.get("Host", "localhost"), port=server_conn.get("Port", 9999), - api_key=server_conn.get("ApiKey") + api_key=api_key ) # Use defaults for other settings @@ -105,7 +144,7 @@ async def plugin_main(input_data: Dict[str, Any]) -> None: scheme=stash_conn.scheme, host=stash_conn.host, port=stash_conn.port, - api_key="" + api_key=api_key ) # Temporarily suppress stash_graphql_client's noisy warnings during config fetch