Skip to main content

Create healthy and secure authentication tokens with UTokeniz.

Project description

UToken - Secure tokens.

BADGE

UToken (or Unhandleable Token) is a library created to be used in the generation of safe and sound tokens, that is, not can be changed. Here's what you can do with UToken:

  • Create secure tokens
  • Insert a content in the token
  • Set expiration time for token

Shortcuts

Installation

To install UToken, use the pip package manager:

pip install utokeniz

How to use

Here's a short tutorial on how to use UToken in a simple way.

Creating a token

Let's start by creating a token, see the code below:

from utoken import encode

# defining our key
KEY = 'secret-key'

# encoding
my_token = encode({'message': 'Firlast'}, KEY)
print(my_token)

# > eyJtZXNzYWdlIjogIkZpcmxhc3QifQ.5c99ae8e7ce3a000d5b0c35cb53e9e8f

First we pass as a parameter to utoken.encode() the content of the token, which can be a dictionary or list, then we pass the key that will be used to encrypt. After that, we have our token.

We can also add the token expiration time using the max-time key in our dictionary, see:

from utoken import encode
from datetime import datetime, timedelta

max_time = datetime.now() + timedelta(minutes=5)

# encoding
my_token = encode({'message': 'Firlast', 'max-time': max_time}, 'KEY')

After the maximum time is reached, the ExpiredTokenError exception will be thrown.

Decoding a token

Now, let's decode a token. See the code below:

from utoken import decode

# defining our key
KEY = 'secret-key'
token = 'eyJtZXNz...'

# decoding
my_decode_token = decode(token, KEY)
print(my_decode_token)

# > {'message': 'Firlast'}

Ready! Our token has been decoded. In utoken.decode() we pass as a parameter the token and the key used in the encoding, simple.

If you set an expiration time on the token, you will get an exception when trying to decode the token if the token is expired, for that, do an exception handling:

from utoken import decode
from utoken import ExpiredTokenError

# defining our key
KEY = 'secret-key'
token = 'eyJtZXNz...'

# decoding
try:
    my_decode_token = decode(token, KEY)
except ExpiredTokenError:
    print('Token has expired')
else:
    print(my_decode_token)

License

GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. https://fsf.org/ Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.

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

utokeniz-1.1.1.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file utokeniz-1.1.1.tar.gz.

File metadata

  • Download URL: utokeniz-1.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for utokeniz-1.1.1.tar.gz
Algorithm Hash digest
SHA256 8b4fd321cf7c2cc9830644be5892b03a046189465fe9a6895fddf8d47d8ee569
MD5 579825807cbe8ee4a42430cc000ca110
BLAKE2b-256 2b2186ffe36676a10471e7acdda3371b1b9fb340179d8877000242582dcbe8ea

See more details on using hashes here.

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