Skip to content

Commit 9c90fdc

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

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/crate/client/http.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ 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, or user credentials, but not both"
197+
)
198+
193199
# Authentication token
194200
if jwt_token is not None and "Authorization" not in headers:
195201
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)