Skip to main content

PYTHON module to generate and rotate token

Project description

token-yielder

Description

This module enables developers to seamlessly rotate Bearer token has a backend task and helps saving time taken to frame logic. It's designed dynamically so user can customize required parameters in runtime. In addition to that the generated token is stored in environment which makes it ready to use production grade application.

Features

  • Seamless token rotation.
  • Configuring environment with new or updated tokens.
  • Options to modify metadata in runtime.
  • Single time token generation.
  • Readable error object.
  • Easy to integrate with API systems.

Installation

Install using pip:

pip install token_yielder

Usage

Initialization

Here, timeout is an optional argument; by default it's 5 seconds

from token-yielder.connect import CreateRequestTask
import asyncio

base_url = 'https://auth-admin.net'
router = '/v2/generate-token'
consumer_id = '123456hui'
consumer_secret = 'gythbsghui5hj789'
timeout = 10
object = CreateRequestTask(base_url, router, consumer_id, consumer_secret, timeout)

Periodic token rotation

It doesn't require any mandatory arguments and a default value is configured.

  • variable_name='Token'; generated token will be stored under this name in env.
  • interval for token rotation is set to 10 hours.
  • token_name='access_token'; function will look for this key in response object to capture the respective value which would be the generated token. This can be customized by user as per their response object.
# run with custom values
import asyncio

variable_name='Token'
interval=36000
token_name='access_token'

asyncio.run(object.rotate_function(variable_name,interval,token_name))
# run with default values
import asyncio

asyncio.run(object.rotate_function())

Single time token generation

Since a default value is configured, It doesn't require any mandatory arguments

  • variable_name='Token'; generated token will be stored under this name in env.
  • store: boolean is set to False which means, generated token will not be stored in env rather it ll be returned.
  • token_name='access_token'; function will look for this key in response object to capture the respective value which would be the generated token. This can be customized by user as per their response object.
# generating and storing token in env
variable_name='Token'
store=True
token_name='access_token'

object.generate_token(variable_name,store,token_name)
# generating token
generated_token = object.generate_token()

Enhanced error object

Module comes up with enhanced error object. It captures unsuccessful response and creates custom error object in readable way which will enable developers to integrat with their main system with ease.

try:
    generated_token = object.generate_token()
except Exception as arg:
    print(arg.status) # gives status code of request
    print(arg.details) # gives details of the exception
Sample error response:
>> status code: 405; content: {"detail":"Method Not Allowed"}

Unhandled exception from client end will be dumped as given below:
>> status code: 422; content: {"detail":[{"type":"int_parsing","loc":["body","client_secret"],"msg":"Input should be a valid integer, unable to parse string as an integer","input":"gsd4826hjsdksdn"}]}

This can be parsed to obtained required info.

It also comes up with mechanism to handle unexpected errors such as connection refused. Those unexpected errors are logged as 400 error.

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

token_yielder-1.0.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

token_yielder-1.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file token_yielder-1.0.0.tar.gz.

File metadata

  • Download URL: token_yielder-1.0.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.10

File hashes

Hashes for token_yielder-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b2baebd4547b6edde8b62f5a6a182eb464e1bebe2a1852ddd7ed02d5e69de735
MD5 872b15dbb61a18ddc6a3573270ef793d
BLAKE2b-256 7ed9d9e7befcb269a8525b0ba5b940e7704412c577c08bf8c827a9bcfcf13207

See more details on using hashes here.

File details

Details for the file token_yielder-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: token_yielder-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.10

File hashes

Hashes for token_yielder-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f6bebf75915c2fd0d6da823e923abca1f482d3d6ea79866a8a3ced3756e7f23
MD5 37e31096967febca72e1572e255ce533
BLAKE2b-256 40604ee609c56d6e9a0011aa665b37ea5bf8d59bc544d2b49f34128741eafbad

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