Skip to content

Add JWT token authentication#717

Merged
amotl merged 2 commits intomainfrom
jwt
Mar 3, 2026
Merged

Add JWT token authentication#717
amotl merged 2 commits intomainfrom
jwt

Conversation

@amotl
Copy link
Member

@amotl amotl commented May 24, 2025

CrateDB 5.7.0 introduced JWT token authentication. Let's also implement it on the client sides.

/cc @matriv, @kneth

@coderabbitai
Copy link

coderabbitai bot commented May 24, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

JWT token authentication support was added to the CrateDB Python client. This includes updates to the connection and HTTP client classes to accept and pass JWT tokens, documentation and changelog updates, and new tests to verify JWT authentication behavior. Existing authentication mechanisms remain unchanged.

Changes

File(s) Change Summary
CHANGES.rst Updated changelog to mention the new JWT token authentication feature.
docs/connect.rst Added documentation and example for JWT token authentication in the connection setup.
src/crate/client/connection.py Extended Connection.__init__ to accept and document a jwt_token parameter.
src/crate/client/http.py Added jwt_token parameter to Client and Server for JWT authentication; sets Authorization header.
tests/client/test_http.py Enhanced test handler for "Bearer" JWT tokens; added/updated tests for JWT token authentication.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Connection
    participant Client
    participant Server

    User->>Connection: connect(jwt_token=...)
    Connection->>Client: __init__(jwt_token=...)
    Client->>Server: request(..., jwt_token=...)
    Server->>Server: Add Authorization: Bearer <jwt_token> header (if not set)
    Server-->>Client: Response
    Client-->>Connection: Response
    Connection-->>User: Connection established
Loading

Poem

In fields of code, a token hops—
JWT in paw, it never stops.
Now clients greet with bearer flair,
Securely bounding here and there.
The docs are plump, the tests are keen,
This rabbit’s code is fresh and clean!
🐇✨

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch jwt

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@amotl amotl requested review from seut and surister May 24, 2025 00:09
@amotl amotl marked this pull request as ready for review May 24, 2025 00:15
Copy link
Member

@seut seut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
Maybe adding a sanity check to give a good error if token and user/password auth is used in conjunction would be useful. Afaik, the user/password args would be used then instead (it will just override the Authorization header afaik) which may not be clear to a user.

@kneth
Copy link
Member

kneth commented Jun 2, 2025

Thank you for providing support for JWT!

@amotl amotl self-assigned this Oct 2, 2025
@surister

This comment was marked as outdated.

@amotl

This comment was marked as outdated.

@amotl amotl removed their assignment Dec 9, 2025
@amotl

This comment was marked as outdated.

@amotl

This comment was marked as outdated.

Comment on lines +193 to +198
# Sanity checks.
if jwt_token is not None and username is not None:
raise ValueError(
"Either JWT tokens are accepted, "
"or user credentials, but not both"
)
Copy link
Member Author

@amotl amotl Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding a sanity check to give a good error if token and user/password auth is used in conjunction would be useful.

Good idea. a1ac249 just added this additional sanity check as suggested.

@amotl amotl merged commit 3d664bb into main Mar 3, 2026
16 checks passed
@amotl amotl deleted the jwt branch March 3, 2026 09:59
Comment on lines 164 to 177
def request(
self,
method,
path,
data=None,
stream=False,
headers=None,
username=None,
password=None,
jwt_token=None,
schema=None,
backoff_factor=0,
**kwargs,
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks ABI for positional arguments. Please always add new arguments last.

But also in general a bit odd that the function takes jwt_token and headers, where the jwt_token ends up becoming part of the header. Might make sense to move this to the caller who provides the header?

Copy link
Member Author

@amotl amotl Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks ABI for positional arguments. Please always add new arguments last.

Apologies. I received this too late for 2.1.0. I will change it and release 2.1.1 right away. ✅

Comment on lines 31 to 54
def __init__(
self,
servers=None,
timeout=None,
backoff_factor=0,
client=None,
verify_ssl_cert=True,
ca_cert=None,
error_trace=False,
cert_file=None,
key_file=None,
ssl_relax_minimum_version=False,
username=None,
password=None,
jwt_token=None,
schema=None,
pool_size=None,
socket_keepalive=True,
socket_tcp_keepidle=None,
socket_tcp_keepintvl=None,
socket_tcp_keepcnt=None,
converter=None,
time_zone=None,
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment about positional arguments

Copy link
Member Author

@amotl amotl Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Will fix and run an aftermath release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants