X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2common%2Fauthmw%2Fauthmiddleware.py;h=31412633379d1c1cb116b8662794db50e5234f09;hb=81a50cbca7ff0b0535463ed4aa21cf5d819d5cd7;hp=cd9df4b3ca80e10bc3717de158f47b18d615e9ca;hpb=7762dca97d4714f79b723e6d3759e954793fb33f;p=pti%2Fo2.git diff --git a/o2common/authmw/authmiddleware.py b/o2common/authmw/authmiddleware.py index cd9df4b..3141263 100644 --- a/o2common/authmw/authmiddleware.py +++ b/o2common/authmw/authmiddleware.py @@ -84,8 +84,9 @@ class authmiddleware(): self.app = app def __call__(self, environ, start_response): - logger.info(__name__ + 'authentication middleware') + logger.debug(__name__ + 'authentication middleware') req = Request(environ, populate_request=True, shallow=True) + auth_token = None try: auth_header = req.headers.get('Authorization', None) if auth_header: @@ -95,7 +96,7 @@ class authmiddleware(): # invoke underlying auth mdw to make k8s/keystone api ret = ad.authenticate(auth_token) if ret is True: - logger.info( + logger.debug( "auth success with oauth token: " + auth_token) try: return self.app(environ, start_response) @@ -123,9 +124,16 @@ class authmiddleware(): return _response_wrapper(environ, start_response, ex.dictize(), prb.serialize()) except Exception as ex: - logger.error('Internal exception happended {}'.format( - str(ex)), exc_info=True) - prb = AuthProblemDetails(500, 'Internal error.', req.path) - return \ - _internal_err_response_wrapper(environ, - start_response, prb.serialize()) + if auth_token: + logger.error('Internal exception happended {}'.format( + str(ex)), exc_info=True) + prb = AuthProblemDetails(500, 'Internal error.', req.path) + return \ + _internal_err_response_wrapper( + environ, start_response, prb.serialize()) + else: + logger.debug('Auth token missing or not obtained.') + ex = AuthRequiredExp('Bearer realm="Authentication Required"') + prb = AuthProblemDetails(401, ex.value, req.path) + return _response_wrapper(environ, start_response, + ex.dictize(), prb.serialize())