Commit 2078b53e authored by Manuel's avatar Manuel

moved token_service into modules/

parent 1b00f4c9
# global import
from security import security_util
from typing import List
import logging
def _verify(token:str, roles:List[str]=[]):
try:
token_info = security_util.decodeToken(token, roles=roles)
return token_info
except Exception as e:
LOGGER = logging.getLogger(__name__)
LOGGER.error("ERROR DURING TOKEN VALIDATION: "+str(e))
return None
def verifyTokenRegular(token, required_scopes):
return _verify(token)
def verifyTokenAdmin(token, required_scopes):
return _verify(token, roles=["a"])
...@@ -3,9 +3,9 @@ securityDefinitions: ...@@ -3,9 +3,9 @@ securityDefinitions:
type: apiKey type: apiKey
name: Authorization name: Authorization
in: header in: header
x-apikeyInfoFunc: "services.token_service.verifyTokenRegular" x-apikeyInfoFunc: "security_util.verifyTokenRegular"
JwtAdmin: JwtAdmin:
type: apiKey type: apiKey
name: Authorization name: Authorization
in: header in: header
x-apikeyInfoFunc: "services.token_service.verifyTokenAdmin" x-apikeyInfoFunc: "security_util.verifyTokenAdmin"
...@@ -38,7 +38,7 @@ def decodeToken(token: str, roles:List[str]=[]) -> Dict: ...@@ -38,7 +38,7 @@ def decodeToken(token: str, roles:List[str]=[]) -> Dict:
verifies the passed token on the user-microservice and returns a dictionary with the verifies the passed token on the user-microservice and returns a dictionary with the
subject entry if the verification was successful, an error is raised otherwise subject entry if the verification was successful, an error is raised otherwise
@params: @params:
token - Required : JWT token from authorization header, must start with "Bearer " token - Required : JWT token from authorization header, must start with "Bearer "
roles - Optional : User must have at least one of these roles roles - Optional : User must have at least one of these roles
''' '''
...@@ -76,3 +76,18 @@ def decodeToken(token: str, roles:List[str]=[]) -> Dict: ...@@ -76,3 +76,18 @@ def decodeToken(token: str, roles:List[str]=[]) -> Dict:
TokenStash.add(token, data["username"], data["role"]) TokenStash.add(token, data["username"], data["role"])
return {"sub": data["username"], "role": data["role"]} return {"sub": data["username"], "role": data["role"]}
def _verify(token:str, roles:List[str]=[]):
try:
token_info = decodeToken(token, roles=roles)
return token_info
except Exception as e:
print("ERROR DURING TOKEN VALIDATION: "+str(e))
return None
def verifyTokenRegular(token, required_scopes):
return _verify(token)
def verifyTokenAdmin(token, required_scopes):
return _verify(token, roles=["a"])
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment