diff --git a/canaille/oidc/endpoints/oauth.py b/canaille/oidc/endpoints/oauth.py index f87616f1..94e1a4da 100644 --- a/canaille/oidc/endpoints/oauth.py +++ b/canaille/oidc/endpoints/oauth.py @@ -15,6 +15,7 @@ from flask import request from flask import session from flask import url_for from werkzeug.datastructures import CombinedMultiDict +from werkzeug.exceptions import HTTPException from canaille import csrf from canaille.app import models @@ -42,6 +43,14 @@ from .well_known import openid_configuration bp = Blueprint("endpoints", __name__, url_prefix="/oauth") +@bp.errorhandler(HTTPException) +def http_error_handler(error): + return { + "error": error.name.lower().replace(" ", "_"), + "error_description": error.description, + }, error.code + + @bp.route("/authorize", methods=["GET", "POST"]) def authorize(): current_app.logger.debug( diff --git a/tests/oidc/test_errors.py b/tests/oidc/test_errors.py new file mode 100644 index 00000000..aa86d9a0 --- /dev/null +++ b/tests/oidc/test_errors.py @@ -0,0 +1,7 @@ +def test_json_oauth_errors(testclient): + """Checks that HTTP errors on the oauth endpoints are in the JSON format.""" + res = testclient.get("/oauth/invalid", status=404) + assert res.json == { + "error": "not_found", + "error_description": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.", + }