feat: JSON HTTP errors on the OAuth endpoints

This commit is contained in:
Éloi Rivard 2024-12-22 11:50:37 +01:00
parent 7493ce3c35
commit b7bb14cfe7
No known key found for this signature in database
GPG key ID: 7EDA204EA57DD184
2 changed files with 16 additions and 0 deletions

View file

@ -15,6 +15,7 @@ from flask import request
from flask import session from flask import session
from flask import url_for from flask import url_for
from werkzeug.datastructures import CombinedMultiDict from werkzeug.datastructures import CombinedMultiDict
from werkzeug.exceptions import HTTPException
from canaille import csrf from canaille import csrf
from canaille.app import models from canaille.app import models
@ -42,6 +43,14 @@ from .well_known import openid_configuration
bp = Blueprint("endpoints", __name__, url_prefix="/oauth") 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"]) @bp.route("/authorize", methods=["GET", "POST"])
def authorize(): def authorize():
current_app.logger.debug( current_app.logger.debug(

View file

@ -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.",
}