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.

115 lines
3.6 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. from ..exceptions import KeycloakAuthorizationConfigError
  24. class Policy:
  25. """
  26. A policy defines the conditions that must be satisfied to grant access to an object.
  27. Unlike permissions, you do not specify the object being protected but rather the conditions
  28. that must be satisfied for access to a given object (for example, resource, scope, or both).
  29. Policies are strongly related to the different access control mechanisms (ACMs) that you can
  30. use to protect your resources. With policies, you can implement strategies for attribute-based
  31. access control (ABAC), role-based access control (RBAC), context-based access control, or any
  32. combination of these.
  33. https://keycloak.gitbooks.io/documentation/authorization_services/topics/policy/overview.html
  34. """
  35. def __init__(self, name, type, logic, decision_strategy):
  36. self._name = name
  37. self._type = type
  38. self._logic = logic
  39. self._decision_strategy = decision_strategy
  40. self._roles = []
  41. self._permissions = []
  42. def __repr__(self):
  43. return "<Policy: %s (%s)>" % (self.name, self.type)
  44. def __str__(self):
  45. return "Policy: %s (%s)" % (self.name, self.type)
  46. @property
  47. def name(self):
  48. return self._name
  49. @name.setter
  50. def name(self, value):
  51. self._name = value
  52. @property
  53. def type(self):
  54. return self._type
  55. @type.setter
  56. def type(self, value):
  57. self._type = value
  58. @property
  59. def logic(self):
  60. return self._logic
  61. @logic.setter
  62. def logic(self, value):
  63. self._logic = value
  64. @property
  65. def decision_strategy(self):
  66. return self._decision_strategy
  67. @decision_strategy.setter
  68. def decision_strategy(self, value):
  69. self._decision_strategy = value
  70. @property
  71. def roles(self):
  72. return self._roles
  73. @property
  74. def permissions(self):
  75. return self._permissions
  76. def add_role(self, role):
  77. """
  78. Add keycloak role in policy.
  79. :param role: keycloak role.
  80. :return:
  81. """
  82. if self.type != "role":
  83. raise KeycloakAuthorizationConfigError(
  84. "Can't add role. Policy type is different of role"
  85. )
  86. self._roles.append(role)
  87. def add_permission(self, permission):
  88. """
  89. Add keycloak permission in policy.
  90. :param permission: keycloak permission.
  91. :return:
  92. """
  93. self._permissions.append(permission)