From 35cbc21a9d10bb4505fe6a8dfc8df96ff2d9b5b4 Mon Sep 17 00:00:00 2001 From: Marcos Pereira Date: Thu, 24 Aug 2017 09:26:48 -0300 Subject: [PATCH] Added select method decode token. --- README.md | 4 ++-- docs/source/conf.py | 4 ++-- docs/source/index.rst | 4 ++-- keycloak/__init__.py | 34 ++++++++++++++++++++++++++++------ setup.py | 2 +- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3a323ff..1200c4a 100644 --- a/README.md +++ b/README.md @@ -86,7 +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']) +policies = keycloak.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY) +permissions = keycloak.get_permissions(token['access_token'], method_token_info='instrospect') ``` \ No newline at end of file diff --git a/docs/source/conf.py b/docs/source/conf.py index 80c72fa..02829e9 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.7.0' +version = '0.8.0' # The full version, including alpha/beta/rc tags. -release = '0.7.0' +release = '0.8.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 11d3000..1da74f9 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -116,6 +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']) + policies = keycloak.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY) + permissions = keycloak.get_permissions(token['access_token'], method_token_info='instrospect') diff --git a/keycloak/__init__.py b/keycloak/__init__.py index 45d7458..9288e0d 100644 --- a/keycloak/__init__.py +++ b/keycloak/__init__.py @@ -98,8 +98,28 @@ class Keycloak: return payload def _build_name_role(self, role): + """ + + :param role: + :return: + """ return self.client_id + "/" + role + def _token_info(self, token, method_token_info, **kwargs): + """ + + :param token: + :param method_token_info: + :param kwargs: + :return: + """ + if method_token_info == 'instropect': + token_info = self.instropect(token) + else: + token_info = self.decode_token(token, **kwargs) + + return token_info + def well_know(self): """ The most important endpoint to understand is the well-known configuration endpoint. It lists endpoints and other configuration options relevant to @@ -271,7 +291,7 @@ class Keycloak: self.authorization.load_config(authorization_json) authorization_file.close() - def get_policies(self, token): + def get_policies(self, token, method_token_info='instropect', **kwargs): """ Get policies by user token @@ -284,9 +304,9 @@ class Keycloak: "Keycloak settings not found. Load Authorization Keycloak settings." ) - token_info = self.instropect(token) + token_info = self._token_info(token, method_token_info, **kwargs) - if not token_info['active']: + if method_token_info == 'instropect' and not token_info['active']: raise KeycloakInvalidTokenError( "Token expired or invalid." ) @@ -305,11 +325,13 @@ class Keycloak: return list(set(policies)) - def get_permissions(self, token): + def get_permissions(self, token, method_token_info='instropect', **kwargs): """ Get permission by user token :param token: user token + :param method_token_info: Decode token method + :param kwargs: parameters for decode :return: permissions list """ @@ -318,9 +340,9 @@ class Keycloak: "Keycloak settings not found. Load Authorization Keycloak settings." ) - token_info = self.instropect(token) + token_info = self._token_info(token, method_token_info, **kwargs) - if not token_info['active']: + if method_token_info == 'instropect' and not token_info['active']: raise KeycloakInvalidTokenError( "Token expired or invalid." ) diff --git a/setup.py b/setup.py index 08ecd5c..22dd342 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup setup( name='python-keycloak', - version='0.7.0', + version='0.8.0', url='https://github.com/marcospereirampj/python-keycloak', license='GNU General Public License - V3', author='Marcos Pereira',