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.

113 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 use to
  30. protect your resources. With policies, you can implement strategies for attribute-based access control
  31. (ABAC), role-based access control (RBAC), context-based access control, or any combination of these.
  32. https://keycloak.gitbooks.io/documentation/authorization_services/topics/policy/overview.html
  33. """
  34. def __init__(self, name, type, logic, decision_strategy):
  35. self._name = name
  36. self._type = type
  37. self._logic = logic
  38. self._decision_strategy = decision_strategy
  39. self._roles = []
  40. self._permissions = []
  41. def __repr__(self):
  42. return "<Policy: %s (%s)>" % (self.name, self.type)
  43. def __str__(self):
  44. return "Policy: %s (%s)" % (self.name, self.type)
  45. @property
  46. def name(self):
  47. return self._name
  48. @name.setter
  49. def name(self, value):
  50. self._name = value
  51. @property
  52. def type(self):
  53. return self._type
  54. @type.setter
  55. def type(self, value):
  56. self._type = value
  57. @property
  58. def logic(self):
  59. return self._logic
  60. @logic.setter
  61. def logic(self, value):
  62. self._logic = value
  63. @property
  64. def decision_strategy(self):
  65. return self._decision_strategy
  66. @decision_strategy.setter
  67. def decision_strategy(self, value):
  68. self._decision_strategy = value
  69. @property
  70. def roles(self):
  71. return self._roles
  72. @property
  73. def permissions(self):
  74. return self._permissions
  75. def add_role(self, role):
  76. """
  77. Add keycloak role in policy.
  78. :param role: keycloak role.
  79. :return:
  80. """
  81. if self.type != 'role':
  82. raise KeycloakAuthorizationConfigError(
  83. "Can't add role. Policy type is different of role")
  84. self._roles.append(role)
  85. def add_permission(self, permission):
  86. """
  87. Add keycloak permission in policy.
  88. :param permission: keycloak permission.
  89. :return:
  90. """
  91. self._permissions.append(permission)