You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.5 KiB

  1. # maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Dict
  17. import json
  18. from aiohttp import web
  19. class _ErrorMeta:
  20. def __init__(self, *args, **kwargs) -> None:
  21. pass
  22. @staticmethod
  23. def _make_error(errcode: str, error: str) -> Dict[str, str]:
  24. return {
  25. "body": json.dumps({
  26. "error": error,
  27. "errcode": errcode,
  28. }).encode("utf-8"),
  29. "content_type": "application/json",
  30. "headers": {
  31. "Access-Control-Allow-Origin": "*",
  32. "Access-Control-Allow-Methods": "OPTIONS, GET, POST, PUT, DELETE, HEAD",
  33. "Access-Control-Allow-Headers": "Authorization, Content-Type",
  34. }
  35. }
  36. @property
  37. def request_not_json(self) -> web.HTTPException:
  38. return web.HTTPBadRequest(**self._make_error("M_NOT_JSON",
  39. "Request body is not valid JSON"))
  40. @property
  41. def missing_auth_header(self) -> web.HTTPException:
  42. return web.HTTPForbidden(**self._make_error("M_MISSING_TOKEN",
  43. "Missing authorization header"))
  44. @property
  45. def missing_user_id_header(self) -> web.HTTPException:
  46. return web.HTTPForbidden(**self._make_error("NET.MAUNIUM_MISSING_USER_ID",
  47. "Missing user ID header"))
  48. @property
  49. def user_not_found(self) -> web.HTTPException:
  50. return web.HTTPNotFound(**self._make_error("NET.MAUNIUM_USER_NOT_FOUND",
  51. "User not found"))
  52. @property
  53. def invalid_auth_header(self) -> web.HTTPException:
  54. return web.HTTPForbidden(**self._make_error("M_UNKNOWN_TOKEN",
  55. "Invalid authorization header"))
  56. @property
  57. def invalid_auth_token(self) -> web.HTTPException:
  58. return web.HTTPForbidden(**self._make_error("M_UNKNOWN_TOKEN",
  59. "Invalid authorization token"))
  60. @property
  61. def auth_token_expired(self) -> web.HTTPException:
  62. return web.HTTPForbidden(**self._make_error("NET.MAUNIUM_TOKEN_EXPIRED",
  63. "Authorization token has expired"))
  64. @property
  65. def invalid_openid_payload(self) -> web.HTTPException:
  66. return web.HTTPBadRequest(**self._make_error("M_BAD_JSON", "Missing one or more "
  67. "fields in OpenID payload"))
  68. @property
  69. def invalid_openid_token(self) -> web.HTTPException:
  70. return web.HTTPForbidden(**self._make_error("M_UNKNOWN_TOKEN",
  71. "Invalid OpenID token"))
  72. @property
  73. def no_access(self) -> web.HTTPException:
  74. return web.HTTPUnauthorized(**self._make_error(
  75. "M_UNAUTHORIZED",
  76. "You are not authorized to access this maunium-stickerpicker instance"))
  77. @property
  78. def homeserver_mismatch(self) -> web.HTTPException:
  79. return web.HTTPUnauthorized(**self._make_error(
  80. "M_UNAUTHORIZED", "Request matrix_server_name and OpenID sub homeserver don't match"))
  81. @property
  82. def pack_not_found(self) -> web.HTTPException:
  83. return web.HTTPNotFound(**self._make_error("NET.MAUNIUM_PACK_NOT_FOUND",
  84. "Sticker pack not found"))
  85. @property
  86. def client_well_known_error(self) -> web.HTTPException:
  87. return web.HTTPForbidden(**self._make_error("NET.MAUNIUM_CLIENT_WELL_KNOWN_ERROR",
  88. "Failed to resolve homeserver URL "
  89. "from client .well-known"))
  90. class Error(metaclass=_ErrorMeta):
  91. pass