Browse Source

Added select method decode token.

pull/12/head
Marcos Pereira 7 years ago
parent
commit
35cbc21a9d
  1. 4
      README.md
  2. 4
      docs/source/conf.py
  3. 4
      docs/source/index.rst
  4. 34
      keycloak/__init__.py
  5. 2
      setup.py

4
README.md

@ -86,7 +86,7 @@ token_info = keycloak.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KE
# Get permissions by token # Get permissions by token
token = keycloak.token("user", "password") token = keycloak.token("user", "password")
keycloak.load_authorization_config("example-authz-config.json") 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')
``` ```

4
docs/source/conf.py

@ -60,9 +60,9 @@ author = 'Marcos Pereira'
# built documents. # built documents.
# #
# The short X.Y version. # The short X.Y version.
version = '0.7.0'
version = '0.8.0'
# The full version, including alpha/beta/rc tags. # 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 # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.

4
docs/source/index.rst

@ -116,6 +116,6 @@ Main methods::
# Get permissions by token # Get permissions by token
token = keycloak.token("user", "password") token = keycloak.token("user", "password")
keycloak.load_authorization_config("example-authz-config.json") 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')

34
keycloak/__init__.py

@ -98,8 +98,28 @@ class Keycloak:
return payload return payload
def _build_name_role(self, role): def _build_name_role(self, role):
"""
:param role:
:return:
"""
return self.client_id + "/" + role 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): def well_know(self):
""" The most important endpoint to understand is the well-known configuration """ The most important endpoint to understand is the well-known configuration
endpoint. It lists endpoints and other configuration options relevant to endpoint. It lists endpoints and other configuration options relevant to
@ -271,7 +291,7 @@ class Keycloak:
self.authorization.load_config(authorization_json) self.authorization.load_config(authorization_json)
authorization_file.close() authorization_file.close()
def get_policies(self, token):
def get_policies(self, token, method_token_info='instropect', **kwargs):
""" """
Get policies by user token Get policies by user token
@ -284,9 +304,9 @@ class Keycloak:
"Keycloak settings not found. Load Authorization Keycloak settings." "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( raise KeycloakInvalidTokenError(
"Token expired or invalid." "Token expired or invalid."
) )
@ -305,11 +325,13 @@ class Keycloak:
return list(set(policies)) return list(set(policies))
def get_permissions(self, token):
def get_permissions(self, token, method_token_info='instropect', **kwargs):
""" """
Get permission by user token Get permission by user token
:param token: user token :param token: user token
:param method_token_info: Decode token method
:param kwargs: parameters for decode
:return: permissions list :return: permissions list
""" """
@ -318,9 +340,9 @@ class Keycloak:
"Keycloak settings not found. Load Authorization Keycloak settings." "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( raise KeycloakInvalidTokenError(
"Token expired or invalid." "Token expired or invalid."
) )

2
setup.py

@ -4,7 +4,7 @@ from setuptools import setup
setup( setup(
name='python-keycloak', name='python-keycloak',
version='0.7.0',
version='0.8.0',
url='https://github.com/marcospereirampj/python-keycloak', url='https://github.com/marcospereirampj/python-keycloak',
license='GNU General Public License - V3', license='GNU General Public License - V3',
author='Marcos Pereira', author='Marcos Pereira',

Loading…
Cancel
Save