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 or use provided 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:

```python
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 (UserPayload):

from datetime import datetime

class UserPayload:
    def __init__(self, user_id, username, request_id=None):
        self.user_id = user_id
        self.username = username
        self.request_id = request_id  # Optional request ID for tracing

# 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 using the InternalTokenService
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 using the InternalTokenService for dictionaries
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. This method takes two parameters:

  • secret: The secret key used to decode and validate the JWT.
  • token: The JWT token to validate and decode.

Example:

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, which can be useful for tracing requests across systems.

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

Example Workflow

from datetime import datetime

# Define a user payload
class UserPayload:
    def __init__(self, user_id, username, request_id=None):
        self.user_id = user_id
        self.username = username
        self.request_id = request_id

# Initialize payload data
payload = UserPayload(user_id=1, username="john_doe")
secret_key = "supersecret"
expiration_time = 600  # Token expires in 10 minutes

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

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

Methods

InternalTokenService.generate_internal_token(secret, expiration_time_in_seconds, payload_obj)

Generates a JWT token based on the provided secret key, expiration time, and payload object.

  • secret: The secret key used for signing the token (string).
  • expiration_time_in_seconds: The expiration time for the token, in seconds (int).
  • payload_obj: A Python object (e.g., UserPayload) containing the data to be encoded.

Returns the encoded JWT token as a string.

InternalTokenService.generate_internal_token_for_dict(secret, expiration_time_in_seconds, payload)

Generates a JWT token based on the provided secret key, expiration time, and dictionary payload.

  • secret: The secret key used for signing the token (string).
  • expiration_time_in_seconds: The expiration time for the token, in seconds (int).
  • payload: A dictionary containing the data to be encoded.

Returns the encoded JWT token as a string.

InternalTokenService.validate_token(secret, token)

Validates a JWT token and decodes its payload.

  • secret: The secret key used for signing the token (string).
  • token: The JWT token to decode and validate (string).

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

InternalTokenService.generate_trace_id()

Generates a unique request ID, typically used for tracing requests across systems.

Returns a string representing the generated UUID.

JwtUtil.generate_internal_token(secret, expiration_time_in_seconds, payload)

Generates a JWT token by encoding the payload object (either as a dictionary or a Python object) with the provided secret and expiration time. This method is used internally by InternalTokenService.

JwtUtil.validate_internal_token(secret, token)

Validates a JWT token and decodes the payload. This method is used internally by InternalTokenService.

Error Handling

  • Token Expiry: If the token is expired, a ValueError with the message "Token has expired" is raised.
  • Invalid Token: If the token is invalid, a ValueError with the message "Invalid token" is raised.
  • Other Errors: Any other errors during validation or token generation will raise a ValueError with a relevant error message.

Dependencies

This package requires the following external dependencies:

  • PyJWT for handling JWT encoding and decoding:

    pip install pyjwt
    
  • uuid and datetime are part of Python's standard library.

License

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


This package provides an easy-to-use utility for securely generating and validating JWT tokens, with added support for traceability using unique request IDs and flexible payload handling (object or dictionary).

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.3.tar.gz (4.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.3-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: internal_token_service-0.3.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.6

File hashes

Hashes for internal_token_service-0.3.tar.gz
Algorithm Hash digest
SHA256 adf73ff1deedc4702868af48761c19db532eb7a88bc8530ca447f91f9cca3bb1
MD5 7910e53991125089a5e708f1a9fde47d
BLAKE2b-256 ba7d97ca54be5213c33787b72a2296ef897cb43a37080e7b22c93478c7efd51e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for internal_token_service-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 015a62499bc5f57e926b6c3f912377894825cf902bfa1a744930498ffb5acbe1
MD5 297878109394d4a120581b295a15498c
BLAKE2b-256 ed5f6c9cdadf498c9d7a6d32d21f53f8883b35f5a8a35f8a98fdc55a21e40053

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