← back to Designer Wallcoverings

DW-Agents/dw-agents/cleanup-auth.py

108 lines

#!/usr/bin/env python3

import re
import os

agents = [
    "digital-samples-agent.ts",
    "in-parallel-agent.ts",
    "marketing-agent.ts",
    "needs-attention-agent.ts",
    "new-client-signup-agent.ts",
    "parallel-processes-agent.ts",
    "purchasing-office-agent.ts",
    "server-uptime-agent.ts",
    "task-orchestrator-agent.ts",
    "todays-highlights-agent.ts",
    "trend-research-agent.ts",
    "zendesk-chat-agent.ts"
]

def clean_agent_file(filepath):
    """Clean up old auth code from agent file"""

    with open(filepath, 'r') as f:
        content = f.read()

    original_content = content

    # Remove old requireAuth function definitions
    # Pattern 1: const requireAuth = ... };
    pattern1 = r'const requireAuth\s*=\s*\([^)]*\)\s*=>\s*\{[^}]*(?:\{[^}]*\}[^}]*)*\};?'
    content = re.sub(pattern1, '', content, flags=re.DOTALL)

    # Pattern 2: function requireAuth...
    pattern2 = r'function requireAuth\s*\([^)]*\)\s*\{[^}]*(?:\{[^}]*\}[^}]*)*\}'
    content = re.sub(pattern2, '', content, flags=re.DOTALL)

    # Remove login GET route
    pattern3 = r"app\.get\('/login'[^;]*\{(?:[^{}]|\{[^}]*\})*\}\);?"
    content = re.sub(pattern3, '', content, flags=re.DOTALL)

    # Remove login POST route
    pattern4 = r"app\.post\('/login'[^;]*\{(?:[^{}]|\{[^}]*\})*\}\);?"
    content = re.sub(pattern4, '', content, flags=re.DOTALL)

    # Remove logout route
    pattern5 = r"app\.(get|post)\('/logout'[^;]*\{[^}]*\}\);?"
    content = re.sub(pattern5, '', content, flags=re.DOTALL)

    # Remove requireAuth from route definitions
    content = re.sub(r',\s*requireAuth,', ',', content)
    content = re.sub(r'requireAuth,\s*', '', content)

    # Remove AUTH_USERNAME and AUTH_PASSWORD constants
    content = re.sub(r"const AUTH_USERNAME\s*=\s*[^;]+;", '', content)
    content = re.sub(r"const AUTH_PASSWORD\s*=\s*[^;]+;", '', content)

    # Replace SSO token headers in API calls
    # Pattern: headers: { 'Cookie': `${SSO_TOKEN_NAME}=${SSO_TOKEN_VALUE}` }
    old_header = r"headers:\s*\{\s*'Cookie':\s*`\$\{SSO_TOKEN_NAME\}=\$\{SSO_TOKEN_VALUE\}`\s*\}"
    new_header = "headers: { 'Cookie': req.cookies?.dw_session ? `dw_session=${req.cookies.dw_session}` : '' }"
    content = re.sub(old_header, new_header, content)

    # Clean up multiple empty lines
    content = re.sub(r'\n\s*\n\s*\n+', '\n\n', content)

    if content != original_content:
        with open(filepath, 'w') as f:
            f.write(content)
        return True
    return False

def main():
    print("Starting cleanup of old authentication code...")
    print("=" * 50)

    updated_count = 0
    error_count = 0

    for agent in agents:
        filepath = f"/root/DW-Agents/{agent}"

        print(f"\nProcessing: {agent}")
        print("-" * 20)

        if not os.path.exists(filepath):
            print(f"❌ File not found: {agent}")
            error_count += 1
            continue

        try:
            if clean_agent_file(filepath):
                print(f"✅ Cleaned up: {agent}")
                updated_count += 1
            else:
                print(f"ℹ️ No changes needed: {agent}")
        except Exception as e:
            print(f"❌ Error processing {agent}: {str(e)}")
            error_count += 1

    print("\n" + "=" * 50)
    print(f"Cleanup complete!")
    print(f"✅ Updated: {updated_count} files")
    if error_count > 0:
        print(f"❌ Errors: {error_count} files")

if __name__ == "__main__":
    main()