105 lines
4.0 KiB
Python
105 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify Microsoft Entra ID OAuth configuration
|
|
Run this to check if your OAuth settings are correct before deploying.
|
|
"""
|
|
|
|
import os
|
|
import requests
|
|
import subprocess
|
|
from authlib.integrations.requests_client import OAuth2Session
|
|
|
|
def get_systemd_env_var(var_name):
|
|
"""Get environment variable from systemd service"""
|
|
try:
|
|
result = subprocess.run(
|
|
['systemctl', 'show', 'pdf-renamer', '-p', f'Environment'],
|
|
capture_output=True, text=True, check=True
|
|
)
|
|
env_lines = result.stdout.strip().split('\n')
|
|
for line in env_lines:
|
|
if f'{var_name}=' in line:
|
|
# Extract the value after the equals sign
|
|
return line.split('=', 1)[1].strip('"')
|
|
return None
|
|
except subprocess.CalledProcessError:
|
|
return None
|
|
|
|
def test_oauth_config():
|
|
# Get configuration from systemd service environment
|
|
tenant_id = get_systemd_env_var('TENANT_ID')
|
|
client_id = get_systemd_env_var('CLIENT_ID')
|
|
client_secret = get_systemd_env_var('CLIENT_SECRET')
|
|
|
|
print("Testing Microsoft Entra ID OAuth Configuration (from systemd service)")
|
|
print("=" * 60)
|
|
print(f"TENANT_ID: {tenant_id}")
|
|
print(f"CLIENT_ID: {client_id}")
|
|
print(f"CLIENT_SECRET: {'*' * len(client_secret) if client_secret else 'None'}")
|
|
print()
|
|
|
|
if not all([tenant_id, client_id, client_secret]):
|
|
print("❌ ERROR: Missing required environment variables in systemd service!")
|
|
print("The PDF renamer service doesn't have the Microsoft Entra credentials set.")
|
|
print()
|
|
print("To fix this:")
|
|
print("1. Edit the service file: sudo nano /etc/systemd/system/pdf-renamer.service")
|
|
print("2. Add these lines in the [Service] section:")
|
|
print(" Environment=\"TENANT_ID=your-tenant-id\"")
|
|
print(" Environment=\"CLIENT_ID=your-client-id\"")
|
|
print(" Environment=\"CLIENT_SECRET=your-client-secret\"")
|
|
print("3. Reload and restart: sudo systemctl daemon-reload && sudo systemctl restart pdf-renamer")
|
|
return False
|
|
|
|
if tenant_id in ['your-tenant-id', 'YOUR_MICROSOFT_ENTRA_TENANT_ID']:
|
|
print("❌ ERROR: TENANT_ID is still set to default placeholder value!")
|
|
return False
|
|
|
|
if client_id in ['your-client-id', 'YOUR_MICROSOFT_ENTRA_CLIENT_ID']:
|
|
print("❌ ERROR: CLIENT_ID is still set to default placeholder value!")
|
|
return False
|
|
|
|
if client_secret in ['your-client-secret', 'YOUR_MICROSOFT_ENTRA_CLIENT_SECRET']:
|
|
print("❌ ERROR: CLIENT_SECRET is still set to default placeholder value!")
|
|
return False
|
|
|
|
# Test metadata URL
|
|
metadata_url = f'https://login.microsoftonline.com/{tenant_id}/v2.0/.well-known/openid_configuration'
|
|
print(f"Testing metadata URL: {metadata_url}")
|
|
|
|
try:
|
|
response = requests.get(metadata_url, timeout=10)
|
|
if response.status_code == 200:
|
|
print("✅ Metadata URL is accessible")
|
|
else:
|
|
print(f"❌ Metadata URL returned status {response.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ Error accessing metadata URL: {e}")
|
|
return False
|
|
|
|
# Test OAuth client creation
|
|
try:
|
|
client = OAuth2Session(
|
|
client_id=client_id,
|
|
client_secret=client_secret,
|
|
scope=['openid', 'email', 'profile']
|
|
)
|
|
print("✅ OAuth client created successfully")
|
|
except Exception as e:
|
|
print(f"❌ Error creating OAuth client: {e}")
|
|
return False
|
|
|
|
print()
|
|
print("🎉 OAuth configuration in systemd service appears to be correct!")
|
|
print()
|
|
print("Next steps:")
|
|
print("1. Make sure your redirect URI is registered in Microsoft Entra:")
|
|
print(" https://yourdomain.com/auth")
|
|
print("2. Restart the application: sudo systemctl restart pdf-renamer")
|
|
print("3. Check the application logs for any remaining issues")
|
|
|
|
return True
|
|
|
|
if __name__ == '__main__':
|
|
test_oauth_config() |