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
pip install kbase-auth-client
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='fe042c54-0eb6-4bd6-a7cc-3c1c4d0228d2', user='gaprice', type=<TokenType.DEVELOPER: 3>, mfa=<MFAStatus.UNKNOWN: 3>, created=1763674630238, expires=1771450630238, 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
- All code additions and updates must be made as pull requests directed at the develop branch.
- Releases
- The main branch is the stable branch. Releases are made from the develop branch to the main branch.
- Update the version in
auth.pyandpyproject.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kbase_auth_client-0.1.2.tar.gz.
File metadata
- Download URL: kbase_auth_client-0.1.2.tar.gz
- Upload date:
- Size: 38.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de421abdc353e9957e3abb5d1dc945a1acfc6b36601a6034b8e198c0dfea522f
|
|
| MD5 |
fef3dd380396df0333da0f8216270ac1
|
|
| BLAKE2b-256 |
1eaf9b74a6a73e590924884ea09992d88bb1455880e269b41949624e63d15690
|
File details
Details for the file kbase_auth_client-0.1.2-py3-none-any.whl.
File metadata
- Download URL: kbase_auth_client-0.1.2-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08e1689a560d4b6b6329210bdd7560ebbf382d6d731508227f5f13d19afe1de4
|
|
| MD5 |
7e3ce5edf59d7b9d032e887fd34f0150
|
|
| BLAKE2b-256 |
575106e0f2ae9da51faf193439ded8cdd616d7a5380e50db90a398c21e9440e7
|