from flask import current_app from flask_wtf import FlaskForm from .form.auth import PasswordForm from hmac import compare_digest as compare_hash import crypt from .model import User import logging from abc import ABC, abstractclassmethod, abstractmethod logger = logging.getLogger(__name__) class AuthProvider(ABC): @classmethod def get_name(cls): return cls.__name__ @staticmethod @abstractmethod def get_form() -> FlaskForm: ... @staticmethod def check_auth(user: User, form) -> bool: ''' checks the submited form is valid return true if user is allowed to auth ''' return False class PasswordAuthProvider(AuthProvider): @staticmethod def get_form() -> FlaskForm: return PasswordForm(prefix='password') @staticmethod def check_auth(user: User, form: FlaskForm) -> bool: if isinstance(form.data['password'], str): return PasswordAuthProvider.check_auth_internal(user, form.data['password']) else: return False @staticmethod def check_auth_internal(user: User, password: str) -> bool: return compare_hash(crypt.crypt(password, user.password_hashed),user.password_hashed) AUTH_PROVIDER_LIST = [ PasswordAuthProvider ] #print(LdapAuthProvider.get_name())