11import logging
22from typing import (
33 Any ,
4+ Optional ,
45)
56
67from aiohttp import (
1819 NoTokenException ,
1920)
2021
21- ANONYMOUS = "Anonymous"
22-
2322logger = logging .getLogger (__name__ )
2423
2524
@@ -39,19 +38,26 @@ async def orchestrate(request: web.Request) -> web.Response:
3938 return microservice_response
4039
4140
42- async def get_user (request ) -> str :
41+ async def get_user (request : web .Request ) -> Optional [str ]:
42+ """Get The user identifier if it is available.
43+
44+ :param request: The external request.
45+ :return: An string value containing the user identifier or ``None`` if no user information is available.
46+ """
47+ auth = request .app ["config" ].rest .auth
48+ if auth is None or not auth .enabled :
49+ return None
50+
4351 try :
4452 await get_token (request )
4553 except NoTokenException :
46- return ANONYMOUS
47- else :
48- try :
49- original_headers = dict (request .headers .copy ())
50- user_uuid = await authenticate ("localhost" , "8082" , "POST" , "token" , original_headers )
51- except InvalidAuthenticationException :
52- return ANONYMOUS
53- else :
54- return user_uuid
54+ return None
55+
56+ try :
57+ original_headers = dict (request .headers .copy ())
58+ return await authenticate (auth .host , auth .port , auth .method , auth .path , original_headers )
59+ except InvalidAuthenticationException :
60+ return None
5561
5662
5763async def discover (host : str , port : int , path : str , verb : str , endpoint : str ) -> dict [str , Any ]:
@@ -81,7 +87,7 @@ async def discover(host: str, port: int, path: str, verb: str, endpoint: str) ->
8187
8288
8389# noinspection PyUnusedLocal
84- async def call (address : str , port : int , original_req : web .Request , user : str , ** kwargs ) -> web .Response :
90+ async def call (address : str , port : int , original_req : web .Request , user : Optional [ str ] , ** kwargs ) -> web .Response :
8591 """Call microservice (redirect the original call)
8692
8793 :param address: The ip of the microservices.
@@ -93,7 +99,11 @@ async def call(address: str, port: int, original_req: web.Request, user: str, **
9399 """
94100
95101 headers = original_req .headers .copy ()
96- headers ["User" ] = user
102+ if user is not None :
103+ headers ["User" ] = user
104+ else : # Enforce that the 'User' entry is only generated by the auth system.
105+ # noinspection PyTypeChecker
106+ headers .pop ("User" , None )
97107
98108 url = original_req .url .with_scheme ("http" ).with_host (address ).with_port (port )
99109 method = original_req .method
@@ -116,8 +126,8 @@ async def _clone_response(response: ClientResponse) -> web.Response:
116126 )
117127
118128
119- async def authenticate (address : str , port : str , method : str , path : str , authorization_headers : dict [str , str ]) -> str :
120- authentication_url = URL (f"http://{ address } :{ port } / { path } " )
129+ async def authenticate (host : str , port : str , method : str , path : str , authorization_headers : dict [str , str ]) -> str :
130+ authentication_url = URL (f"http://{ host } :{ port } { path } " )
121131 authentication_method = method
122132 logger .info ("Authenticating request..." )
123133
0 commit comments