From d5f9d3be49af4d127abc2f4dca892f5f2ad50475 Mon Sep 17 00:00:00 2001 From: UdoChudo Date: Sun, 22 Jun 2025 23:23:31 +0500 Subject: [PATCH] fix: (auth page) initialize checking ldap available before login connection to display correct message instean "Invalid username or password" Signed-off-by: UdoChudo --- app/services/auth_service.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/app/services/auth_service.py b/app/services/auth_service.py index 39d047c..19a1f1d 100644 --- a/app/services/auth_service.py +++ b/app/services/auth_service.py @@ -1,9 +1,12 @@ from flask import current_app from flask_ldap3_login import LDAP3LoginManager, AuthenticationResponseStatus +from ldap3.core.exceptions import LDAPSocketOpenError, LDAPServerPoolExhaustedError from werkzeug.middleware.proxy_fix import ProxyFix + import config + def init_ldap(app): app.config['LDAP_HOST'] = config.LDAP_HOST app.config['LDAP_PORT'] = config.LDAP_PORT @@ -24,15 +27,41 @@ def init_ldap(app): ldap_manager.init_app(app) app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1) + + def authenticate_user(username, password): ldap_manager = current_app.extensions['ldap3_login'] - response = ldap_manager.authenticate(username, password) + + try: + # Явно пытаемся подключиться к LDAP + conn = ldap_manager.connection + if not conn.bind(): + current_app.logger.error(f"Ошибка соединения с LDAP: {conn.last_error}") + return False, None, "LDAP-сервер недоступен. Повторите попытку позже." + except LDAPServerPoolExhaustedError as e: + current_app.logger.error(f"LDAP сервер недоступен: {e}") + return False, None, "LDAP-сервер не отвечает. Повторите попытку позже." + except LDAPSocketOpenError as e: + current_app.logger.error(f"Ошибка подключения к LDAP: {e}") + return False, None, "Ошибка подключения к LDAP-серверу. Повторите попытку позже." + except Exception as e: + current_app.logger.exception("Непредвиденная ошибка при подключении к LDAP") + return False, None, "Внутренняя ошибка при подключении к LDAP." + + try: + response = ldap_manager.authenticate(username, password) + except Exception as e: + current_app.logger.exception("Ошибка при попытке аутентификации") + return False, None, "Ошибка при выполнении аутентификации." + if response.status == AuthenticationResponseStatus.success: return True, response.user_info, None elif response.status == AuthenticationResponseStatus.fail: - return False, None, "Invalid username or password." + return False, None, "Неверное имя пользователя или пароль." else: - return False, None, f"LDAP Error: {response.status}" + return False, None, f"LDAP ошибка: {response.status}" + + def parse_ldap_user(user_info): def get(attr):