Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apigateway/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def unauthorized():
This overrides the default behavior or re-directing to a login view
"""
abort(401)

@app.teardown_request
def teardown_request(exception=None):
"""This function will close active transaction, if there is one
Expand Down
1 change: 1 addition & 0 deletions apigateway/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def test_route():

assert "X-api-uid" in request.headers


def test_headers_not_set(self, app):

@app.route("/test_auth_headers_not_set")
Expand Down
5 changes: 5 additions & 0 deletions apigateway/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import jsondiff as jd
import requests
from authlib.oauth2.rfc6749.errors import UnsupportedTokenTypeError
from authlib.integrations.flask_oauth2 import ResourceProtector
from flask import Request, current_app, request
from flask.views import View
Expand Down Expand Up @@ -259,6 +260,10 @@ def _construct_remote_url(self) -> str:
class GatewayResourceProtector(ResourceProtector):
def raise_error_response(self, error):
body = json.dumps(dict({"message": error.description}))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for the special handling is because the Bearer:TOKEN doesn't register as a Bearer token so authlib throws an UnsupportedTokenTypeError and that does not have a description so the body of the error is empty without additional handling.

if type(error)==UnsupportedTokenTypeError:
current_app.logger.info("Token not recognized as a Bearer Token.")
body="Unauthorized. If you are using the API, please confirm your Authorization header is of the form Bearer TOKEN and not Bearer:TOKEN"
raise Oauth2HttpError(error.status_code, error.description, body, error.get_headers())
raise Oauth2HttpError(error.status_code, error.description, body, error.get_headers())


Expand Down