authentic/src/authentic2/authenticators.py

50 lines
1.6 KiB
Python

# authentic2 - versatile identity manager
# Copyright (C) 2010-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from .utils.evaluate import evaluate_condition
logger = logging.getLogger(__name__)
class BaseAuthenticator:
how = ()
def __init__(self, show_condition=None, **kwargs):
self.show_condition = show_condition
def get_show_condition(self, instance_id=None):
if isinstance(self.show_condition, dict):
if instance_id and instance_id in self.show_condition:
return self.show_condition[instance_id]
else:
return self.show_condition
def shown(self, instance_id=None, ctx=()):
show_condition = self.get_show_condition(instance_id)
if not show_condition:
return True
ctx = dict(ctx, id=instance_id)
try:
return evaluate_condition(show_condition, ctx, on_raise=True)
except Exception as e:
logger.error(e)
return False
def get_identifier(self):
return self.id