81 lines
2.4 KiB
Python
Executable file
81 lines
2.4 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""
|
|
Admin Password Reset Utility
|
|
|
|
This script helps reset the admin password in the .env file.
|
|
Run with: python reset_admin_password.py [new_password]
|
|
If no password provided, a random secure password will be generated.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import random
|
|
import string
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
def generate_secure_password(length=12):
|
|
"""Generate a secure random password."""
|
|
chars = string.ascii_letters + string.digits + "!@#$%^&*"
|
|
return ''.join(random.choice(chars) for _ in range(length))
|
|
|
|
def update_env_file(new_password):
|
|
"""Update the ADMIN_PASSWORD in .env file."""
|
|
env_file = Path(".env")
|
|
|
|
if not env_file.exists():
|
|
print("❌ .env file not found!")
|
|
print("Create one from .env.example: cp .env.example .env")
|
|
sys.exit(1)
|
|
|
|
# Read current content
|
|
with open(env_file, 'r') as f:
|
|
lines = f.readlines()
|
|
|
|
# Update ADMIN_PASSWORD line
|
|
updated = False
|
|
new_lines = []
|
|
for line in lines:
|
|
if line.startswith("ADMIN_PASSWORD="):
|
|
new_lines.append(f"ADMIN_PASSWORD={new_password}\n")
|
|
updated = True
|
|
else:
|
|
new_lines.append(line)
|
|
|
|
# Write back
|
|
with open(env_file, 'w') as f:
|
|
f.writelines(new_lines)
|
|
|
|
if updated:
|
|
print(f"✅ Password updated in .env file")
|
|
return True
|
|
else:
|
|
print("❌ ADMIN_PASSWORD line not found in .env file")
|
|
return False
|
|
|
|
def main():
|
|
# Get new password from command line or generate
|
|
if len(sys.argv) > 1:
|
|
new_password = sys.argv[1]
|
|
print(f"🔐 Using provided password")
|
|
else:
|
|
new_password = generate_secure_password()
|
|
print(f"🔐 Generated secure password: {new_password}")
|
|
|
|
# Update .env file
|
|
if update_env_file(new_password):
|
|
print("\n📋 Next steps:")
|
|
print(f"1. New password: {new_password}")
|
|
print("2. Restart the server for changes to take effect")
|
|
print("3. Log out and log back in if currently authenticated")
|
|
|
|
# Offer to restart if server is running
|
|
print("\n💡 To restart:")
|
|
print(" If using 'python run.py': Ctrl+C and restart")
|
|
print(" If using 'uvicorn app:app --reload': It will auto-restart")
|
|
|
|
# Show current settings
|
|
print("\n📄 Current .env location:", Path(".env").resolve())
|
|
|
|
if __name__ == "__main__":
|
|
main()
|