2020-06-13 03:34:01 +02:00
|
|
|
from typing import Any, List, Mapping, Optional
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2013-03-21 20:15:27 +01:00
|
|
|
from django.http import HttpResponse, HttpResponseNotAllowed
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2012-11-06 20:31:53 +01:00
|
|
|
|
2017-07-21 02:19:52 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2016-06-04 20:38:42 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2013-03-21 20:15:27 +01:00
|
|
|
class HttpResponseUnauthorized(HttpResponse):
|
|
|
|
status_code = 401
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def __init__(self, realm: str, www_authenticate: Optional[str] = None) -> None:
|
2013-03-21 20:15:27 +01:00
|
|
|
HttpResponse.__init__(self)
|
2016-05-18 03:42:07 +02:00
|
|
|
if www_authenticate is None:
|
2020-06-10 06:41:04 +02:00
|
|
|
self["WWW-Authenticate"] = f'Basic realm="{realm}"'
|
2016-05-18 03:42:07 +02:00
|
|
|
elif www_authenticate == "session":
|
2020-06-10 06:41:04 +02:00
|
|
|
self["WWW-Authenticate"] = f'Session realm="{realm}"'
|
2016-05-18 03:42:07 +02:00
|
|
|
else:
|
2017-03-05 09:26:07 +01:00
|
|
|
raise AssertionError("Invalid www_authenticate value!")
|
2013-03-21 20:15:27 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def json_unauthorized(
|
|
|
|
message: Optional[str] = None, www_authenticate: Optional[str] = None
|
|
|
|
) -> HttpResponse:
|
2019-09-14 00:58:02 +02:00
|
|
|
if message is None:
|
|
|
|
message = _("Not logged in: API authentication or user session required")
|
2016-05-18 03:42:07 +02:00
|
|
|
resp = HttpResponseUnauthorized("zulip", www_authenticate=www_authenticate)
|
2020-08-07 01:09:47 +02:00
|
|
|
resp.content = orjson.dumps(
|
2021-02-12 08:19:30 +01:00
|
|
|
{"result": "error", "msg": message},
|
|
|
|
option=orjson.OPT_APPEND_NEWLINE,
|
2020-08-07 01:09:47 +02:00
|
|
|
)
|
2013-07-30 23:20:16 +02:00
|
|
|
return resp
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def json_method_not_allowed(methods: List[str]) -> HttpResponseNotAllowed:
|
2013-03-21 20:15:27 +01:00
|
|
|
resp = HttpResponseNotAllowed(methods)
|
2021-02-12 08:19:30 +01:00
|
|
|
resp.content = orjson.dumps(
|
|
|
|
{"result": "error", "msg": "Method Not Allowed", "allowed_methods": methods}
|
|
|
|
)
|
2013-03-21 20:15:27 +01:00
|
|
|
return resp
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def json_response(
|
|
|
|
res_type: str = "success", msg: str = "", data: Mapping[str, Any] = {}, status: int = 200
|
|
|
|
) -> HttpResponse:
|
2012-11-06 20:31:53 +01:00
|
|
|
content = {"result": res_type, "msg": msg}
|
2020-06-13 03:34:01 +02:00
|
|
|
content.update(data)
|
2020-08-07 01:09:47 +02:00
|
|
|
|
|
|
|
# Because we don't pass a default handler, OPT_PASSTHROUGH_DATETIME
|
|
|
|
# actually causes orjson to raise a TypeError on datetime objects. This
|
|
|
|
# helps us avoid relying on the particular serialization used by orjson.
|
|
|
|
return HttpResponse(
|
|
|
|
content=orjson.dumps(
|
|
|
|
content,
|
|
|
|
option=orjson.OPT_APPEND_NEWLINE | orjson.OPT_PASSTHROUGH_DATETIME,
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
content_type="application/json",
|
2020-08-07 01:09:47 +02:00
|
|
|
status=status,
|
|
|
|
)
|
2012-11-06 20:31:53 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def json_success(data: Mapping[str, Any] = {}) -> HttpResponse:
|
2012-11-06 20:31:53 +01:00
|
|
|
return json_response(data=data)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def json_response_from_error(exception: JsonableError) -> HttpResponse:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2017-07-25 22:17:55 +02:00
|
|
|
This should only be needed in middleware; in app code, just raise.
|
|
|
|
|
|
|
|
When app code raises a JsonableError, the JsonErrorHandler
|
|
|
|
middleware takes care of transforming it into a response by
|
|
|
|
calling this function.
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
|
|
|
response = json_response(
|
2021-02-12 08:20:45 +01:00
|
|
|
"error", msg=exception.msg, data=exception.data, status=exception.http_status_code
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-11-27 16:33:01 +01:00
|
|
|
|
|
|
|
for header, value in exception.extra_headers.items():
|
|
|
|
response[header] = value
|
|
|
|
|
|
|
|
return response
|
2017-07-21 02:19:52 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def json_error(msg: str, data: Mapping[str, Any] = {}, status: int = 400) -> HttpResponse:
|
2012-11-06 20:31:53 +01:00
|
|
|
return json_response(res_type="error", msg=msg, data=data, status=status)
|