An authentication system with one-time login links and templated email sending
Project description
OTC Auth System
This OTC Auth system is a Python package that provides a simple authentication mechanism using one-time login links. Users receive an email containing a unique login link, generated by the package, which allows them to authenticate without a password. This package is intended to be integrated into larger projects, providing reusable authentication functionality.
Installation
To install the package, clone the repository or download the source code. Navigate to the root directory of the package and run:
pip3 install TT_auth_system
This command installs the package and its dependencies.
Setup and Configuration
Environment Variables
The package relies on several environment variables to configure its behavior, particularly for SMTP settings, database access, and the base URL of the login link. These should be set in a .env file in the root of the main project using the package.
Create a .env file with the following variables:
• SMTP_SERVER: The address of the SMTP server used to send emails (e.g., smtp.example.com).
• SMTP_PORT: The port number for the SMTP server (commonly 587 for TLS or 465 for SSL).
• SMTP_USERNAME: The username for SMTP authentication.
• SMTP_PASSWORD: The password for SMTP authentication.
• LOGIN_LINK_BASE_URL: The base URL for the login link sent to users. This should point to the part of your application that handles login (e.g., https://yourapp.com/login).
• DB_HOST: The database server host.
• DB_PORT: The database server port (commonly 3306 for MySQL).
• DB_USER: The username for the database.
• DB_PASSWORD: The password for the database.
• DB_NAME: The name of the database.
• DB_TABLE: The name of the table containing user data.
Example .env file:
SMTP_SERVER=smtp.example.com SMTP_PORT=587 SMTP_USERNAME=your_smtp_username SMTP_PASSWORD=your_smtp_password LOGIN_LINK_BASE_URL=https://yourapp.com/login DB_HOST=your_db_host DB_PORT=3306 DB_USER=your_db_user DB_PASSWORD=your_db_password DB_NAME=your_db_name DB_TABLE=your_table_name
HTML Email Template
The package sends an HTML email containing the login link. The HTML template file should be placed in the templates directory at the root of the main project. The file should be named otc_email.html.
Directory Structure: main_project/ ├── .env ├── templates/ │ └── otc_email.html ├── main_script.py
Example otc_email.html:
Hello, {{ first_name }},
Click the link below to log in:
This link is valid for one-time use only.
Best regards,
Your App Team
The {{ login_link }} placeholder will be replaced with the actual login link when the email is sent.
Usage
To use the package in your project, import the necessary functions and configure your environment:
from TT_auth_system import generate_and_send_email, validate_code, purge_valid_codes
Generate a one-time code and send a login email if the email exists in the database
code = generate_and_send_email("user@example.com")
Validate a code (returns the associated email if valid, None otherwise)
is_valid = validate_code(code)
Purge all valid codes from memory
purge_valid_codes()
Dependencies
The package depends on the following Python libraries:
• python-dotenv: For loading environment variables from a .env file.
• jinja2: For rendering HTML templates.
• pymysql: For connecting to a MySQL database.
• smtplib: Part of Python’s standard library, used for sending emails via SMTP.
These dependencies are automatically installed when you install the package using pip.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file TT_auth_system-1.0.0.tar.gz.
File metadata
- Download URL: TT_auth_system-1.0.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89a45bd75babee9705d8307bb4194f9701ff7ddd36e3d31ac84f775ffb084e93
|
|
| MD5 |
e7baa3d2945e9b0756b36ce2872719df
|
|
| BLAKE2b-256 |
e478eb57446a1b42a396697a2027a283fa8346cf4005cf99696b91fe13edf552
|
File details
Details for the file TT_auth_system-1.0.0-py3-none-any.whl.
File metadata
- Download URL: TT_auth_system-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7fbdbd886d86589181a9f303dfde099dc3022d054e6fe46021099d62760e61b
|
|
| MD5 |
30a6232d69a466dd0a77cbfefceec212
|
|
| BLAKE2b-256 |
43021c1a0883bcb6299fc523f25197498833c6ebecfa8c1ece52c6df4c757475
|