X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2common%2Fauthmw%2Fauthmiddleware.py;h=032e735dc253bd2c4a687e715902a3160fe9924e;hb=242185955154b1d709c2fdbce5445e4aae8058ac;hp=c70adfcf7dc8a88785f572be542eb6728205a0aa;hpb=5601b5899b0fd15748ae0474de9f5f6dda72864c;p=pti%2Fo2.git diff --git a/o2common/authmw/authmiddleware.py b/o2common/authmw/authmiddleware.py index c70adfc..032e735 100644 --- a/o2common/authmw/authmiddleware.py +++ b/o2common/authmw/authmiddleware.py @@ -43,6 +43,11 @@ def _response_wrapper(environ, start_response, header): return res(environ, start_response) +def _internal_err_response_wrapper(environ, start_response): + res = Response(mimetype='text/plain', status=500) + return res(environ, start_response) + + class authmiddleware(): ''' @@ -56,8 +61,7 @@ class authmiddleware(): logger.info(__name__ + 'authentication middleware') req = Request(environ, populate_request=True, shallow=True) try: - auth_header = req.headers['Authorization'] - + auth_header = req.headers.get('Authorization', None) if auth_header: auth_token = auth_header.split(" ")[1] @@ -67,7 +71,15 @@ class authmiddleware(): if ret is True: logger.info( "auth success with oauth token: " + auth_token) - return self.app(environ, start_response) + try: + return self.app(environ, start_response) + except Exception as ex: + logger.error( + 'Internal exception happend \ + ed {}'.format(str(ex)), exc_info=True) + return \ + _internal_err_response_wrapper(environ, + start_response) else: raise AuthFailureExp( 'Bearer realm="Authentication Failed"') @@ -77,7 +89,7 @@ class authmiddleware(): return _response_wrapper(environ, start_response, ex.dictize()) except AuthFailureExp as ex: return _response_wrapper(environ, start_response, ex.dictize()) - except Exception: - hint = 'Bearer realm="Authentication Required"' - return _response_wrapper(environ, start_response, - AuthRequiredExp(hint).dictize()) + except Exception as ex: + logger.error('Internal exception happended {}'.format( + str(ex)), exc_info=True) + return _internal_err_response_wrapper(environ, start_response)