lenticular_cloud2/lenticular_cloud/auth_providers.py

58 lines
1.3 KiB
Python
Raw Permalink Normal View History

2020-05-09 18:00:07 +00:00
from flask import current_app
2022-06-18 17:35:05 +00:00
from flask_wtf import FlaskForm
2023-12-25 17:55:20 +00:00
from .form.auth import PasswordForm
2022-06-18 17:35:05 +00:00
from hmac import compare_digest as compare_hash
import crypt
from .model import User
import logging
2024-05-20 10:40:34 +00:00
from abc import ABC, abstractclassmethod, abstractmethod
logger = logging.getLogger(__name__)
2020-05-09 18:00:07 +00:00
2024-05-20 10:40:34 +00:00
class AuthProvider(ABC):
2020-05-09 18:00:07 +00:00
@classmethod
2024-05-20 10:40:34 +00:00
def get_name(cls):
return cls.__name__
2020-05-09 18:00:07 +00:00
@staticmethod
2024-05-20 10:40:34 +00:00
@abstractmethod
2022-06-18 17:35:05 +00:00
def get_form() -> FlaskForm:
2024-05-20 10:40:34 +00:00
...
2020-05-09 18:00:07 +00:00
@staticmethod
2022-06-18 17:35:05 +00:00
def check_auth(user: User, form) -> bool:
2020-05-09 18:00:07 +00:00
'''
checks the submited form is valid
return true if user is allowed to auth
'''
return False
2022-06-18 17:35:05 +00:00
class PasswordAuthProvider(AuthProvider):
2020-05-09 18:00:07 +00:00
@staticmethod
2022-06-18 17:35:05 +00:00
def get_form() -> FlaskForm:
2020-05-09 18:00:07 +00:00
return PasswordForm(prefix='password')
@staticmethod
2022-06-18 17:35:05 +00:00
def check_auth(user: User, form: FlaskForm) -> bool:
if isinstance(form.data['password'], str):
return PasswordAuthProvider.check_auth_internal(user, form.data['password'])
else:
2020-05-09 18:00:07 +00:00
return False
2022-06-18 17:35:05 +00:00
@staticmethod
def check_auth_internal(user: User, password: str) -> bool:
return compare_hash(crypt.crypt(password, user.password_hashed),user.password_hashed)
2020-05-09 18:00:07 +00:00
AUTH_PROVIDER_LIST = [
2023-12-25 18:44:38 +00:00
PasswordAuthProvider
2020-05-09 18:00:07 +00:00
]
2022-02-06 22:57:01 +00:00
#print(LdapAuthProvider.get_name())
2020-05-09 18:00:07 +00:00