From 2a19155fab1c72b664a08b6e29b5a21d8e7a3d4b Mon Sep 17 00:00:00 2001 From: Marcos Pereira Date: Wed, 23 Aug 2017 16:50:34 -0300 Subject: [PATCH] Added feature get_policies. --- README.md | 1 + docs/source/conf.py | 4 ++-- docs/source/index.rst | 1 + keycloak/__init__.py | 34 ++++++++++++++++++++++++++++++++++ setup.py | 2 +- 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a11a985..3a323ff 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ token_info = keycloak.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KE # Get permissions by token token = keycloak.token("user", "password") keycloak.load_authorization_config("example-authz-config.json") +policies = keycloak.get_policies(token['access_token']) permissions = keycloak.get_permissions(token['access_token']) ``` \ No newline at end of file diff --git a/docs/source/conf.py b/docs/source/conf.py index 4114863..80c72fa 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -60,9 +60,9 @@ author = 'Marcos Pereira' # built documents. # # The short X.Y version. -version = '0.6.5' +version = '0.7.0' # The full version, including alpha/beta/rc tags. -release = '0.6.5' +release = '0.7.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/source/index.rst b/docs/source/index.rst index a785943..11d3000 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -116,5 +116,6 @@ Main methods:: # Get permissions by token token = keycloak.token("user", "password") keycloak.load_authorization_config("example-authz-config.json") + policies = keycloak.get_policies(token['access_token']) permissions = keycloak.get_permissions(token['access_token']) diff --git a/keycloak/__init__.py b/keycloak/__init__.py index be8a5b8..45d7458 100644 --- a/keycloak/__init__.py +++ b/keycloak/__init__.py @@ -271,6 +271,40 @@ class Keycloak: self.authorization.load_config(authorization_json) authorization_file.close() + def get_policies(self, token): + """ + Get policies by user token + + :param token: user token + :return: policies list + """ + + if not self.authorization.policies: + raise KeycloakAuthorizationConfigError( + "Keycloak settings not found. Load Authorization Keycloak settings." + ) + + token_info = self.instropect(token) + + if not token_info['active']: + raise KeycloakInvalidTokenError( + "Token expired or invalid." + ) + + user_resources = token_info['resource_access'].get(self.client_id) + + if not user_resources: + return None + + policies = [] + + for policy_name, policy in self.authorization.policies.items(): + for role in user_resources['roles']: + if self._build_name_role(role) in policy.roles: + policies.append(policy) + + return list(set(policies)) + def get_permissions(self, token): """ Get permission by user token diff --git a/setup.py b/setup.py index 13c84cd..08ecd5c 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup setup( name='python-keycloak', - version='0.6.5', + version='0.7.0', url='https://github.com/marcospereirampj/python-keycloak', license='GNU General Public License - V3', author='Marcos Pereira',