diff --git a/main.py b/main.py index 95c36c2..9556549 100644 --- a/main.py +++ b/main.py @@ -26,12 +26,11 @@ hostname = socket.gethostname() print( "Running on: {}".format( hostname) ) app = Flask(__name__) -### what is this value? I gather I should change it? app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['ENV'] = os.environ['FLASK_ENV'] -app.config.from_mapping( SECRET_KEY=b'\xd6\x04\xbdj\xfe\xed$c\x1e@\xad\x0f\x13,@G') +app.config.from_mapping( SECRET_KEY=b'\xe9\xaf\xe1b\xe9mg\xbe\\\x90\x1f(\xf8Kp\xce91\x17\x14lJ\x9e0') # ldap config vars: (the last one is required, or python ldap freaks out) app.config['LDAP_HOST'] = 'mara.ddp.net' @@ -44,14 +43,14 @@ app.config['LDAP_BIND_USER_DN'] = None app.config['LDAP_BIND_USER_PASSWORD'] = None app.config['LDAP_GROUP_OBJECT_FILTER'] = '(objectclass=posixGroup)' -db = SQLAlchemy(app) -ma = Marshmallow(app) -Bootstrap(app) +db = SQLAlchemy(app) # create the (flask) sqlalchemy connection +ma = Marshmallow(app) # set up Marshmallow - data marshalling / serialising +Bootstrap(app) # set up Bootstrap - used in flask-forms (TODO: CONFIRM THIS IS NEEDED - sometimes I do boostrap by hand anyway) login_manager = LoginManager(app) # Setup a Flask-Login Manager ldap_manager = LDAP3LoginManager(app) # Setup a LDAP3 Login Manager. login_manager.login_view = "login" # default login route, failed with url_for, so hard-coded -################################# Now, import non-book classes ################################### +################################# Now, import separated class files ################################### from ai import aistats from settings import Settings from files import Entry, GetJM_Message, ClearJM_Message @@ -75,23 +74,8 @@ app.jinja_env.globals['LocationIcon'] = LocationIcon app.jinja_env.globals['StoragePathNames'] = StoragePathNames -# Declare an Object Model for the user, and make it comply with the -# flask-login UserMixin mixin. -class User(UserMixin): - def __init__(self, dn, username, data): - self.dn = dn - self.username = username - self.data = data - - def __repr__(self): - return self.dn - - def get_id(self): - return self.dn - -# Declare a User Loader for Flask-Login. -# Simply returns the User if it exists in our 'database', otherwise -# returns None. +# Declare a User Loader for Flask-Login. +# Returns the User if it exists in our 'database', otherwise returns None. @login_manager.user_loader def load_user(id): pau=PAUser.query.filter(PAUser.dn==id).first() @@ -99,8 +83,7 @@ def load_user(id): # Declare The User Saver for Flask-Ldap3-Login # This method is called whenever a LDAPLoginForm() successfully validates. -# Here you have to save the user, and return it so it can be used in the -# login controller. +# store the user details / session in the DB if it is not in there already @ldap_manager.save_user def save_user(dn, username, data, memberships): pau=PAUser.query.filter(PAUser.dn==dn).first() @@ -122,6 +105,9 @@ def main_page(): return render_template("base.html") +# route for the login page/box +# POST is when user submits pwd & uses flask-login to hit ldap, validate pwd +# if valid, then we save user/session into the DB via login_user() -> calls save_user() @app.route('/login', methods=['GET', 'POST']) def login(): # Instantiate a LDAPLoginForm which has a validator to check if the user @@ -135,9 +121,7 @@ def login(): print( f"WARNING: Detected special LDAP chars in username: {request.form['username']}") return redirect('/login') if form.validate_on_submit(): - # Successfully logged in, We can now access the saved user object - # via form.user. - print( f"form user = {form.user}" ) + # Successfully logged in, We can now access the saved user object via form.user. login_user(form.user, remember=True) # Tell flask-login to log them in. next = request.args.get("next") if next: