lenticular_cloud2/lenticular_cloud/auth_providers.py

86 lines
2 KiB
Python
Raw 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
from .form.auth import PasswordForm, TotpForm, Fido2Form
2022-06-18 17:35:05 +00:00
from hmac import compare_digest as compare_hash
import crypt
from .model import User
import logging
logger = logging.getLogger(__name__)
2020-05-09 18:00:07 +00:00
class AuthProvider:
@classmethod
def get_name(csl):
return csl.__name__
@staticmethod
2022-06-18 17:35:05 +00:00
def get_form() -> FlaskForm:
2020-05-09 18:00:07 +00:00
return
@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
class U2FAuthProvider(AuthProvider):
@staticmethod
2022-06-18 17:35:05 +00:00
def get_from() -> FlaskForm:
2020-05-09 18:00:07 +00:00
return Fido2Form(prefix='fido2')
class WebAuthProvider(AuthProvider):
pass
class TotpAuthProvider(AuthProvider):
@staticmethod
def get_form():
return TotpForm(prefix='totp')
@staticmethod
2022-06-18 17:35:05 +00:00
def check_auth(user: User, form: FlaskForm) -> bool:
2020-05-09 18:00:07 +00:00
data = form.data['totp']
if data is not None:
2022-02-06 22:57:01 +00:00
#print(f'data totp: {data}')
2020-05-10 12:34:28 +00:00
if len(user.totps) == 0: # migration, TODO remove
return True
2020-05-09 18:00:07 +00:00
for totp in user.totps:
2020-05-10 12:34:28 +00:00
if totp.verify(data):
2020-05-09 18:00:07 +00:00
return True
return False
AUTH_PROVIDER_LIST = [
2022-06-18 17:35:05 +00:00
PasswordAuthProvider,
2020-05-09 18:00:07 +00:00
TotpAuthProvider
]
2022-02-06 22:57:01 +00:00
#print(LdapAuthProvider.get_name())
2020-05-09 18:00:07 +00:00