Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Validate client/account/deactivate
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Jul 5, 2022
1 parent 52b0ef3 commit 3ea5f1c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
32 changes: 17 additions & 15 deletions synapse/rest/client/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
# limitations under the License.
import logging
import random
from http import HTTPStatus
from typing import TYPE_CHECKING, Optional, Tuple
from urllib.parse import urlparse

from pydantic import BaseModel, StrictBool, StrictStr

from twisted.web.server import Request

from synapse.api.constants import LoginType
Expand All @@ -34,12 +35,14 @@
from synapse.http.servlet import (
RestServlet,
assert_params_in_dict,
parse_and_validate_json_object_from_request,
parse_json_object_from_request,
parse_string,
)
from synapse.http.site import SynapseRequest
from synapse.metrics import threepid_send_requests
from synapse.push.mailer import Mailer
from synapse.rest.client.models import AuthenticationData
from synapse.types import JsonDict
from synapse.util.msisdn import phone_number_to_msisdn
from synapse.util.stringutils import assert_valid_client_secret, random_string
Expand Down Expand Up @@ -289,6 +292,13 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
return 200, {}


class DeactivateAccountBody(BaseModel):
auth: Optional[AuthenticationData] = None
id_server: Optional[StrictStr] = None
# Not specced, see https://github.com/matrix-org/matrix-spec/issues/297
erase: StrictBool = False


class DeactivateAccountRestServlet(RestServlet):
PATTERNS = client_patterns("/account/deactivate$")

Expand All @@ -301,35 +311,27 @@ def __init__(self, hs: "HomeServer"):

@interactive_auth_handler
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
body = parse_json_object_from_request(request)
erase = body.get("erase", False)
if not isinstance(erase, bool):
raise SynapseError(
HTTPStatus.BAD_REQUEST,
"Param 'erase' must be a boolean, if given",
Codes.BAD_JSON,
)
body = parse_and_validate_json_object_from_request(
request, DeactivateAccountBody
)

requester = await self.auth.get_user_by_req(request)

# allow ASes to deactivate their own users
if requester.app_service:
await self._deactivate_account_handler.deactivate_account(
requester.user.to_string(), erase, requester
requester.user.to_string(), body.erase, requester
)
return 200, {}

await self.auth_handler.validate_user_via_ui_auth(
requester,
request,
body,
body.dict(),
"deactivate your account",
)
result = await self._deactivate_account_handler.deactivate_account(
requester.user.to_string(),
erase,
requester,
id_server=body.get("id_server"),
requester.user.to_string(), body.erase, requester, id_server=body.id_server
)
if result:
id_server_unbind_result = "success"
Expand Down
24 changes: 24 additions & 0 deletions synapse/rest/client/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2022 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional

from pydantic import BaseModel, StrictStr, Extra


class AuthenticationData(BaseModel):
class Config:
extra = Extra.allow

session: Optional[StrictStr] = None
type: Optional[StrictStr] = None
2 changes: 1 addition & 1 deletion tests/rest/client/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def deactivate(self, user_id: str, tok: str) -> None:
channel = self.make_request(
"POST", "account/deactivate", request_data, access_token=tok
)
self.assertEqual(channel.code, 200)
self.assertEqual(channel.code, 200, channel.json_body)


class WhoamiTestCase(unittest.HomeserverTestCase):
Expand Down

0 comments on commit 3ea5f1c

Please sign in to comment.