Skip to content

Commit a1ac249

Browse files
committed
JWT token authentication: Add sanity checks
1 parent 7ae4445 commit a1ac249

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/crate/client/http.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ def request(
190190
if length is not None:
191191
headers["Content-Length"] = str(length)
192192

193+
# Sanity checks.
194+
if jwt_token is not None and username is not None:
195+
raise ValueError(
196+
"Either JWT tokens are accepted, "
197+
"or user credentials, but not both"
198+
)
199+
193200
# Authentication token
194201
if jwt_token is not None and "Authorization" not in headers:
195202
headers["Authorization"] = "Bearer %s" % jwt_token

tests/client/test_http.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,15 @@ def test_credentials(serve_http):
725725
assert conn.client.jwt_token == jwt_token
726726
conn.client.sql("select 3;")
727727
assert server.SHARED["jwt_token"] == jwt_token
728+
729+
730+
def test_credentials_and_token(serve_http):
731+
"""
732+
Verify exception when user provides both credentials and token.
733+
"""
734+
with serve_http(SharedStateRequestHandler) as (server, url):
735+
with pytest.raises(ProgrammingError) as excinfo:
736+
connect(url, username="foo", jwt_token="bar")
737+
assert excinfo.match(
738+
"Either JWT tokens are accepted, or user credentials, but not both"
739+
)

0 commit comments

Comments
 (0)