File size: 5,166 Bytes
6b6d936 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | ##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""External 2FA Authentication Registry."""
from abc import abstractmethod, abstractproperty
from typing import Union
import flask
from pgadmin.utils.dynamic_registry import create_registry_metaclass
"""
class: MultiFactorAuthRegistry
An registry factory for the multi-factor authentication methods.
"""
@classmethod
def load_modules(cls, app=None):
submodules = []
from . import authenticator as module
submodules.append(module)
from . import email as module
submodules.append(module)
from . import utils as module
submodules.append(module)
from . import views as module
submodules.append(module)
for module in submodules:
if "init_app" in module.__dict__.keys():
module.__dict__["init_app"](app)
MultiFactorAuthRegistry = create_registry_metaclass(
'MultiFactorAuthRegistry', __package__, load_modules=load_modules,
decorate_as_module=True
)
class BaseMFAuth(metaclass=MultiFactorAuthRegistry):
"""
Base Multi-Factor Authentication (MFA) class
A Class implements this class will be registered with
the registry class 'MultiFactorAuthRegistry', and it will be automatically
available as a MFA method.
"""
@property
@abstractmethod
def name(self) -> str:
"""
Represents the short name for the authentiation method. It can be used
in the MFA_SUPPORTED_METHODS parameter in the configuration as a
supported authentication method.
Returns:
str: Short name for this authentication method
NOTE: Name must not contain special characters
"""
pass
@property
@abstractmethod
def label(self) -> str:
"""
Represents the user visible name for the authentiation method. It will
be visible on the authentication page and registration page.
Returns:
str: Value for the UI for the authentication method
"""
pass
@property
def icon(self) -> str:
"""
A url for the icon for the authentication method.
Returns:
str: Value for the UI for the authentication method
"""
return ""
@property
def validate_script(self) -> Union[str, None]:
"""
A url route for the javscript required for the auth method.
Override this method for the auth methods, when it required a
javascript on the authentication page.
Returns:
Union[str, None]: Url for the auth method or None
"""
return None
@abstractmethod
def validate(self, **kwargs) -> str:
"""
Validate the code/password sent using the HTTP request during the
authentication process.
If the validation is not done successfully for some reason, it must
raise a ValidationException exception.
Parameters:
kwargs: data sent during the authentication process
Raises:
ValidationException: Raises when code/otp is not valid
"""
pass
@abstractmethod
def validation_view(self) -> str:
"""
Authenction route (view) for the auth method.
"""
pass
@abstractmethod
def registration_view(self, form_data) -> str:
"""
Registration View for the auth method.
Must override this for rendering the registration page for the auth
method.
Args:
form_data (dict): Form data sent from the registration page.
"""
pass
def register_url_endpoints(self, blueprint: flask.Blueprint) -> None:
"""
Register the URL end-points for the auth method (special case).
Args:
blueprint (flask.Blueprint): MFA blueprint for registering the
end-point for the method
NOTE: Override this method only when there is special need to expose
an url end-point for the auth method.
"""
pass
def to_dict(self) -> dict:
"""
A diction representation for the auth method.
Returns:
dict (id, label, icon): Diction representation for an auth method.
"""
return {
"id": self.name,
"label": self.label,
"icon": self.icon,
}
def validation_view_dict(self, selected_mfa: str) -> dict:
"""
A diction representation for the auth method to be used on the
registration page.
Returns:
dict: Diction representation for an auth method to be used on the
regisration page.
"""
res = self.to_dict()
res['view'] = self.validation_view()
res['selected'] = selected_mfa == self.name
res['script'] = self.validate_script
return res
|