Skip to content

PYCO-92: Add JWT authenticator#12

Merged
thejcfactor merged 2 commits intocouchbase:mainfrom
anirudhlakhotia:JWT
May 6, 2026
Merged

PYCO-92: Add JWT authenticator#12
thejcfactor merged 2 commits intocouchbase:mainfrom
anirudhlakhotia:JWT

Conversation

@anirudhlakhotia
Copy link
Copy Markdown
Contributor

  • Adds Credential.from_jwt(token); sends Authorization: Bearer <jwt> on every HTTP request.
  • Replaces httpx BasicAuth with a DynamicCredentialAuth flow that reads the current credential at request time, so credential rotation takes effect without rebuilding the HTTP client.
  • Adds Cluster.set_credential() (sync + async) to rotate credentials at runtime. Switching credential types at runtime is rejected.
  • Pre-computes the Authorization header on Credential construction so per-request dispatch is a cheap attribute read.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds JWT-based authentication and runtime credential rotation to the Couchbase Analytics Python SDK, shifting HTTP auth to a request-time header injection model so credential updates take effect without rebuilding the underlying httpx client.

Changes:

  • Introduces Credential.from_jwt() and CredentialType, with Credential precomputing an Authorization header for both password and JWT credentials.
  • Replaces httpx.BasicAuth usage with DynamicCredentialAuth, which reads the current credential from _ConnectionDetails on each request.
  • Adds sync/async Cluster.set_credential() APIs plus adapter support to rotate credentials at runtime (rejecting credential-type switches), and adds unit tests for the new behavior.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
couchbase_analytics/common/credential.py Adds JWT credential support, credential typing, and precomputed Authorization header generation.
couchbase_analytics/protocol/_core/auth.py New httpx.Auth implementation that injects the current credential’s Authorization header at request time.
couchbase_analytics/protocol/_core/client_adapter.py Switches from BasicAuth to DynamicCredentialAuth; adds runtime credential update method.
couchbase_analytics/protocol/connection.py Stores Credential directly in _ConnectionDetails (instead of a username/password tuple).
couchbase_analytics/protocol/cluster.py Adds Cluster.set_credential() (protocol layer) delegating to the adapter.
couchbase_analytics/protocol/cluster.pyi Exposes set_credential() in protocol cluster typing.
couchbase_analytics/cluster.py Adds public sync Cluster.set_credential() API and documentation.
couchbase_analytics/cluster.pyi Exposes set_credential() in public sync cluster typing.
acouchbase_analytics/protocol/_core/client_adapter.py Mirrors sync adapter: uses DynamicCredentialAuth; adds async credential update method.
acouchbase_analytics/protocol/cluster.py Adds async protocol set_credential() delegating to the adapter.
acouchbase_analytics/protocol/cluster.pyi Exposes async set_credential() in protocol typing.
acouchbase_analytics/cluster.py Adds public async Cluster.set_credential() API and documentation.
acouchbase_analytics/cluster.pyi Exposes set_credential() in public async cluster typing.
couchbase_analytics/tests/credential_t.py New sync unit tests covering JWT credentials, header formatting/redaction, dynamic auth, and rotation/type-switch rejection.
acouchbase_analytics/tests/credential_t.py New async unit tests covering the same behaviors for async adapter/cluster.
conftest.py Registers the new credential unit test suites in the unit test list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 33 out of 33 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread acouchbase_analytics/cluster.py Outdated
Comment thread couchbase_analytics/common/credential.py Outdated
Comment thread couchbase_analytics/common/credential.py Outdated
Comment thread couchbase_analytics/cluster.py Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 34 out of 34 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread couchbase_analytics/common/credential.py
Comment thread couchbase_analytics/tests/credential_t.py
Comment thread acouchbase_analytics/tests/credential_t.py
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread couchbase_analytics/protocol/_core/auth.py Outdated
Comment thread couchbase_analytics/common/credential.py
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

couchbase_analytics/common/credential.py:162

  • Credential.__repr__ renders JWT credentials as Credential(token=****), but the public constructor parameter and factory method use jwt_token / from_jwt. Using a different label in repr makes debugging harder (it looks like a different API). Consider changing this to Credential(jwt_token=****) (or Credential(jwt=****)) to match the public naming.
    def __repr__(self) -> str:
        if self._kind == 'password':
            return f'Credential(username={self._username!r}, password=****)'
        return 'Credential(token=****)'


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread couchbase_analytics/common/credential.py
Comment thread couchbase_analytics/protocol/_core/auth.py
Comment thread couchbase_analytics/protocol/connection.py
# Conflicts:
#	acouchbase_analytics/cluster.py
#	acouchbase_analytics/cluster.pyi
#	acouchbase_analytics/protocol/_core/client_adapter.py
#	couchbase_analytics/cluster.py
#	couchbase_analytics/cluster.pyi
#	couchbase_analytics/protocol/_core/client_adapter.py
#	couchbase_analytics/protocol/cluster.pyi
The test adapters subclass _ClientAdapter / _AsyncClientAdapter and override
__init__ to wrap an existing adapter, copying its private attributes.  After
the _CredentialHolder refactor, the new _credential_holder attribute also
needs to be copied or test_server tests fail with AttributeError.
@thejcfactor thejcfactor merged commit c719afd into couchbase:main May 6, 2026
37 of 43 checks passed
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.

3 participants