Files
Version-1-2/AppCode/Auth.py

55 lines
1.6 KiB
Python

from flask import Flask, render_template, request, redirect, url_for, send_from_directory, flash, jsonify, json
from flask_login import LoginManager, UserMixin
from logging.handlers import RotatingFileHandler
from ldap3 import Server, Connection, ALL, SUBTREE
from ldap3 import Server, Connection, ALL
from ldap3.core.exceptions import LDAPBindError
class DefaultCredentials:
username = 'admin'
password = 'admin123'
class LoginLDAP:
def __init__(self, request):
self.username = request.form['username'].strip()
self.password = request.form['password']
self.isDefaultCredentials = False
self.isValidLogin = False
self.errorMessage = ""
ldap_user_dn = f"uid={self.username},ou=users,dc=lcepl,dc=org"
ldap_server = 'ldap://localhost:389'
#Need to re-factor further
# Static fallback user
if self.username == DefaultCredentials.username and self.password == DefaultCredentials.password:
self.isDefaultCredentials = True
self.isValidLogin = True
return
try:
# LDAP authentication
conn = Connection(
Server(self.ldap_server, get_info=ALL),
user=self.ldap_user_dn,
password=self.password,
auto_bind=True
)
self.isValidLogin = True
return
except LDAPBindError:
self.errorMessage = "Invalid credentials."
except Exception as e:
self.errorMessage = str(e)
class User(UserMixin):
def __init__(self, id):
self.id = id