Skip to main content

No project description provided

Project description

InternalTokenService - Python Package

Overview

The InternalTokenService is a Python package that helps with generating and validating JWT tokens. It leverages the HMAC-SHA256 algorithm for securing tokens and includes features for expiration, payload management, and traceability via request IDs.

This package is suitable for use in microservices, secure API communications, or any scenario where short-lived tokens need to be generated, validated, and tracked.

Features

  • JWT Token Generation: Securely generate JWT tokens from Python objects or dictionaries.
  • Token Validation: Validate JWT tokens to ensure their authenticity and integrity.
  • Traceability: Automatically generate unique request IDs for tracing requests.
  • Expiration Handling: Easily handle token expiration with customizable expiration times.

Installation

You can install the package using pip from the source directory:

pip install .

Usage

1. Importing the Class

Import the InternalTokenService class into your Python project:

from internal_token_service import InternalTokenService

2. Generating a Token

There are two methods to generate a JWT token:

  • Using a custom Python object (e.g., user data class).
  • Using a dictionary (if you already have your payload as a dictionary).

Example with Custom Python Object:

class UserPayload:
    def __init__(self, user_id, username):
        self.user_id = user_id
        self.username = username

# Initialize payload data
payload = UserPayload(user_id=1, username="john_doe")
secret_key = "my_secret_key"
expiration_time = 3600  # Token expires in 1 hour

# Generate the token
token = InternalTokenService.generate_internal_token(secret_key, expiration_time, payload)
print("Generated Token:", token)

Example with Dictionary Payload:

payload = {
    "user_id": 1,
    "username": "john_doe"
}
secret_key = "my_secret_key"
expiration_time = 3600  # Token expires in 1 hour

# Generate the token
token = InternalTokenService.generate_internal_token_for_dict(secret_key, expiration_time, payload)
print("Generated Token:", token)

3. Validating a Token

To validate a JWT token, use the validate_token method:

try:
    decoded_data = InternalTokenService.validate_token(secret_key, token)
    print("Decoded Token:", decoded_data)
except ValueError as e:
    print("Error:", e)

4. Generating a Request ID

The generate_trace_id method generates a unique request ID:

request_id = InternalTokenService.generate_trace_id()
print("Generated Request ID:", request_id)

Methods

InternalTokenService.generate_internal_token(secret, expiration_time_in_seconds, payload_obj)

Generates a JWT token with a payload object.

  • secret: Secret key for signing the token.
  • expiration_time_in_seconds: Expiration time in seconds.
  • payload_obj: A Python object containing the data to be encoded.

Returns an encoded JWT token as a string.

InternalTokenService.generate_internal_token_for_dict(secret, expiration_time_in_seconds, payload)

Generates a JWT token with a dictionary payload.

  • secret: Secret key for signing the token.
  • expiration_time_in_seconds: Expiration time in seconds.
  • payload: Dictionary containing the data to be encoded.

Returns an encoded JWT token as a string.

InternalTokenService.validate_token(secret, token)

Validates a JWT token and decodes its payload.

  • secret: Secret key used for signing the token.
  • token: JWT token to decode and validate.

Returns the decoded payload as a dictionary or raises ValueError if invalid.

InternalTokenService.generate_trace_id()

Generates a unique request ID for tracing.

Returns a string representing the generated UUID.

Error Handling

  • Token Expiry: Raises ValueError("Token has expired") if the token is expired.
  • Invalid Token: Raises ValueError("Invalid token") if the token is invalid.
  • Other Errors: Raises ValueError with an appropriate message for any other error.

Dependencies

This package requires:

  • PyJWT for handling JWT encoding and decoding:
    pip install pyjwt
    
  • uuid and datetime (part of Python's standard library).

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

internal_token_service-0.5.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

internal_token_service-0.5-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file internal_token_service-0.5.tar.gz.

File metadata

  • Download URL: internal_token_service-0.5.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.9

File hashes

Hashes for internal_token_service-0.5.tar.gz
Algorithm Hash digest
SHA256 ba2405984ed0ada5abbc88fb6eb2fd0e038589bfb74b1d79cb39e30518909a46
MD5 2e0b32a6b521ad35d01ba40261552beb
BLAKE2b-256 2b46d79a384470dae739c65206bbb65b1be3ff4722a83e04f700edc805a239d5

See more details on using hashes here.

File details

Details for the file internal_token_service-0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for internal_token_service-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 880f713f2fd2128d74585bf1e4118fcc82fec83b914f2923cb2f0a2f3d973e84
MD5 7f346f1959f0983c629c9544a36b60b7
BLAKE2b-256 015c339bfd7bab017b253d77ca5ae6dd063937b145aa432ebae9d337f287d4b8

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