2018-07-02 20:03:35 -04:00
|
|
|
from django.http import JsonResponse
|
|
|
|
from lemoncurry.middleware import ResponseException
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
def forbidden() -> ResponseException:
|
2023-08-10 02:52:37 -04:00
|
|
|
return res("forbidden", 403)
|
2018-07-02 20:03:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
def unauthorized() -> ResponseException:
|
2023-08-10 02:52:37 -04:00
|
|
|
return res("unauthorized", 401)
|
2018-07-02 20:03:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
def bad_req(msg: str) -> ResponseException:
|
2023-08-10 02:52:37 -04:00
|
|
|
return res("invalid_request", msg=msg)
|
2018-07-02 20:03:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
def bad_type(type: str) -> ResponseException:
|
2023-08-10 02:52:37 -04:00
|
|
|
msg = "unsupported request type {0}".format(type)
|
|
|
|
return res("invalid_request", 415, msg)
|
2018-07-02 20:03:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
def bad_scope(scope: str) -> ResponseException:
|
2023-08-10 02:52:37 -04:00
|
|
|
return res("insufficient_scope", 401, scope=scope)
|
2018-07-02 20:03:35 -04:00
|
|
|
|
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
def res(
|
|
|
|
error: str,
|
|
|
|
status: Optional[int] = 400,
|
|
|
|
msg: Optional[str] = None,
|
|
|
|
scope: Optional[str] = None,
|
|
|
|
):
|
|
|
|
content = {"error": error}
|
2018-07-02 20:03:35 -04:00
|
|
|
if msg is not None:
|
2023-08-10 02:52:37 -04:00
|
|
|
content["error_description"] = msg
|
2018-07-02 20:03:35 -04:00
|
|
|
if scope:
|
2023-08-10 02:52:37 -04:00
|
|
|
content["scope"] = scope
|
2018-07-02 20:03:35 -04:00
|
|
|
return ResponseException(JsonResponse(content, status=status))
|