Skip to content

Commit 4fbc38f

Browse files
committed
Enhance GraphQLClient token handling to support both API keys and JWTs
- Updated the token handling logic in `GraphQLClient` to differentiate between long-lived API keys (prefixed with `rfs`) and short-lived JWTs. - API keys are sent in the `X-API-Key` header, while JWTs are sent in the `Authorization` header as a Bearer token. - This change ensures proper authentication for various client use cases, maintaining compatibility with existing conventions.
1 parent 2710a53 commit 4fbc38f

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎robosystems_client/graphql/client.py‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,24 @@ def __init__(
6464
if headers:
6565
self._headers.update(headers)
6666
if token:
67-
# Match the existing facade convention: send the token as X-API-Key.
68-
self._headers["X-API-Key"] = token
67+
# The backend accepts two credential formats, and they go in
68+
# DIFFERENT headers — not interchangeable:
69+
#
70+
# - Long-lived API keys (``rfs…`` prefix) → ``X-API-Key``.
71+
# Validated against the api_keys table.
72+
# - Short-lived JWTs → ``Authorization: Bearer …``. Validated
73+
# by the JWT middleware.
74+
#
75+
# Sending a JWT as ``X-API-Key`` (or an API key as Bearer) both
76+
# fail with 401 "Invalid API key". Python callers overwhelmingly
77+
# pass ``rfs…`` keys since this client targets server/CLI/MCP
78+
# use cases, but we keep the routing symmetric with the TS
79+
# client so a caller that forwards a JWT (e.g. a backend
80+
# proxying a request-scoped token) still works.
81+
if token.startswith("rfs"):
82+
self._headers["X-API-Key"] = token
83+
else:
84+
self._headers["Authorization"] = f"Bearer {token}"
6985

7086
def _url_for(self, graph_id: str) -> str:
7187
if not graph_id:

0 commit comments

Comments
 (0)