_auth_config_checker.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from ..._config import _Config
  12. from ..issue_collector import IssueCollector
  13. from ._config_checker import _ConfigChecker
  14. class _AuthConfigChecker(_ConfigChecker):
  15. def __init__(self, config: _Config, collector: IssueCollector):
  16. super().__init__(config, collector)
  17. def _check(self) -> IssueCollector:
  18. auth_config = self._config._auth_config # type: ignore
  19. self._check_predefined_protocol(auth_config)
  20. return self._collector
  21. def _check_predefined_protocol(self, auth_config):
  22. if auth_config.protocol == auth_config._PROTOCOL_LDAP:
  23. self.__check_ldap(auth_config)
  24. if auth_config.protocol == auth_config._PROTOCOL_TAIPY:
  25. self.__check_taipy(auth_config)
  26. def __check_taipy(self, auth_config):
  27. if auth_config._TAIPY_ROLES not in auth_config.properties:
  28. self._error(
  29. "properties",
  30. auth_config._LDAP_SERVER,
  31. f"`{auth_config._LDAP_SERVER}` property must be populated when {auth_config._PROTOCOL_LDAP} is used.",
  32. )
  33. if auth_config._TAIPY_PWD not in auth_config.properties:
  34. self._warning(
  35. "properties",
  36. auth_config._TAIPY_PWD,
  37. f"`In order to protect authentication with passwords using {auth_config._PROTOCOL_TAIPY} protocol,"
  38. f" {auth_config._TAIPY_PWD}` property can be populated.",
  39. )
  40. def __check_ldap(self, auth_config):
  41. if auth_config._LDAP_SERVER not in auth_config.properties:
  42. self._error(
  43. "properties",
  44. auth_config._LDAP_SERVER,
  45. f"`{auth_config._LDAP_SERVER}` attribute must be populated when {auth_config._PROTOCOL_LDAP} is used.",
  46. )
  47. if auth_config._LDAP_BASE_DN not in auth_config.properties:
  48. self._error(
  49. "properties",
  50. auth_config._LDAP_BASE_DN,
  51. f"`{auth_config._LDAP_BASE_DN}` field must be populated when {auth_config._PROTOCOL_LDAP} is used.",
  52. )