Add LDAP Aunthentication #18
20
.env
20
.env
@@ -4,7 +4,7 @@
|
|||||||
FLASK_ENV=development
|
FLASK_ENV=development
|
||||||
FLASK_DEBUG=True
|
FLASK_DEBUG=True
|
||||||
FLASK_HOST=0.0.0.0
|
FLASK_HOST=0.0.0.0
|
||||||
FLASK_PORT=5015
|
FLASK_PORT=5011
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Security
|
# Security
|
||||||
@@ -23,15 +23,17 @@ DB_USER=root
|
|||||||
DB_PASSWORD=root
|
DB_PASSWORD=root
|
||||||
|
|
||||||
# DATABASE_URL=mysql+pymysql://root:root@localhost/comparisondb
|
# DATABASE_URL=mysql+pymysql://root:root@localhost/comparisondb
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# LDAP Configuration new
|
# LDAP Configuration
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
LDAP_SERVER=ldap://host.docker.internal
|
USE_LDAP_AUTH=true
|
||||||
LDAP_PORT=389
|
|
||||||
LDAP_USE_SSL=False
|
|
||||||
|
|
||||||
|
LDAP_URL=ldap://192.168.0.25:389
|
||||||
|
LDAP_BIND_DN=cn=admin,dc=lcepl,dc=org
|
||||||
|
LDAP_BIND_PASSWORD=Lcepl1950@2026
|
||||||
|
LDAP_BASE_DN=dc=lcepl,dc=org
|
||||||
LDAP_DOMAIN=lcepl.org
|
LDAP_DOMAIN=lcepl.org
|
||||||
LDAP_BASE_DN=DC=lcepl,DC=org
|
|
||||||
LDAP_SEARCH_BASE=OU=Users,DC=lcepl,DC=org
|
# OpenLDAP standard username attribute
|
||||||
|
LDAP_SEARCH_FILTER=(uid={username})
|
||||||
|
|
||||||
|
|||||||
@@ -5,20 +5,23 @@ WORKDIR /app
|
|||||||
# Install system dependencies
|
# Install system dependencies
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
gcc \
|
gcc \
|
||||||
|
default-libmysqlclient-dev \
|
||||||
|
pkg-config \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Copy requirements and install Python dependencies
|
# Copy requirements and install Python dependencies
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt gunicorn
|
||||||
|
|
||||||
# Copy application code
|
# Copy application code
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Create necessary directories
|
# Create necessary directories
|
||||||
RUN mkdir -p app/logs app/static/uploads app/static/downloads
|
RUN mkdir -p app/logs app/static/uploads app/static/downloads
|
||||||
|
ENV FLASK_APP=run.py
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
EXPOSE 5001
|
EXPOSE 5001
|
||||||
|
|
||||||
# Run the application
|
# Run the application with Gunicorn (production WSGI server)
|
||||||
CMD ["python", "run.py"]
|
CMD ["gunicorn", "--bind", "0.0.0.0:5001", "run:app"]
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
from flask import Flask, redirect, url_for
|
from flask import Flask, redirect, url_for
|
||||||
from app.config import Config
|
from app.config import Config
|
||||||
from app.services.db_service import db
|
from app.services.db_service import db, migrate
|
||||||
from app.services.logger_service import LoggerService
|
from app.services.logger_service import LoggerService
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
@@ -9,6 +9,9 @@ def create_app():
|
|||||||
|
|
||||||
# Initialize extensions
|
# Initialize extensions
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
migrate.init_app(app, db)
|
||||||
|
with app.app_context():
|
||||||
|
db.create_all()
|
||||||
|
|
||||||
# Initialize Logger
|
# Initialize Logger
|
||||||
LoggerService.init_app(app)
|
LoggerService.init_app(app)
|
||||||
@@ -35,10 +38,7 @@ def register_blueprints(app):
|
|||||||
from app.routes.file_report import file_report_bp
|
from app.routes.file_report import file_report_bp
|
||||||
from app.routes.generate_comparison_report import generate_report_bp
|
from app.routes.generate_comparison_report import generate_report_bp
|
||||||
from app.routes.file_format import file_format_bp
|
from app.routes.file_format import file_format_bp
|
||||||
|
|
||||||
# new
|
|
||||||
from app.routes.activity_routes import activity_bp
|
from app.routes.activity_routes import activity_bp
|
||||||
from app.routes.engineering_master_routes import engi_bp
|
|
||||||
|
|
||||||
app.register_blueprint(auth_bp)
|
app.register_blueprint(auth_bp)
|
||||||
app.register_blueprint(user_bp)
|
app.register_blueprint(user_bp)
|
||||||
@@ -47,11 +47,8 @@ def register_blueprints(app):
|
|||||||
app.register_blueprint(file_import_bp)
|
app.register_blueprint(file_import_bp)
|
||||||
app.register_blueprint(file_report_bp)
|
app.register_blueprint(file_report_bp)
|
||||||
app.register_blueprint(generate_report_bp)
|
app.register_blueprint(generate_report_bp)
|
||||||
app.register_blueprint(file_format_bp)
|
app.register_blueprint(file_format_bp )
|
||||||
|
|
||||||
# new
|
|
||||||
app.register_blueprint(activity_bp)
|
app.register_blueprint(activity_bp)
|
||||||
app.register_blueprint(engi_bp)
|
|
||||||
|
|
||||||
|
|
||||||
def register_error_handlers(app):
|
def register_error_handlers(app):
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
|
# project base url
|
||||||
|
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
# secret key
|
# secret key
|
||||||
@@ -21,14 +23,23 @@ class Config:
|
|||||||
)
|
)
|
||||||
|
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
# uploads folder path
|
||||||
|
UPLOAD_FOLDER = os.path.join(BASE_DIR, "static", "uploads")
|
||||||
|
# file extension
|
||||||
|
ALLOWED_EXTENSIONS = {"xlsx", "xls", "csv"}
|
||||||
|
|
||||||
|
# ---------------- LDAP settings ----------------
|
||||||
# LDAP Configuration New
|
USE_LDAP_AUTH = os.getenv("USE_LDAP_AUTH", "false").lower() == "true"
|
||||||
LDAP_SERVER = os.getenv("LDAP_SERVER")
|
# e.g. "ldap://192.168.0.25:389" or "ldaps://192.168.0.25:636" (preferred, encrypted)
|
||||||
LDAP_PORT = int(os.getenv("LDAP_PORT", 389))
|
LDAP_SERVER = os.getenv("LDAP_URL", "ldap://192.168.0.25:389")
|
||||||
LDAP_USE_SSL = os.getenv("LDAP_USE_SSL", "False").lower() == "true"
|
# Service/admin account used only to SEARCH for a user's real DN.
|
||||||
|
# The user's own password is never used for this bind.
|
||||||
LDAP_BASE_DN = os.getenv("LDAP_BASE_DN")
|
LDAP_BIND_DN = os.getenv("LDAP_BIND_DN", "cn=admin,dc=lcepl,dc=org")
|
||||||
LDAP_DOMAIN = os.getenv("LDAP_DOMAIN")
|
LDAP_BIND_PASSWORD = os.getenv("LDAP_BIND_PASSWORD", "")
|
||||||
|
# Base DN to search for user entries under
|
||||||
LDAP_SEARCH_BASE = os.getenv("LDAP_SEARCH_BASE")
|
LDAP_BASE_DN = os.getenv("LDAP_BASE_DN", "dc=lcepl,dc=org")
|
||||||
|
# Used only as a fallback to build an email if the directory entry has none
|
||||||
|
LDAP_DOMAIN = os.getenv("LDAP_DOMAIN", "lcepl.org")
|
||||||
|
# Filter used to find the user's entry by their login username.
|
||||||
|
# Standard OpenLDAP attribute is "uid". Active Directory would use sAMAccountName.
|
||||||
|
LDAP_SEARCH_FILTER = os.getenv("LDAP_SEARCH_FILTER", "(uid={username})")
|
||||||
|
|||||||
@@ -7,10 +7,13 @@ class User(db.Model):
|
|||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
name = db.Column(db.String(200), nullable=False)
|
name = db.Column(db.String(200), nullable=False)
|
||||||
email = db.Column(db.String(120), unique=True, nullable=False)
|
email = db.Column(db.String(120), unique=True, nullable=False)
|
||||||
password_hash = db.Column(db.String(255), nullable=False)
|
password_hash = db.Column(db.String(255), nullable=True)
|
||||||
|
auth_source = db.Column(db.String(20), nullable=False, default="local")
|
||||||
|
|
||||||
def set_password(self, password):
|
def set_password(self, password):
|
||||||
self.password_hash = generate_password_hash(password)
|
self.password_hash = generate_password_hash(password)
|
||||||
|
|
||||||
def check_password(self, password):
|
def check_password(self, password):
|
||||||
|
if not self.password_hash:
|
||||||
|
return False
|
||||||
return check_password_hash(self.password_hash, password)
|
return check_password_hash(self.password_hash, password)
|
||||||
|
|||||||
@@ -1,98 +1,49 @@
|
|||||||
from flask import (Blueprint, render_template, request, redirect, url_for, flash, session, current_app)
|
from flask import Blueprint, render_template, request, redirect, url_for, flash, session
|
||||||
|
|
||||||
from app.services.user_service import UserService
|
from app.services.user_service import UserService
|
||||||
from app.constants.messages import SuccessMessage, ErrorMessage
|
|
||||||
from app.constants.http_status import HTTPStatus
|
|
||||||
|
|
||||||
auth_bp = Blueprint("auth", __name__)
|
auth_bp = Blueprint("auth", __name__)
|
||||||
|
|
||||||
|
|
||||||
# ==========================
|
|
||||||
# LOGIN
|
|
||||||
# ==========================
|
|
||||||
@auth_bp.route("/login", methods=["GET", "POST"])
|
@auth_bp.route("/login", methods=["GET", "POST"])
|
||||||
def login():
|
def login():
|
||||||
|
|
||||||
if session.get("user_id"):
|
if session.get("user_id"):
|
||||||
current_app.logger.info("User already logged in.")
|
|
||||||
return redirect(url_for("dashboard.dashboard"))
|
return redirect(url_for("dashboard.dashboard"))
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
|
email = request.form.get("email")
|
||||||
try:
|
password = request.form.get("password")
|
||||||
email = request.form.get("email", "").strip()
|
|
||||||
password = request.form.get("password", "")
|
|
||||||
|
|
||||||
if not email or not password:
|
|
||||||
flash(ErrorMessage.INVALID_REQUEST, "danger")
|
|
||||||
current_app.logger.warning("Login failed. Email or password missing.")
|
|
||||||
return render_template("login.html", title="Login")
|
|
||||||
|
|
||||||
user = UserService.validate_login(email, password)
|
user = UserService.validate_login(email, password)
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
session.clear()
|
|
||||||
session["user_id"] = user.id
|
session["user_id"] = user.id
|
||||||
session["user_name"] = user.name
|
session["user_name"] = user.name
|
||||||
session["email"] = user.email
|
session["user_email"] = user.email
|
||||||
session.permanent = True
|
flash("Login successful", "success")
|
||||||
|
|
||||||
current_app.logger.info(f"Login successful. User={user.name}")
|
|
||||||
flash(SuccessMessage.LOGIN, "success")
|
|
||||||
return redirect(url_for("dashboard.dashboard"))
|
return redirect(url_for("dashboard.dashboard"))
|
||||||
|
|
||||||
current_app.logger.warning(f"Invalid login attempt. Email={email}")
|
flash("Invalid email or password", "danger")
|
||||||
flash(ErrorMessage.LOGIN_FAILED,"danger")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
current_app.logger.exception("Login Error" )
|
|
||||||
flash(ErrorMessage.INTERNAL_SERVER_ERROR,"danger")
|
|
||||||
|
|
||||||
return render_template("login.html", title="Login")
|
return render_template("login.html", title="Login")
|
||||||
|
|
||||||
|
|
||||||
# ==========================
|
|
||||||
# LOGOUT
|
|
||||||
# ==========================
|
|
||||||
@auth_bp.route("/logout")
|
@auth_bp.route("/logout")
|
||||||
def logout():
|
def logout():
|
||||||
username = session.get("user_name", "Unknown")
|
|
||||||
session.clear()
|
session.clear()
|
||||||
current_app.logger.info(f"Logout successful. User={username}")
|
flash("Logged out successfully", "info")
|
||||||
flash(SuccessMessage.LOGOUT,"info")
|
|
||||||
|
|
||||||
return redirect(url_for("auth.login"))
|
return redirect(url_for("auth.login"))
|
||||||
|
|
||||||
|
|
||||||
# ==========================
|
|
||||||
# REGISTER
|
|
||||||
# ==========================
|
|
||||||
@auth_bp.route("/register", methods=["GET", "POST"])
|
@auth_bp.route("/register", methods=["GET", "POST"])
|
||||||
def register():
|
def register():
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
try:
|
name = request.form.get("name")
|
||||||
name = request.form.get("name", "").strip()
|
email = request.form.get("email")
|
||||||
email = request.form.get("email", "").strip()
|
password = request.form.get("password")
|
||||||
password = request.form.get("password", "")
|
|
||||||
|
|
||||||
if not name or not email or not password:
|
|
||||||
flash(ErrorMessage.INVALID_REQUEST,"danger")
|
|
||||||
return redirect(url_for("auth.register"))
|
|
||||||
|
|
||||||
user = UserService.register_user(name, email, password)
|
user = UserService.register_user(name, email, password)
|
||||||
|
|
||||||
if not user:
|
if not user:
|
||||||
current_app.logger.warning(f"Duplicate Registration: {email}")
|
flash("Email already exists", "danger")
|
||||||
flash(ErrorMessage.DUPLICATE_ENTRY,"danger")
|
|
||||||
return redirect(url_for("auth.register"))
|
return redirect(url_for("auth.register"))
|
||||||
|
|
||||||
current_app.logger.info(f"New user registered: {email}")
|
flash("User registered successfully", "success")
|
||||||
flash(SuccessMessage.CREATED,"success")
|
|
||||||
return redirect(url_for("auth.login"))
|
return redirect(url_for("auth.login"))
|
||||||
|
|
||||||
except Exception:
|
return render_template("register.html", title="Register")
|
||||||
current_app.logger.exception("User Registration Failed" )
|
|
||||||
flash(ErrorMessage.INTERNAL_SERVER_ERROR,"danger")
|
|
||||||
|
|
||||||
return render_template("register.html",title="Register")
|
|
||||||
|
|||||||
@@ -1,130 +1,72 @@
|
|||||||
from ldap3 import (
|
|
||||||
Server,
|
|
||||||
Connection,
|
|
||||||
ALL,
|
|
||||||
NTLM,
|
|
||||||
SIMPLE,
|
|
||||||
SUBTREE
|
|
||||||
)
|
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from app.config import Config
|
from ldap3 import Server, Connection, ALL, SUBTREE
|
||||||
|
from ldap3.core.exceptions import LDAPException
|
||||||
|
|
||||||
|
|
||||||
class LDAPService:
|
class LDAPService:
|
||||||
"""
|
"""
|
||||||
LDAP / Active Directory Authentication Service
|
Handles authentication against an LDAP / OpenLDAP server using the
|
||||||
|
standard "search + bind" pattern:
|
||||||
|
1. Bind with a service/admin account just to SEARCH for the user's DN.
|
||||||
|
2. Re-bind using that DN + the password the user typed, to verify it.
|
||||||
|
The user's typed password is only ever used in step 2, never sent
|
||||||
|
anywhere else.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def authenticate(username, password):
|
def authenticate(username, password):
|
||||||
"""
|
|
||||||
Authenticate LDAP User
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
{
|
|
||||||
"success": True,
|
|
||||||
"user": {
|
|
||||||
"username": "...",
|
|
||||||
"name": "...",
|
|
||||||
"email": "..."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OR
|
|
||||||
|
|
||||||
{
|
|
||||||
"success": False,
|
|
||||||
"message": "Invalid username or password"
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
if not username or not password:
|
if not username or not password:
|
||||||
return {
|
return None
|
||||||
"success": False,
|
|
||||||
"message": "Username and Password are required."
|
|
||||||
}
|
|
||||||
|
|
||||||
|
server = Server(current_app.config["LDAP_SERVER"], get_info=ALL)
|
||||||
|
|
||||||
|
# --- Step 1: bind as the admin/service account to search the directory ---
|
||||||
try:
|
try:
|
||||||
|
admin_conn = Connection(
|
||||||
# -----------------------------------
|
|
||||||
# LDAP SERVER
|
|
||||||
# -----------------------------------
|
|
||||||
server = Server(
|
|
||||||
Config.LDAP_SERVER,
|
|
||||||
port=Config.LDAP_PORT,
|
|
||||||
use_ssl=Config.LDAP_USE_SSL,
|
|
||||||
get_info=ALL
|
|
||||||
)
|
|
||||||
|
|
||||||
# -----------------------------------
|
|
||||||
# Login Format
|
|
||||||
#
|
|
||||||
# username@domain.com
|
|
||||||
# -----------------------------------
|
|
||||||
user_dn = f"{username}@{Config.LDAP_DOMAIN}"
|
|
||||||
|
|
||||||
conn = Connection(
|
|
||||||
server,
|
server,
|
||||||
user=user_dn,
|
user=current_app.config["LDAP_BIND_DN"],
|
||||||
password=password,
|
password=current_app.config["LDAP_BIND_PASSWORD"],
|
||||||
authentication=SIMPLE,
|
auto_bind=True,
|
||||||
auto_bind=True
|
|
||||||
)
|
)
|
||||||
|
except LDAPException as e:
|
||||||
|
current_app.logger.error(f"LDAP service account bind failed: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
# -----------------------------------
|
# --- Step 2: find the user's real DN + profile attributes ---
|
||||||
# Search User
|
try:
|
||||||
# -----------------------------------
|
search_filter = current_app.config["LDAP_SEARCH_FILTER"].format(username=username)
|
||||||
search_filter = f"(sAMAccountName={username})"
|
admin_conn.search(
|
||||||
|
search_base=current_app.config["LDAP_BASE_DN"],
|
||||||
conn.search(
|
|
||||||
search_base=Config.LDAP_SEARCH_BASE,
|
|
||||||
search_filter=search_filter,
|
search_filter=search_filter,
|
||||||
search_scope=SUBTREE,
|
search_scope=SUBTREE,
|
||||||
attributes=[
|
attributes=["cn", "mail", "uid"],
|
||||||
"displayName",
|
|
||||||
"mail",
|
|
||||||
"givenName",
|
|
||||||
"sn",
|
|
||||||
"cn"
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
except LDAPException as e:
|
||||||
|
current_app.logger.error(f"LDAP search failed for '{username}': {e}")
|
||||||
|
admin_conn.unbind()
|
||||||
|
return None
|
||||||
|
|
||||||
display_name = username
|
if not admin_conn.entries:
|
||||||
email = ""
|
current_app.logger.warning(f"LDAP user not found: {username}")
|
||||||
|
admin_conn.unbind()
|
||||||
|
return None
|
||||||
|
|
||||||
if conn.entries:
|
entry = admin_conn.entries[0]
|
||||||
|
user_dn = entry.entry_dn
|
||||||
entry = conn.entries[0]
|
name = str(entry.cn) if "cn" in entry and entry.cn.value else username
|
||||||
|
email = (
|
||||||
if "displayName" in entry:
|
str(entry.mail)
|
||||||
display_name = str(entry.displayName)
|
if "mail" in entry and entry.mail.value
|
||||||
|
else f"{username}@{current_app.config['LDAP_DOMAIN']}"
|
||||||
if "mail" in entry:
|
|
||||||
email = str(entry.mail)
|
|
||||||
|
|
||||||
conn.unbind()
|
|
||||||
|
|
||||||
current_app.logger.info(
|
|
||||||
f"LDAP Login Success : {username}"
|
|
||||||
)
|
)
|
||||||
|
admin_conn.unbind()
|
||||||
|
|
||||||
return {
|
# --- Step 3: the actual auth check - bind AS the user with their password ---
|
||||||
"success": True,
|
try:
|
||||||
"user": {
|
user_conn = Connection(server, user=user_dn, password=password, auto_bind=True)
|
||||||
"username": username,
|
user_conn.unbind()
|
||||||
"name": display_name,
|
except LDAPException as e:
|
||||||
"email": email
|
current_app.logger.warning(f"LDAP authentication failed for '{username}': {e}")
|
||||||
}
|
return None
|
||||||
}
|
|
||||||
|
|
||||||
except Exception as ex:
|
return {"username": username, "name": name, "email": email}
|
||||||
|
|
||||||
current_app.logger.warning(
|
|
||||||
f"LDAP Login Failed : {username} : {str(ex)}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"success": False,
|
|
||||||
"message": "Invalid Username or Password."
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
from flask import current_app
|
||||||
from app.models.user_model import User
|
from app.models.user_model import User
|
||||||
from app.services.db_service import db
|
from app.services.db_service import db
|
||||||
from flask import current_app
|
from app.services.ldap_service import LDAPService
|
||||||
|
|
||||||
class UserService:
|
class UserService:
|
||||||
|
|
||||||
@@ -9,21 +10,57 @@ class UserService:
|
|||||||
if User.query.filter_by(email=email).first():
|
if User.query.filter_by(email=email).first():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
user = User(name=name, email=email)
|
user = User(name=name, email=email, auth_source="local")
|
||||||
user.set_password(password)
|
user.set_password(password)
|
||||||
|
|
||||||
db.session.add(user)
|
db.session.add(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
current_app.logger.info("User list viewed")
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def validate_login(email, password):
|
def validate_login(identifier, password):
|
||||||
user = User.query.filter_by(email=email).first()
|
"""
|
||||||
|
identifier = whatever was typed in the login form. Can be an email
|
||||||
|
(local users) or an LDAP username, depending on USE_LDAP_AUTH.
|
||||||
|
"""
|
||||||
|
if current_app.config.get("USE_LDAP_AUTH"):
|
||||||
|
ldap_user = UserService._validate_ldap_login(identifier, password)
|
||||||
|
if ldap_user:
|
||||||
|
return ldap_user
|
||||||
|
return None
|
||||||
|
user = User.query.filter_by(email=identifier).first()
|
||||||
if user and user.check_password(password):
|
if user and user.check_password(password):
|
||||||
return user
|
return user
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_ldap_login(username, password):
|
||||||
|
ldap_info = LDAPService.authenticate(username, password)
|
||||||
|
if not ldap_info:
|
||||||
|
return None
|
||||||
|
return UserService._get_or_create_ldap_user(ldap_info)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_or_create_ldap_user(ldap_info):
|
||||||
|
"""
|
||||||
|
LDAP is the source of truth for the password. We still keep a row in
|
||||||
|
our local `users` table (no password) so the rest of the app - which
|
||||||
|
expects a User with an id - keeps working unchanged.
|
||||||
|
"""
|
||||||
|
user = User.query.filter_by(email=ldap_info["email"]).first()
|
||||||
|
if user is None:
|
||||||
|
user = User(
|
||||||
|
name=ldap_info["name"],
|
||||||
|
email=ldap_info["email"],
|
||||||
|
auth_source="ldap",
|
||||||
|
)
|
||||||
|
db.session.add(user)
|
||||||
|
db.session.commit()
|
||||||
|
elif user.name != ldap_info["name"]:
|
||||||
|
user.name = ldap_info["name"]
|
||||||
|
db.session.commit()
|
||||||
|
return user
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all_users():
|
def get_all_users():
|
||||||
return User.query.all()
|
return User.query.all()
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ services:
|
|||||||
build: .
|
build: .
|
||||||
container_name: comparison_app
|
container_name: comparison_app
|
||||||
restart: always
|
restart: always
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
environment:
|
environment:
|
||||||
FLASK_ENV: development
|
FLASK_ENV: production
|
||||||
FLASK_DEBUG: "True"
|
FLASK_DEBUG: "False"
|
||||||
FLASK_HOST: "0.0.0.0"
|
FLASK_HOST: "0.0.0.0"
|
||||||
FLASK_PORT: "5001"
|
FLASK_PORT: "5001"
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
Flask
|
Flask
|
||||||
|
ldap3
|
||||||
pandas
|
pandas
|
||||||
openpyxl
|
openpyxl
|
||||||
xlrd
|
xlrd
|
||||||
@@ -9,4 +10,3 @@ xlsxwriter
|
|||||||
matplotlib
|
matplotlib
|
||||||
flask_sqlalchemy
|
flask_sqlalchemy
|
||||||
flask_migrate
|
flask_migrate
|
||||||
weasyprint
|
|
||||||
4
run.py
4
run.py
@@ -1,15 +1,11 @@
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
from app import create_app
|
from app import create_app
|
||||||
from app.services.db_service import db
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
app = create_app()
|
app = create_app()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with app.app_context():
|
|
||||||
db.create_all()
|
|
||||||
|
|
||||||
app.run(
|
app.run(
|
||||||
host=os.getenv("FLASK_HOST"),
|
host=os.getenv("FLASK_HOST"),
|
||||||
port=int(os.getenv("FLASK_PORT")),
|
port=int(os.getenv("FLASK_PORT")),
|
||||||
|
|||||||
Reference in New Issue
Block a user