Skip to main content

Python Tokens

Build Status Code Coverage PyPI Downloads Latest PyPI version License

A Python library that keeps OAuth 2.0 service access tokens in memory for your usage.

Installation

$ sudo pip3 install --upgrade stups-tokens

Usage

import requests
import time
import tokens

# will use OAUTH2_ACCESS_TOKEN_URL environment variable by default
# will try to read application credentials from CREDENTIALS_DIR
tokens.configure(url='https://example.com/access_tokens')
tokens.manage('example', ['read', 'write'])
tokens.start()

tok = tokens.get('example')

requests.get('https://example.org/', headers={'Authorization': 'Bearer {}'.format(tok)})

time.sleep(3600) # make the token expire

tok = tokens.get('example') # will refresh the expired token
requests.get('https://example.org/', headers={'Authorization': 'Bearer {}'.format(tok)})

This library also allows reading tokens directly from a file. The token needs to be in a file name ${CREDENTIALS_DIR}/${TOKEN_NAME}-secret:

import tokens

# the environment variable CREDENTIALS_DIR must be set correctly
tokens.configure(from_file_only=True)
tokens.manage('full-access')
tok = tokens.get('full-access')

requests.get('https://example.org/', headers={'Authorization': 'Bearer {}'.format(tok)})

Local testing

The “tokens” library allows injecting fixed OAuth2 access tokens via the OAUTH2_ACCESS_TOKENS environment variable. This allows testing applications using the library locally with personal OAuth2 tokens (e.g. generated by “zign”):

$ MY_TOKEN=$(zign token -n mytok)
$ export OAUTH2_ACCESS_TOKENS=mytok=$MY_TOKEN
$ ./myapp.py # start my local Python app using the tokens library

Releasing

Uploading a new version to PyPI:

$ ./release.sh <NEW-VERSION>

Metadata

Release files for stups-tokens 1.1.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stups-tokens 1.1.19
File Size Uploaded Provenance
stups-tokens-1.1.19.tar.gz 4.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for stups-tokens 1.1.19
File Interpreter ABI Platform
stups_tokens-1.1.19-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / stups-tokens-1.1.19.tar.gz

Download URL stups-tokens-1.1.19.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7830ad83ccbfd52a9734608ffcefcca917137ce9480cc91a4fbd321a4aca3160
BLAKE2b-256 checksum
How to use checksums
80bbd9b6991f59b7542ed6de9837e9160af066f6f47fb9515a412f0311315d2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / stups_tokens-1.1.19-py3-none-any.whl

Download URL stups_tokens-1.1.19-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
317f4386763bac9dd5c0a4c8b0f9f0238dc3fa81de3c6fd1971b6b01662b5750
BLAKE2b-256 checksum
How to use checksums
70dd4fd77cb049b7f1e7da1abc857a8638e07dc67224acf9ebde1f02e33167ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for stups-tokens 1.1.19

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page