You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import ast
  18. import json
  19. from keycloak.authorization.permission import Permission
  20. from keycloak.authorization.policy import Policy
  21. from keycloak.authorization.role import Role
  22. class Authorization:
  23. def __init__(self):
  24. self._policies = {}
  25. @property
  26. def policies(self):
  27. return self._policies
  28. @policies.setter
  29. def policies(self, value):
  30. self._policies = value
  31. def load_config(self, data):
  32. """
  33. :param data:
  34. :return:
  35. """
  36. for pol in data['policies']:
  37. if pol['type'] == 'role':
  38. policy = Policy(name=pol['name'],
  39. type=pol['type'],
  40. logic=pol['logic'],
  41. decision_strategy=pol['decisionStrategy'])
  42. config_roles = json.loads(pol['config']['roles'])
  43. for role in config_roles:
  44. policy.add_role(Role(name=role['id'],
  45. required=role['required']))
  46. self.policies[policy.name] = policy
  47. if pol['type'] == 'scope':
  48. permission = Permission(name=pol['name'],
  49. type=pol['type'],
  50. logic=pol['logic'],
  51. decision_strategy=pol['decisionStrategy'])
  52. permission.scopes = ast.literal_eval(pol['config']['scopes'])
  53. for policy_name in ast.literal_eval(pol['config']['applyPolicies']):
  54. self.policies[policy_name].add_permission(permission)
  55. if pol['type'] == 'resource':
  56. permission = Permission(name=pol['name'],
  57. type=pol['type'],
  58. logic=pol['logic'],
  59. decision_strategy=pol['decisionStrategy'])
  60. permission.resources = ast.literal_eval(pol['config']['resources'])
  61. for policy_name in ast.literal_eval(pol['config']['applyPolicies']):
  62. self.policies[policy_name].add_permission(permission)