Skip to main content

Client for the KBase Authentication Service

Project description

Auth2 client for Python

This repo contains a minimal client for the KBase Auth2 server, covering only the most common operations - e.g. validating tokens and user names and getting user roles.

Most other uses are easily done with any http/REST client like requests or httpx.

Installation

TODO INSTALL setup a KBase pypi org and publish there

Usage

Both sync and async versions of the client are provided - KBaseAuthClient and AsyncKBaseAuthClient, respectively. Here we demonstrate usage of the async client - to use the sync client, just switch the client name when creating the client and remove the async and await keywords. The examples assume there is a valid KBase token in the token variable.

Note that all methods have internal caches and further caching is not necessary.

Replace the CI environment url with the url of the environment you wish to query.

Get the version of the auth service

from kbase.auth import AsyncKBaseAuthClient

async with await AsyncKBaseAuthClient.create("https://ci.kbase.us/services/auth") as cli:
    print(await cli.service_version())
0.7.2

Get a token

This is the cheapest method to get a KBase username from a token.

from kbase.auth import AsyncKBaseAuthClient

async with await AsyncKBaseAuthClient.create("https://ci.kbase.us/services/auth") as cli:
    print(await cli.get_token(token))
Token(id='67797406-c6a3-4ee0-870d-976739dacd61', user='gaprice', created=1755561300704, expires=1763337300704, cachefor=300000)

Get a user

from kbase.auth import AsyncKBaseAuthClient

async with await AsyncKBaseAuthClient.create("https://ci.kbase.us/services/auth") as cli:
    print(await cli.get_user(token))
User(user='gaprice', customroles=['KBASE_STAFF', 'goofypants'])

Validate usernames

from kbase.auth import AsyncKBaseAuthClient

async with await AsyncKBaseAuthClient.create("https://ci.kbase.us/services/auth") as cli:
    print(await cli.validate_usernames(token, "gaprice", "superfake"))
{'gaprice': True, 'superfake': False}

Without a context manager

The clients can be used without a context manager, in which case the user is responsible for ensuring they're closed:

from kbase.auth import AsyncKBaseAuthClient

cli = await AsyncKBaseAuthClient.create("https://ci.kbase.us/services/auth")

await cli.close()

Development

Creating the synchronous client

The synchronous client is generated from the asynchronous client code - do not make any changes in the _sync directory as they will be overwritten.

To update the synchronous code after modifying the asynchronous code run

uv sync --dev  # only required on first run or when the uv.lock file changes
uv run scripts/process_unasync.py

Adding and releasing code

  • Adding code
    • All code additions and updates must be made as pull requests directed at the develop branch.
      • All tests must pass and all new code must be covered by tests.
      • All new code must be documented appropriately
        • Pydocs
        • General documentation if appropriate
        • Release notes
  • Releases
    • The main branch is the stable branch. Releases are made from the develop branch to the main branch.
    • Update the version in auth.py and pyproject.toml.
    • Tag the version in git and github.
    • Create a github release.

Testing

uv sync --dev  # only required on first run or when the uv.lock file changes
PYTHONPATH=src uv run pytest test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kbase_auth_client-0.1.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kbase_auth_client-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file kbase_auth_client-0.1.0.tar.gz.

File metadata

  • Download URL: kbase_auth_client-0.1.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for kbase_auth_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7912c7111881eab73b19e546eeeccf42508667fa8f67eb4ac5f5077a299d4894
MD5 7effa4f1ca1cc107c87d09f7b53ad634
BLAKE2b-256 b3e64df589b78744b9c8bc1eedfc63cdb9c7e358dd33d67ee0443351f4bebd4e

See more details on using hashes here.

File details

Details for the file kbase_auth_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kbase_auth_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c39b914f03602ad707839c6850621607eda49ee30651b88dd66e95350354767f
MD5 7d84640a6b6b0fe879fb7e4e450c0a61
BLAKE2b-256 fd431ff87ff6c16061fdc25e0c395b9e86594788e7f86bee526058b8438c47bd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page