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.1.tar.gz (6.9 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.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: token_yielder-1.0.1.tar.gz
  • Upload date:
  • Size: 6.9 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.1.tar.gz
Algorithm Hash digest
SHA256 e3c4721eae0ddc3d43673b2ec5324e19f0c8767b38b3ac46af9a07b4e2265cda
MD5 66145fb588274c439b2cf12a55a9ae5d
BLAKE2b-256 683366612f1df1c3f31cca2837138c298baa6c08880a88141890bb0c1da228c7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: token_yielder-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bae379ec41bfc4d88bbf4a009db0056fbf5b2fb617a359dec918e7e01c282d93
MD5 bf50798540cf1a8dc79d324bf4c7557b
BLAKE2b-256 3f29dd1a4014499222e69d6044725d28245f31fa8fcc884140338939e7775255

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