# Create a debug log to confirm script execution import os import sys import django LOG_PATH = os.environ.get("AUTH_DEBUG_LOG", "/tmp/auth_debug.log") def log(data): try: with open(LOG_PATH, "a") as f: f.write(f"{data}\n") except Exception: pass # Set up Django environment os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") # Adjust if needed django.setup() from django.contrib.auth import authenticate # noqa: E402 from django.contrib.auth.models import User # noqa: E402 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("Authentication success") log("Sent 1") print("1", flush=True) # Success else: log("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()