Skip to main content

Python library to manage OAuth access tokens

Project description

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>

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

stups-tokens-1.0.19.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

stups_tokens-1.0.19-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file stups-tokens-1.0.19.tar.gz.

File metadata

File hashes

Hashes for stups-tokens-1.0.19.tar.gz
Algorithm Hash digest
SHA256 61a0c34931312fed6cad3e80ace5ac1480434b78d482baffbdf1e88ad3eb4ce2
MD5 ff9cef887560f64c514b9ecf5af7961c
BLAKE2b-256 2abddf8fcb2d25c6c80750a83d0f3a41486591dac8f6ebfb12e4c18662a4513f

See more details on using hashes here.

Provenance

File details

Details for the file stups_tokens-1.0.19-py3-none-any.whl.

File metadata

File hashes

Hashes for stups_tokens-1.0.19-py3-none-any.whl
Algorithm Hash digest
SHA256 3fcd8bc3d2643f1165fcb4eaa48516c4499b3d83b228f4a3b6f5ef29d80bf56b
MD5 c098c2f57657a70942b11a36f98986c7
BLAKE2b-256 881794eb1f8c685a0c5ee705bb49441100db2ccc1cf5e3e99a1f6e18876243f2

See more details on using hashes here.

Provenance

Supported by

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