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.

98 lines
3.5 KiB

7 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. # this software and associated documentation files (the "Software"), to deal in
  9. # the Software without restriction, including without limitation the rights to
  10. # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. # the Software, and to permit persons to whom the Software is furnished to do so,
  12. # subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. import ast
  24. import json
  25. from .permission import Permission
  26. from .policy import Policy
  27. from .role import Role
  28. class Authorization:
  29. """
  30. Keycloak Authorization (policies, roles, scopes and resources).
  31. https://keycloak.gitbooks.io/documentation/authorization_services/index.html
  32. """
  33. def __init__(self):
  34. self._policies = {}
  35. @property
  36. def policies(self):
  37. return self._policies
  38. @policies.setter
  39. def policies(self, value):
  40. self._policies = value
  41. def load_config(self, data):
  42. """
  43. Load policies, roles and permissions (scope/resources).
  44. :param data: keycloak authorization data (dict)
  45. :return:
  46. """
  47. for pol in data["policies"]:
  48. if pol["type"] == "role":
  49. policy = Policy(
  50. name=pol["name"],
  51. type=pol["type"],
  52. logic=pol["logic"],
  53. decision_strategy=pol["decisionStrategy"],
  54. )
  55. config_roles = json.loads(pol["config"]["roles"])
  56. for role in config_roles:
  57. policy.add_role(Role(name=role["id"], required=role["required"]))
  58. self.policies[policy.name] = policy
  59. if pol["type"] == "scope":
  60. permission = Permission(
  61. name=pol["name"],
  62. type=pol["type"],
  63. logic=pol["logic"],
  64. decision_strategy=pol["decisionStrategy"],
  65. )
  66. permission.scopes = ast.literal_eval(pol["config"]["scopes"])
  67. for policy_name in ast.literal_eval(pol["config"]["applyPolicies"]):
  68. self.policies[policy_name].add_permission(permission)
  69. if pol["type"] == "resource":
  70. permission = Permission(
  71. name=pol["name"],
  72. type=pol["type"],
  73. logic=pol["logic"],
  74. decision_strategy=pol["decisionStrategy"],
  75. )
  76. permission.resources = ast.literal_eval(pol["config"].get("resources", "[]"))
  77. for policy_name in ast.literal_eval(pol["config"]["applyPolicies"]):
  78. if self.policies.get(policy_name) is not None:
  79. self.policies[policy_name].add_permission(permission)