|
| 1 | +################################################################### |
| 2 | +# |
| 3 | +# Authors: Tom Kralidis <tomkralidis@gmail.com> |
| 4 | +# |
| 5 | +# Copyright (c) 2024 Tom Kralidis |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person |
| 8 | +# obtaining a copy of this software and associated documentation |
| 9 | +# files (the "Software"), to deal in the Software without |
| 10 | +# restriction, including without limitation the rights to use, |
| 11 | +# copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +# copies of the Software, and to permit persons to whom the |
| 13 | +# Software is furnished to do so, subject to the following |
| 14 | +# conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be |
| 17 | +# included in all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 23 | +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 27 | +# |
| 28 | +################################################################### |
| 29 | + |
| 30 | +from abc import ABC, abstractmethod |
| 31 | +import importlib |
| 32 | +import logging |
| 33 | +from typing import IO |
| 34 | + |
| 35 | +import yaml |
| 36 | + |
| 37 | +LOGGER = logging.getLogger(__name__) |
| 38 | + |
| 39 | + |
| 40 | +class AuthProvider(ABC): |
| 41 | + def __init__(self, config: IO = None): |
| 42 | + """Initializer""" |
| 43 | + |
| 44 | + self.config = yaml.safe_load(config) |
| 45 | + self.resources = self.get_resources() |
| 46 | + |
| 47 | + @abstractmethod |
| 48 | + def get_resources(self): |
| 49 | + raise NotImplementedError() |
| 50 | + |
| 51 | + @abstractmethod |
| 52 | + def is_resource_protected( |
| 53 | + self, path: str, api_prefix: str = None) -> bool: |
| 54 | + |
| 55 | + raise NotImplementedError() |
| 56 | + |
| 57 | + |
| 58 | +def load_auth_provider(factory: str, config_file: str) -> AuthProvider: |
| 59 | + """ |
| 60 | + Load auth provider plugin |
| 61 | +
|
| 62 | + :param factory: `str` of dotted path of Python module/class |
| 63 | + :param configuration: `str` of configuration of auth provider |
| 64 | +
|
| 65 | + :returns: AuthProvider object |
| 66 | + """ |
| 67 | + |
| 68 | + modulename = f'{__package__}.{factory}' |
| 69 | + classname = AUTH_PROVIDERS[factory] |
| 70 | + |
| 71 | + LOGGER.debug(f'module name: {modulename}') |
| 72 | + LOGGER.debug(f'class name: {classname}') |
| 73 | + |
| 74 | + module = importlib.import_module(modulename) |
| 75 | + |
| 76 | + return getattr(module, classname)(config_file) |
| 77 | + |
| 78 | + |
| 79 | +AUTH_PROVIDERS = { |
| 80 | + 'authelia': 'AutheliaAuthProvider' |
| 81 | +} |
0 commit comments