Skip to main content

A Python implementation of JSON Web Tokens (JWT) for authentication and authorization using a symmetric secret key and HMAC-SHA256.

Project description

JWT Authentication

A Python implementation of JSON Web Tokens (JWT) for authentication and authorization using a symmetric secret key and HMAC-SHA256.

Changelog

0.1.2

  • updated timezone awareness

0.1.0

  • does not return the 'user_id' as additional argument anymore.
  • 'user_id' is now added to the request object.
  • changed 404 to 401 if token is missing and updated response messages.

0.0.20

  • returns JsonResponse with {'message': "string"} and the status-code

Features

  • Generate JWT tokens with customizable expiration times
  • Validate JWT tokens and verify their integrity using a symmetric secret key and HMAC-SHA256
  • Extract user information (such as user ID 'sub') from JWT tokens

Installation

You can install the package via pip:

pip install ft_jwt

Usage

from ft_jwt.ft_jwt import FT_JWT

# Create a JWT instance with your secret key

secret_key = 'your_secret_key'
ftjwt = FT_JWT(secret_key)

# Generate a token for a user

sub = '1'
token = jwt.createToken(sub)

# Validate a token

is_valid, message = ftjwt.validateToken(token)
if is_valid:
    print('Token is valid')
else:
    print(f'Token is invalid: {message}')

# Get the user ID from a token

user_id = ftjwt.getUserId(token)
print(f'User ID: {sub}')

# Use the decorator to validate against a JWT for a protected route

@ftjwt.token_required
def protected_route():
    # This function will be executed only if the token is valid
    # ...

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

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

ft_jwt-0.1.2.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file ft_jwt-0.1.2.tar.gz.

File metadata

  • Download URL: ft_jwt-0.1.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for ft_jwt-0.1.2.tar.gz
Algorithm Hash digest
SHA256 37c804e36d676b2c04308a4b16ea2b116c88fc3194afd46a24a9f98324906e76
MD5 afdd2fc9e61a97919f456ad216f86190
BLAKE2b-256 63b4ca7273317809eb9d2c0e28fcf369f807b612f182fcd99dbfbe823bd72ae3

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