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.

185 lines
5.1 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. """Keycloak authorization Policy module."""
  24. from ..exceptions import KeycloakAuthorizationConfigError
  25. class Policy:
  26. """Base policy class.
  27. A policy defines the conditions that must be satisfied to grant access to an object.
  28. Unlike permissions, you do not specify the object being protected but rather the conditions
  29. that must be satisfied for access to a given object (for example, resource, scope, or both).
  30. Policies are strongly related to the different access control mechanisms (ACMs) that you can
  31. use to protect your resources. With policies, you can implement strategies for attribute-based
  32. access control (ABAC), role-based access control (RBAC), context-based access control, or any
  33. combination of these.
  34. https://keycloak.gitbooks.io/documentation/authorization_services/topics/policy/overview.html
  35. :param name: Name
  36. :type name: str
  37. :param type: Type
  38. :type type: str
  39. :param logic: Logic
  40. :type logic: str
  41. :param decision_strategy: Decision strategy
  42. :type decision_strategy: str
  43. """
  44. def __init__(self, name, type, logic, decision_strategy):
  45. """Init method.
  46. :param name: Name
  47. :type name: str
  48. :param type: Type
  49. :type type: str
  50. :param logic: Logic
  51. :type logic: str
  52. :param decision_strategy: Decision strategy
  53. :type decision_strategy: str
  54. """
  55. self.name = name
  56. self.type = type
  57. self.logic = logic
  58. self.decision_strategy = decision_strategy
  59. self.roles = []
  60. self.permissions = []
  61. def __repr__(self):
  62. """Repr method.
  63. :returns: Class representation
  64. :rtype: str
  65. """
  66. return "<Policy: %s (%s)>" % (self.name, self.type)
  67. def __str__(self):
  68. """Str method.
  69. :returns: Class string representation
  70. :rtype: str
  71. """
  72. return "Policy: %s (%s)" % (self.name, self.type)
  73. @property
  74. def name(self):
  75. """Get name.
  76. :returns: Name
  77. :rtype: str
  78. """
  79. return self._name
  80. @name.setter
  81. def name(self, value):
  82. self._name = value
  83. @property
  84. def type(self):
  85. """Get type.
  86. :returns: Type
  87. :rtype: str
  88. """
  89. return self._type
  90. @type.setter
  91. def type(self, value):
  92. self._type = value
  93. @property
  94. def logic(self):
  95. """Get logic.
  96. :returns: Logic
  97. :rtype: str
  98. """
  99. return self._logic
  100. @logic.setter
  101. def logic(self, value):
  102. self._logic = value
  103. @property
  104. def decision_strategy(self):
  105. """Get decision strategy.
  106. :returns: Decision strategy
  107. :rtype: str
  108. """
  109. return self._decision_strategy
  110. @decision_strategy.setter
  111. def decision_strategy(self, value):
  112. self._decision_strategy = value
  113. @property
  114. def roles(self):
  115. """Get roles.
  116. :returns: Roles
  117. :rtype: list
  118. """
  119. return self._roles
  120. @roles.setter
  121. def roles(self, value):
  122. self._roles = value
  123. @property
  124. def permissions(self):
  125. """Get permissions.
  126. :returns: Permissions
  127. :rtype: list
  128. """
  129. return self._permissions
  130. @permissions.setter
  131. def permissions(self, value):
  132. self._permissions = value
  133. def add_role(self, role):
  134. """Add keycloak role in policy.
  135. :param role: Keycloak role
  136. :type role: keycloak.authorization.Role
  137. :raises KeycloakAuthorizationConfigError: In case of misconfigured policy type
  138. """
  139. if self.type != "role":
  140. raise KeycloakAuthorizationConfigError(
  141. "Can't add role. Policy type is different of role"
  142. )
  143. self._roles.append(role)
  144. def add_permission(self, permission):
  145. """Add keycloak permission in policy.
  146. :param permission: Keycloak permission
  147. :type permission: keycloak.authorization.Permission
  148. """
  149. self._permissions.append(permission)