Skip to main content

This library will create and validate JWT tokens and decode and validate queries against a pydantic model in input. In order to work succesfully, it needs that you have defined the following environment variables: 'JWT_SECRET' your secret key for generating tokens,'JWT_ALGORITHM' your favorite algorithm to be use to generate the tokens

Project description

Description of the Encryption Module

This module provides essential functionalities for secure authentication and password management in Python applications, particularly useful in web services developed with FastAPI. It is designed with a focus on security, efficiency, and ease of use.

Features

JWT Token Management

  • create_jwt_token(payload: dict): Creates a JWT (JSON Web Token) with a custom payload, typically including user details like ID and role. The token is encrypted using a secret key and an algorithm specified in the environment variables. It has a default expiration of 7 days.
  • check_token(token: str): Validates the given JWT token, verifying its integrity and authenticity. Returns True if the token is valid, otherwise False.
  • read_token_from_header(request: Request): Extracts and validates a JWT token from the HTTP request header. If the token is invalid or expired, it raises an HTTPException with appropriate status codes.
  • read_token(token: str): Validates a given string token. Raises an HTTPException if the token is invalid or expired, otherwise, returns the token's payload.

Password Encryption and Verification

  • encrypt_password(password: str): Encrypts a plain password using bcrypt, providing a secure way to store passwords.
  • check_password(plain_password: str, hashed_password: str): Verifies a plain password against its hashed version, typically used during the login process.

This module is particularly suitable for web applications requiring robust authentication mechanisms and secure password handling.

Description of the Query Module

This module is designed to handle and validate query strings in Python applications, especially in the context of web APIs developed using FastAPI and Pydantic. It focuses on flexibility and reliability in processing and validating query strings.

Features

Query String Decoding

  • decode_query(query: str): Decodes a query string into a dictionary. It can handle both URL-encoded JSON strings and standard query strings. For JSON strings, it parses them into a dictionary, and for standard query strings, it processes them as key-value pairs.

Query Validation

  • decode_and_validate_query(query: str, validation_model: BaseModel): Decodes the given query string and then validates it against a specified Pydantic model. It ensures that the query matches the expected schema and data types.
  • validate_query_over_schema(validation_model: BaseModel, query: dict): Validates a dictionary (typically the result of a decoded query) against a Pydantic model. It checks if all fields in the query are contained in the model and match the expected types. If the query is incorrect, it raises an HTTPException with details of the discrepancy.

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

api_service-0.0.4.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

api_service-0.0.4-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file api_service-0.0.4.tar.gz.

File metadata

  • Download URL: api_service-0.0.4.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for api_service-0.0.4.tar.gz
Algorithm Hash digest
SHA256 80308928b2c611d642ae26bbe92982c1e5924730227dba5c29c8d901b7b13b89
MD5 e56d1de44c26795895a8fbba30f4b6b8
BLAKE2b-256 e6725fff914c07dbef381ec0fc8d6132fdf1a308673296986a68232d17aeb4d2

See more details on using hashes here.

File details

Details for the file api_service-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: api_service-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for api_service-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 571d7edf202f4725d75391fa1481f099acf7c2d855e11fe38a904300f2d88e74
MD5 df05029c852b4df0feec32ad28c3191e
BLAKE2b-256 979b4a1caa2bb0462f83fa9278c9256e3936de252d0c142e1926af5b36994ceb

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