Skip to content

Access Token Lifecycle Management #58

Description

@Manu21012000

Feature Proposal: Built-in Access Token Lifecycle Management

Hi maintainers,

First, thank you for providing and maintaining the Python SDK.

While integrating Kopo Kopo into a Django application [and other frameworks too ], I noticed that the SDK currently provides the primitives necessary to obtain and inspect access tokens through TokenService, but token lifecycle management is left entirely to SDK consumers.

Current Implementation

The relevant functionality is implemented in:

k2connect/token_service.py

Specifically:

  • request_access_token()
  • request_token_info()
  • introspect_access_token()
  • revoke_access_token()

The authorization documentation states:

  • Access tokens are valid for 1 hour (expires_in)
  • Only one access token is valid at a time

As a result, applications need to:

  1. Request an access token
  2. Store it
  3. Track its expiration
  4. Re-acquire a new token before expiration
  5. Avoid unnecessary token requests

Real-World Experience

In a production integration, I found myself implementing additional logic outside the SDK to:

  • Cache the current token
  • Track expiration using expires_in
  • Prevent duplicate token requests
  • Automatically obtain a new token when required

This functionality seems likely to be reimplemented by most SDK users.

Proposed Enhancement

Introduce an optional helper method that transparently manages token validity.

Example:

token_service = k2connect.Tokens

access_token = token_service.get_valid_token()

Possible behavior:

  • Return cached token if still valid
  • Request a new token when expired (or close to expiry)
  • Cache token metadata internally
  • Remain fully backward compatible with existing APIs

Pseudo implementation:

def get_valid_token(self):
    if self._token and not self._is_expired():
        return self._token

    response = self.request_access_token()

    self._token = response["access_token"]
    self._expires_at = (
        current_time + response["expires_in"]
    )

    return self._token

##it will be helpful in

  • Reducing duplicated authentication code across applications
  • Reducing unnecessary authorization requests
  • Improving developer experience
  • Keeps existing SDK interfaces unchanged and
  • Provides a more production-ready authentication workflow

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions