|
| 1 | +""" |
| 2 | +Tool to add new users to the chatbot database with authentication tokens. |
| 3 | +Handles user creation and token generation with secure password hashing. |
| 4 | +""" |
| 5 | +import uuid |
| 6 | +import getpass |
| 7 | +import sys |
| 8 | +from datetime import datetime, timedelta, UTC |
| 9 | + |
| 10 | +import bcrypt |
| 11 | +from sqlalchemy import create_engine, Column, String, TIMESTAMP, Table, MetaData, select, exc |
| 12 | +from sqlalchemy.dialects.postgresql import UUID |
| 13 | + |
| 14 | +def main(): |
| 15 | + """Entry point for chatbot_db module.""" |
| 16 | + database_url = input("Enter your DATABASE URL " |
| 17 | + "(e.g. postgresql://user:pass@host:port/db): ").strip() |
| 18 | + username = input("Enter username: ").strip() |
| 19 | + email = input("Enter email: ").strip() |
| 20 | + password = getpass.getpass("Enter password (will be hashed): ") |
| 21 | + password_hash = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8") |
| 22 | + engine = create_engine(database_url) |
| 23 | + metadata = MetaData() |
| 24 | + users = Table( |
| 25 | + "users", metadata, |
| 26 | + Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4), |
| 27 | + Column("username", String(150), nullable=False, unique=True), |
| 28 | + Column("password_hash", String(255), nullable=False), |
| 29 | + Column("email", String(150), nullable=False) |
| 30 | + ) |
| 31 | + tokens = Table( |
| 32 | + "tokens", metadata, |
| 33 | + Column("token", String(64), primary_key=True), |
| 34 | + Column("username", String(50)), |
| 35 | + Column("created_at", TIMESTAMP, default=datetime.now(UTC)), |
| 36 | + Column("expires_at", TIMESTAMP, nullable=False) |
| 37 | + ) |
| 38 | + metadata.create_all(engine) |
| 39 | + with engine.connect() as conn: |
| 40 | + user_exists = conn.execute( |
| 41 | + select(users.c.username).where(users.c.username == username) |
| 42 | + ).fetchone() |
| 43 | + |
| 44 | + if user_exists: |
| 45 | + print(f"Error: User '{username}' already exists. Please choose a different username.") |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + try: |
| 49 | + with engine.begin() as conn: |
| 50 | + user_id = uuid.uuid4() |
| 51 | + conn.execute(users.insert().values( |
| 52 | + id=user_id, |
| 53 | + username=username, |
| 54 | + password_hash=password_hash, |
| 55 | + email=email |
| 56 | + )) |
| 57 | + |
| 58 | + token_value = uuid.uuid4().hex |
| 59 | + conn.execute(tokens.insert().values( |
| 60 | + token=token_value, |
| 61 | + username=username, |
| 62 | + created_at=datetime.now(UTC), |
| 63 | + expires_at=datetime.now(UTC) + timedelta(days=30) |
| 64 | + )) |
| 65 | + |
| 66 | + print(f"\n User '{username}' created with token: {token_value}") |
| 67 | + except exc.IntegrityError as e: |
| 68 | + print("Error: Database integrity error occurred. User may already exist or " |
| 69 | + "there's a constraint violation.") |
| 70 | + print(f"Details: {str(e)}") |
| 71 | + sys.exit(1) |
| 72 | + except (ConnectionError, TimeoutError) as e: |
| 73 | + print(f"Error: An unexpected error occurred: {str(e)}") |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + main() |
0 commit comments