diff --git a/pdf_renamer.py b/pdf_renamer.py index 87433d9..28b4813 100644 --- a/pdf_renamer.py +++ b/pdf_renamer.py @@ -29,14 +29,25 @@ print(f"DEBUG: CLIENT_SECRET = {'*' * len(CLIENT_SECRET) if CLIENT_SECRET else ' ADMIN_USERNAME = os.getenv('ADMIN_USERNAME', 'trbcit') ADMIN_PASSWORD = os.getenv('ADMIN_PASSWORD', 'atdrmbicn') +# Initialize OAuth oauth = OAuth(app) -microsoft = oauth.register( - name='microsoft', - client_id=CLIENT_ID, - client_secret=CLIENT_SECRET, - server_metadata_url=f'https://login.microsoftonline.com/{TENANT_ID}/v2.0/.well-known/openid_configuration', - client_kwargs={'scope': 'openid email profile'} -) + +# Register Microsoft OAuth provider with proper configuration +try: + microsoft = oauth.register( + name='microsoft', + client_id=CLIENT_ID, + client_secret=CLIENT_SECRET, + server_metadata_url=f'https://login.microsoftonline.com/{TENANT_ID}/v2.0/.well-known/openid-configuration', + client_kwargs={ + 'scope': 'openid email profile', + 'token_endpoint_auth_method': 'client_secret_post' + } + ) + print("DEBUG: Microsoft OAuth provider registered successfully") +except Exception as e: + print(f"ERROR: Failed to register Microsoft OAuth provider: {e}") + microsoft = None def analyze_failed_extractions(log_path): """ @@ -654,6 +665,11 @@ def login(): @app.route('/entra-login') def entra_login(): + # Check if Microsoft OAuth provider is available + if microsoft is None: + flash('Microsoft Entra ID authentication is not configured.', 'error') + return redirect(url_for('login')) + # Check if Microsoft Entra ID is configured if TENANT_ID in ['your-tenant-id', 'YOUR_MICROSOFT_ENTRA_TENANT_ID'] or \ CLIENT_ID in ['your-client-id', 'YOUR_MICROSOFT_ENTRA_CLIENT_ID'] or \ @@ -661,8 +677,16 @@ def entra_login(): flash('Microsoft Entra ID is not properly configured. Please check your environment variables.', 'error') return redirect(url_for('login')) - redirect_uri = url_for('auth', _external=True) - return microsoft.authorize_redirect(redirect_uri) + try: + redirect_uri = url_for('auth', _external=True) + print(f"DEBUG: Initiating OAuth with redirect_uri: {redirect_uri}") + return microsoft.authorize_redirect(redirect_uri) + except Exception as e: + print(f"ERROR: Failed to initiate OAuth: {e}") + import traceback + print(f"Traceback: {traceback.format_exc()}") + flash('Failed to initiate Microsoft authentication. Please try again or use admin login.', 'error') + return redirect(url_for('login')) @app.route('/admin-login', methods=['POST']) def admin_login():