Files
GIA/auth_django.py

75 lines
2.3 KiB
Python
Executable File

# Create a debug log to confirm script execution
import sys
import django
import os
LOG_PATH = "auth_debug.log"
def log(data):
with open(LOG_PATH, "a") as f:
f.write(f"{data}\n")
# Set up Django environment
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") # Adjust if needed
django.setup()
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
def check_credentials(username, password):
"""Authenticate user via Django"""
user = authenticate(username=username, password=password)
return user is not None and user.is_active
def main():
"""Process authentication requests from Prosody"""
while True:
try:
# Read a single line from stdin
line = sys.stdin.readline().strip()
if not line:
break # Exit if input is empty (EOF)
# Log received command (for debugging)
# log(f"Received: {line}")
parts = line.split(":")
if len(parts) < 3:
log("Sending 0")
print("0", flush=True) # Invalid format, return failure
continue
command, username, domain = parts[:3]
password = ":".join(parts[3:]) if len(parts) > 3 else None # Reconstruct password
if command == "auth":
if password and check_credentials(username, password):
log(f"Authentication success")
log("Sent 1")
print("1", flush=True) # Success
else:
log(f"Authentication failure")
log("Sent 0")
print("0", flush=True) # Failure
elif command == "isuser":
if User.objects.filter(username=username).exists():
print("1", flush=True) # User exists
else:
print("0", flush=True) # User does not exist
elif command == "setpass":
print("0", flush=True) # Not supported
else:
print("0", flush=True) # Unknown command, return failure
except Exception as e:
# Log any unexpected errors
log(f"Error: {str(e)}\n")
print("0", flush=True) # Return failure for any error
if __name__ == "__main__":
main()