Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

matrix-org / synapse / 4532

23 Sep 2019 - 19:39 coverage decreased (-49.7%) to 17.596%
4532

Pull #6079

buildkite

Richard van der Hoff
update changelog
Pull Request #6079: Add submit_url response parameter to msisdn /requestToken

359 of 12986 branches covered (2.76%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 1 file covered. (0.0%)

18869 existing lines in 281 files now uncovered.

8809 of 39116 relevant lines covered (22.52%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.05
/synapse/rest/admin/__init__.py
1
# -*- coding: utf-8 -*-
2
# Copyright 2014-2016 OpenMarket Ltd
3
# Copyright 2018-2019 New Vector Ltd
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16

17
import hashlib
1×
18
import hmac
1×
19
import logging
1×
20
import platform
1×
21
import re
1×
22

23
from six import text_type
1×
24
from six.moves import http_client
1×
25

26
from twisted.internet import defer
1×
27

28
import synapse
1×
29
from synapse.api.constants import Membership, UserTypes
1×
30
from synapse.api.errors import Codes, NotFoundError, SynapseError
1×
31
from synapse.http.server import JsonResource
1×
32
from synapse.http.servlet import (
1×
33
    RestServlet,
34
    assert_params_in_dict,
35
    parse_integer,
36
    parse_json_object_from_request,
37
    parse_string,
38
)
39
from synapse.rest.admin._base import (
1×
40
    assert_requester_is_admin,
41
    assert_user_is_admin,
42
    historical_admin_path_patterns,
43
)
44
from synapse.rest.admin.media import ListMediaInRoom, register_servlets_for_media_repo
1×
45
from synapse.rest.admin.purge_room_servlet import PurgeRoomServlet
1×
46
from synapse.rest.admin.server_notice_servlet import SendServerNoticeServlet
1×
47
from synapse.rest.admin.users import UserAdminServlet
1×
48
from synapse.types import UserID, create_requester
1×
49
from synapse.util.versionstring import get_version_string
1×
50

51
logger = logging.getLogger(__name__)
1×
52

53

54
class UsersRestServlet(RestServlet):
1×
55
    PATTERNS = historical_admin_path_patterns("/users/(?P<user_id>[^/]*)$")
1×
56

57
    def __init__(self, hs):
1×
UNCOV
58
        self.hs = hs
!
UNCOV
59
        self.auth = hs.get_auth()
!
UNCOV
60
        self.handlers = hs.get_handlers()
!
61

62
    @defer.inlineCallbacks
1×
63
    def on_GET(self, request, user_id):
64
        target_user = UserID.from_string(user_id)
!
65
        yield assert_requester_is_admin(self.auth, request)
!
66

67
        if not self.hs.is_mine(target_user):
Branches [[0, 68], [0, 70]] missed. !
68
            raise SynapseError(400, "Can only users a local user")
!
69

70
        ret = yield self.handlers.admin_handler.get_users()
!
71

72
        return 200, ret
!
73

74

75
class VersionServlet(RestServlet):
1×
76
    PATTERNS = (re.compile("^/_synapse/admin/v1/server_version$"),)
1×
77

78
    def __init__(self, hs):
1×
UNCOV
79
        self.res = {
!
80
            "server_version": get_version_string(synapse),
81
            "python_version": platform.python_version(),
82
        }
83

84
    def on_GET(self, request):
1×
85
        return 200, self.res
!
86

87

88
class UserRegisterServlet(RestServlet):
1×
89
    """
90
    Attributes:
91
         NONCE_TIMEOUT (int): Seconds until a generated nonce won't be accepted
92
         nonces (dict[str, int]): The nonces that we will accept. A dict of
93
             nonce to the time it was generated, in int seconds.
94
    """
95

96
    PATTERNS = historical_admin_path_patterns("/register")
1×
97
    NONCE_TIMEOUT = 60
1×
98

99
    def __init__(self, hs):
1×
UNCOV
100
        self.handlers = hs.get_handlers()
!
UNCOV
101
        self.reactor = hs.get_reactor()
!
UNCOV
102
        self.nonces = {}
!
UNCOV
103
        self.hs = hs
!
104

105
    def _clear_old_nonces(self):
1×
106
        """
107
        Clear out old nonces that are older than NONCE_TIMEOUT.
108
        """
UNCOV
109
        now = int(self.reactor.seconds())
!
110

UNCOV
111
        for k, v in list(self.nonces.items()):
Branches [[0, 105], [0, 112]] missed. !
UNCOV
112
            if now - v > self.NONCE_TIMEOUT:
Branches [[0, 111], [0, 113]] missed. !
113
                del self.nonces[k]
!
114

115
    def on_GET(self, request):
1×
116
        """
117
        Generate a new nonce.
118
        """
UNCOV
119
        self._clear_old_nonces()
!
120

UNCOV
121
        nonce = self.hs.get_secrets().token_hex(64)
!
UNCOV
122
        self.nonces[nonce] = int(self.reactor.seconds())
!
UNCOV
123
        return 200, {"nonce": nonce}
!
124

125
    @defer.inlineCallbacks
1×
126
    def on_POST(self, request):
UNCOV
127
        self._clear_old_nonces()
!
128

UNCOV
129
        if not self.hs.config.registration_shared_secret:
Branches [[0, 130], [0, 132]] missed. !
130
            raise SynapseError(400, "Shared secret registration is not enabled")
!
131

UNCOV
132
        body = parse_json_object_from_request(request)
!
133

UNCOV
134
        if "nonce" not in body:
Branches [[0, 135], [0, 137]] missed. !
135
            raise SynapseError(400, "nonce must be specified", errcode=Codes.BAD_JSON)
!
136

UNCOV
137
        nonce = body["nonce"]
!
138

UNCOV
139
        if nonce not in self.nonces:
Branches [[0, 140], [0, 143]] missed. !
140
            raise SynapseError(400, "unrecognised nonce")
!
141

142
        # Delete the nonce, so it can't be reused, even if it's invalid
UNCOV
143
        del self.nonces[nonce]
!
144

UNCOV
145
        if "username" not in body:
Branches [[0, 146], [0, 150]] missed. !
146
            raise SynapseError(
!
147
                400, "username must be specified", errcode=Codes.BAD_JSON
148
            )
149
        else:
UNCOV
150
            if (
Branches [[0, 154], [0, 156]] missed. !
151
                not isinstance(body["username"], text_type)
152
                or len(body["username"]) > 512
153
            ):
154
                raise SynapseError(400, "Invalid username")
!
155

UNCOV
156
            username = body["username"].encode("utf-8")
!
UNCOV
157
            if b"\x00" in username:
Branches [[0, 158], [0, 160]] missed. !
158
                raise SynapseError(400, "Invalid username")
!
159

UNCOV
160
        if "password" not in body:
Branches [[0, 161], [0, 165]] missed. !
161
            raise SynapseError(
!
162
                400, "password must be specified", errcode=Codes.BAD_JSON
163
            )
164
        else:
UNCOV
165
            if (
Branches [[0, 169], [0, 171]] missed. !
166
                not isinstance(body["password"], text_type)
167
                or len(body["password"]) > 512
168
            ):
169
                raise SynapseError(400, "Invalid password")
!
170

UNCOV
171
            password = body["password"].encode("utf-8")
!
UNCOV
172
            if b"\x00" in password:
Branches [[0, 173], [0, 175]] missed. !
173
                raise SynapseError(400, "Invalid password")
!
174

UNCOV
175
        admin = body.get("admin", None)
!
UNCOV
176
        user_type = body.get("user_type", None)
!
177

UNCOV
178
        if user_type is not None and user_type not in UserTypes.ALL_USER_TYPES:
Branches [[0, 179], [0, 181]] missed. !
179
            raise SynapseError(400, "Invalid user type")
!
180

UNCOV
181
        got_mac = body["mac"]
!
182

UNCOV
183
        want_mac = hmac.new(
!
184
            key=self.hs.config.registration_shared_secret.encode(),
185
            digestmod=hashlib.sha1,
186
        )
UNCOV
187
        want_mac.update(nonce.encode("utf8"))
!
UNCOV
188
        want_mac.update(b"\x00")
!
UNCOV
189
        want_mac.update(username)
!
UNCOV
190
        want_mac.update(b"\x00")
!
UNCOV
191
        want_mac.update(password)
!
UNCOV
192
        want_mac.update(b"\x00")
!
UNCOV
193
        want_mac.update(b"admin" if admin else b"notadmin")
!
UNCOV
194
        if user_type:
Branches [[0, 195], [0, 197]] missed. !
195
            want_mac.update(b"\x00")
!
196
            want_mac.update(user_type.encode("utf8"))
!
UNCOV
197
        want_mac = want_mac.hexdigest()
!
198

UNCOV
199
        if not hmac.compare_digest(want_mac.encode("ascii"), got_mac.encode("ascii")):
Branches [[0, 200], [0, 203]] missed. !
200
            raise SynapseError(403, "HMAC incorrect")
!
201

202
        # Reuse the parts of RegisterRestServlet to reduce code duplication
UNCOV
203
        from synapse.rest.client.v2_alpha.register import RegisterRestServlet
!
204

UNCOV
205
        register = RegisterRestServlet(self.hs)
!
206

UNCOV
207
        user_id = yield register.registration_handler.register_user(
!
208
            localpart=body["username"].lower(),
209
            password=body["password"],
210
            admin=bool(admin),
211
            user_type=user_type,
212
        )
213

UNCOV
214
        result = yield register._create_registration_details(user_id, body)
!
UNCOV
215
        return 200, result
!
216

217

218
class WhoisRestServlet(RestServlet):
1×
219
    PATTERNS = historical_admin_path_patterns("/whois/(?P<user_id>[^/]*)")
1×
220

221
    def __init__(self, hs):
1×
UNCOV
222
        self.hs = hs
!
UNCOV
223
        self.auth = hs.get_auth()
!
UNCOV
224
        self.handlers = hs.get_handlers()
!
225

226
    @defer.inlineCallbacks
1×
227
    def on_GET(self, request, user_id):
UNCOV
228
        target_user = UserID.from_string(user_id)
!
UNCOV
229
        requester = yield self.auth.get_user_by_req(request)
!
UNCOV
230
        auth_user = requester.user
!
231

UNCOV
232
        if target_user != auth_user:
Branches [[0, 233], [0, 235]] missed. !
233
            yield assert_user_is_admin(self.auth, auth_user)
!
234

UNCOV
235
        if not self.hs.is_mine(target_user):
Branches [[0, 236], [0, 238]] missed. !
236
            raise SynapseError(400, "Can only whois a local user")
!
237

UNCOV
238
        ret = yield self.handlers.admin_handler.get_whois(target_user)
!
239

UNCOV
240
        return 200, ret
!
241

242

243
class PurgeHistoryRestServlet(RestServlet):
1×
244
    PATTERNS = historical_admin_path_patterns(
1×
245
        "/purge_history/(?P<room_id>[^/]*)(/(?P<event_id>[^/]+))?"
246
    )
247

248
    def __init__(self, hs):
1×
249
        """
250

251
        Args:
252
            hs (synapse.server.HomeServer)
253
        """
UNCOV
254
        self.pagination_handler = hs.get_pagination_handler()
!
UNCOV
255
        self.store = hs.get_datastore()
!
UNCOV
256
        self.auth = hs.get_auth()
!
257

258
    @defer.inlineCallbacks
1×
259
    def on_POST(self, request, room_id, event_id):
UNCOV
260
        yield assert_requester_is_admin(self.auth, request)
!
261

UNCOV
262
        body = parse_json_object_from_request(request, allow_empty_body=True)
!
263

UNCOV
264
        delete_local_events = bool(body.get("delete_local_events", False))
!
265

266
        # establish the topological ordering we should keep events from. The
267
        # user can provide an event_id in the URL or the request body, or can
268
        # provide a timestamp in the request body.
UNCOV
269
        if event_id is None:
Branches [[0, 270], [0, 272]] missed. !
UNCOV
270
            event_id = body.get("purge_up_to_event_id")
!
271

UNCOV
272
        if event_id is not None:
Branches [[0, 273], [0, 281]] missed. !
UNCOV
273
            event = yield self.store.get_event(event_id)
!
274

UNCOV
275
            if event.room_id != room_id:
Branches [[0, 276], [0, 278]] missed. !
276
                raise SynapseError(400, "Event is for wrong room.")
!
277

UNCOV
278
            token = yield self.store.get_topological_token_for_event(event_id)
!
279

UNCOV
280
            logger.info("[purge] purging up to token %s (event_id %s)", token, event_id)
!
UNCOV
281
        elif "purge_up_to_ts" in body:
Branches [[0, 282], [0, 315]] missed. !
UNCOV
282
            ts = body["purge_up_to_ts"]
!
UNCOV
283
            if not isinstance(ts, int):
Branches [[0, 284], [0, 288]] missed. !
284
                raise SynapseError(
!
285
                    400, "purge_up_to_ts must be an int", errcode=Codes.BAD_JSON
286
                )
287

UNCOV
288
            stream_ordering = (yield self.store.find_first_stream_ordering_after_ts(ts))
!
289

UNCOV
290
            r = (
!
291
                yield self.store.get_room_event_after_stream_ordering(
292
                    room_id, stream_ordering
293
                )
294
            )
UNCOV
295
            if not r:
Branches [[0, 296], [0, 305]] missed. !
296
                logger.warn(
!
297
                    "[purge] purging events not possible: No event found "
298
                    "(received_ts %i => stream_ordering %i)",
299
                    ts,
300
                    stream_ordering,
301
                )
302
                raise SynapseError(
!
303
                    404, "there is no event to be purged", errcode=Codes.NOT_FOUND
304
                )
UNCOV
305
            (stream, topo, _event_id) = r
!
UNCOV
306
            token = "t%d-%d" % (topo, stream)
!
UNCOV
307
            logger.info(
!
308
                "[purge] purging up to token %s (received_ts %i => "
309
                "stream_ordering %i)",
310
                token,
311
                ts,
312
                stream_ordering,
313
            )
314
        else:
315
            raise SynapseError(
!
316
                400,
317
                "must specify purge_up_to_event_id or purge_up_to_ts",
318
                errcode=Codes.BAD_JSON,
319
            )
320

UNCOV
321
        purge_id = yield self.pagination_handler.start_purge_history(
!
322
            room_id, token, delete_local_events=delete_local_events
323
        )
324

UNCOV
325
        return 200, {"purge_id": purge_id}
!
326

327

328
class PurgeHistoryStatusRestServlet(RestServlet):
1×
329
    PATTERNS = historical_admin_path_patterns(
1×
330
        "/purge_history_status/(?P<purge_id>[^/]+)"
331
    )
332

333
    def __init__(self, hs):
1×
334
        """
335

336
        Args:
337
            hs (synapse.server.HomeServer)
338
        """
UNCOV
339
        self.pagination_handler = hs.get_pagination_handler()
!
UNCOV
340
        self.auth = hs.get_auth()
!
341

342
    @defer.inlineCallbacks
1×
343
    def on_GET(self, request, purge_id):
UNCOV
344
        yield assert_requester_is_admin(self.auth, request)
!
345

UNCOV
346
        purge_status = self.pagination_handler.get_purge_status(purge_id)
!
UNCOV
347
        if purge_status is None:
Branches [[0, 348], [0, 350]] missed. !
348
            raise NotFoundError("purge id '%s' not found" % purge_id)
!
349

UNCOV
350
        return 200, purge_status.asdict()
!
351

352

353
class DeactivateAccountRestServlet(RestServlet):
1×
354
    PATTERNS = historical_admin_path_patterns("/deactivate/(?P<target_user_id>[^/]*)")
1×
355

356
    def __init__(self, hs):
1×
UNCOV
357
        self._deactivate_account_handler = hs.get_deactivate_account_handler()
!
UNCOV
358
        self.auth = hs.get_auth()
!
359

360
    @defer.inlineCallbacks
1×
361
    def on_POST(self, request, target_user_id):
362
        yield assert_requester_is_admin(self.auth, request)
!
363
        body = parse_json_object_from_request(request, allow_empty_body=True)
!
364
        erase = body.get("erase", False)
!
365
        if not isinstance(erase, bool):
Branches [[0, 366], [0, 372]] missed. !
366
            raise SynapseError(
!
367
                http_client.BAD_REQUEST,
368
                "Param 'erase' must be a boolean, if given",
369
                Codes.BAD_JSON,
370
            )
371

372
        UserID.from_string(target_user_id)
!
373

374
        result = yield self._deactivate_account_handler.deactivate_account(
!
375
            target_user_id, erase
376
        )
377
        if result:
Branches [[0, 378], [0, 380]] missed. !
378
            id_server_unbind_result = "success"
!
379
        else:
380
            id_server_unbind_result = "no-support"
!
381

382
        return 200, {"id_server_unbind_result": id_server_unbind_result}
!
383

384

385
class ShutdownRoomRestServlet(RestServlet):
1×
386
    """Shuts down a room by removing all local users from the room and blocking
387
    all future invites and joins to the room. Any local aliases will be repointed
388
    to a new room created by `new_room_user_id` and kicked users will be auto
389
    joined to the new room.
390
    """
391

392
    PATTERNS = historical_admin_path_patterns("/shutdown_room/(?P<room_id>[^/]+)")
1×
393

394
    DEFAULT_MESSAGE = (
1×
395
        "Sharing illegal content on this server is not permitted and rooms in"
396
        " violation will be blocked."
397
    )
398

399
    def __init__(self, hs):
1×
UNCOV
400
        self.hs = hs
!
UNCOV
401
        self.store = hs.get_datastore()
!
UNCOV
402
        self.state = hs.get_state_handler()
!
UNCOV
403
        self._room_creation_handler = hs.get_room_creation_handler()
!
UNCOV
404
        self.event_creation_handler = hs.get_event_creation_handler()
!
UNCOV
405
        self.room_member_handler = hs.get_room_member_handler()
!
UNCOV
406
        self.auth = hs.get_auth()
!
407

408
    @defer.inlineCallbacks
1×
409
    def on_POST(self, request, room_id):
UNCOV
410
        requester = yield self.auth.get_user_by_req(request)
!
UNCOV
411
        yield assert_user_is_admin(self.auth, requester.user)
!
412

UNCOV
413
        content = parse_json_object_from_request(request)
!
UNCOV
414
        assert_params_in_dict(content, ["new_room_user_id"])
!
UNCOV
415
        new_room_user_id = content["new_room_user_id"]
!
416

UNCOV
417
        room_creator_requester = create_requester(new_room_user_id)
!
418

UNCOV
419
        message = content.get("message", self.DEFAULT_MESSAGE)
!
UNCOV
420
        room_name = content.get("room_name", "Content Violation Notification")
!
421

UNCOV
422
        info = yield self._room_creation_handler.create_room(
!
423
            room_creator_requester,
424
            config={
425
                "preset": "public_chat",
426
                "name": room_name,
427
                "power_level_content_override": {"users_default": -10},
428
            },
429
            ratelimit=False,
430
        )
UNCOV
431
        new_room_id = info["room_id"]
!
432

UNCOV
433
        requester_user_id = requester.user.to_string()
!
434

UNCOV
435
        logger.info(
!
436
            "Shutting down room %r, joining to new room: %r", room_id, new_room_id
437
        )
438

439
        # This will work even if the room is already blocked, but that is
440
        # desirable in case the first attempt at blocking the room failed below.
UNCOV
441
        yield self.store.block_room(room_id, requester_user_id)
!
442

UNCOV
443
        users = yield self.state.get_current_users_in_room(room_id)
!
UNCOV
444
        kicked_users = []
!
UNCOV
445
        failed_to_kick_users = []
!
UNCOV
446
        for user_id in users:
Branches [[0, 447], [0, 483]] missed. !
UNCOV
447
            if not self.hs.is_mine_id(user_id):
Branches [[0, 448], [0, 450]] missed. !
UNCOV
448
                continue
!
449

UNCOV
450
            logger.info("Kicking %r from %r...", user_id, room_id)
!
451

UNCOV
452
            try:
!
UNCOV
453
                target_requester = create_requester(user_id)
!
UNCOV
454
                yield self.room_member_handler.update_membership(
!
455
                    requester=target_requester,
456
                    target=target_requester.user,
457
                    room_id=room_id,
458
                    action=Membership.LEAVE,
459
                    content={},
460
                    ratelimit=False,
461
                    require_consent=False,
462
                )
463

UNCOV
464
                yield self.room_member_handler.forget(target_requester.user, room_id)
!
465

UNCOV
466
                yield self.room_member_handler.update_membership(
!
467
                    requester=target_requester,
468
                    target=target_requester.user,
469
                    room_id=new_room_id,
470
                    action=Membership.JOIN,
471
                    content={},
472
                    ratelimit=False,
473
                    require_consent=False,
474
                )
475

UNCOV
476
                kicked_users.append(user_id)
!
477
            except Exception:
!
478
                logger.exception(
!
479
                    "Failed to leave old room and join new room for %r", user_id
480
                )
481
                failed_to_kick_users.append(user_id)
!
482

UNCOV
483
        yield self.event_creation_handler.create_and_send_nonmember_event(
!
484
            room_creator_requester,
485
            {
486
                "type": "m.room.message",
487
                "content": {"body": message, "msgtype": "m.text"},
488
                "room_id": new_room_id,
489
                "sender": new_room_user_id,
490
            },
491
            ratelimit=False,
492
        )
493

UNCOV
494
        aliases_for_room = yield self.store.get_aliases_for_room(room_id)
!
495

UNCOV
496
        yield self.store.update_aliases_for_room(
!
497
            room_id, new_room_id, requester_user_id
498
        )
499

UNCOV
500
        return (
!
501
            200,
502
            {
503
                "kicked_users": kicked_users,
504
                "failed_to_kick_users": failed_to_kick_users,
505
                "local_aliases": aliases_for_room,
506
                "new_room_id": new_room_id,
507
            },
508
        )
509

510

511
class ResetPasswordRestServlet(RestServlet):
1×
512
    """Post request to allow an administrator reset password for a user.
513
    This needs user to have administrator access in Synapse.
514
        Example:
515
            http://localhost:8008/_synapse/admin/v1/reset_password/
516
            @user:to_reset_password?access_token=admin_access_token
517
        JsonBodyToSend:
518
            {
519
                "new_password": "secret"
520
            }
521
        Returns:
522
            200 OK with empty object if success otherwise an error.
523
        """
524

525
    PATTERNS = historical_admin_path_patterns(
1×
526
        "/reset_password/(?P<target_user_id>[^/]*)"
527
    )
528

529
    def __init__(self, hs):
1×
UNCOV
530
        self.store = hs.get_datastore()
!
UNCOV
531
        self.hs = hs
!
UNCOV
532
        self.auth = hs.get_auth()
!
UNCOV
533
        self._set_password_handler = hs.get_set_password_handler()
!
534

535
    @defer.inlineCallbacks
1×
536
    def on_POST(self, request, target_user_id):
537
        """Post request to allow an administrator reset password for a user.
538
        This needs user to have administrator access in Synapse.
539
        """
540
        requester = yield self.auth.get_user_by_req(request)
!
541
        yield assert_user_is_admin(self.auth, requester.user)
!
542

543
        UserID.from_string(target_user_id)
!
544

545
        params = parse_json_object_from_request(request)
!
546
        assert_params_in_dict(params, ["new_password"])
!
547
        new_password = params["new_password"]
!
548

549
        yield self._set_password_handler.set_password(
!
550
            target_user_id, new_password, requester
551
        )
552
        return 200, {}
!
553

554

555
class GetUsersPaginatedRestServlet(RestServlet):
1×
556
    """Get request to get specific number of users from Synapse.
557
    This needs user to have administrator access in Synapse.
558
        Example:
559
            http://localhost:8008/_synapse/admin/v1/users_paginate/
560
            @admin:user?access_token=admin_access_token&start=0&limit=10
561
        Returns:
562
            200 OK with json object {list[dict[str, Any]], count} or empty object.
563
        """
564

565
    PATTERNS = historical_admin_path_patterns(
1×
566
        "/users_paginate/(?P<target_user_id>[^/]*)"
567
    )
568

569
    def __init__(self, hs):
1×
UNCOV
570
        self.store = hs.get_datastore()
!
UNCOV
571
        self.hs = hs
!
UNCOV
572
        self.auth = hs.get_auth()
!
UNCOV
573
        self.handlers = hs.get_handlers()
!
574

575
    @defer.inlineCallbacks
1×
576
    def on_GET(self, request, target_user_id):
577
        """Get request to get specific number of users from Synapse.
578
        This needs user to have administrator access in Synapse.
579
        """
580
        yield assert_requester_is_admin(self.auth, request)
!
581

582
        target_user = UserID.from_string(target_user_id)
!
583

584
        if not self.hs.is_mine(target_user):
Branches [[0, 585], [0, 587]] missed. !
585
            raise SynapseError(400, "Can only users a local user")
!
586

587
        order = "name"  # order by name in user table
!
588
        start = parse_integer(request, "start", required=True)
!
589
        limit = parse_integer(request, "limit", required=True)
!
590

591
        logger.info("limit: %s, start: %s", limit, start)
!
592

593
        ret = yield self.handlers.admin_handler.get_users_paginate(order, start, limit)
!
594
        return 200, ret
!
595

596
    @defer.inlineCallbacks
1×
597
    def on_POST(self, request, target_user_id):
598
        """Post request to get specific number of users from Synapse..
599
        This needs user to have administrator access in Synapse.
600
        Example:
601
            http://localhost:8008/_synapse/admin/v1/users_paginate/
602
            @admin:user?access_token=admin_access_token
603
        JsonBodyToSend:
604
            {
605
                "start": "0",
606
                "limit": "10
607
            }
608
        Returns:
609
            200 OK with json object {list[dict[str, Any]], count} or empty object.
610
        """
611
        yield assert_requester_is_admin(self.auth, request)
!
612
        UserID.from_string(target_user_id)
!
613

614
        order = "name"  # order by name in user table
!
615
        params = parse_json_object_from_request(request)
!
616
        assert_params_in_dict(params, ["limit", "start"])
!
617
        limit = params["limit"]
!
618
        start = params["start"]
!
619
        logger.info("limit: %s, start: %s", limit, start)
!
620

621
        ret = yield self.handlers.admin_handler.get_users_paginate(order, start, limit)
!
622
        return 200, ret
!
623

624

625
class SearchUsersRestServlet(RestServlet):
1×
626
    """Get request to search user table for specific users according to
627
    search term.
628
    This needs user to have administrator access in Synapse.
629
        Example:
630
            http://localhost:8008/_synapse/admin/v1/search_users/
631
            @admin:user?access_token=admin_access_token&term=alice
632
        Returns:
633
            200 OK with json object {list[dict[str, Any]], count} or empty object.
634
    """
635

636
    PATTERNS = historical_admin_path_patterns("/search_users/(?P<target_user_id>[^/]*)")
1×
637

638
    def __init__(self, hs):
1×
UNCOV
639
        self.store = hs.get_datastore()
!
UNCOV
640
        self.hs = hs
!
UNCOV
641
        self.auth = hs.get_auth()
!
UNCOV
642
        self.handlers = hs.get_handlers()
!
643

644
    @defer.inlineCallbacks
1×
645
    def on_GET(self, request, target_user_id):
646
        """Get request to search user table for specific users according to
647
        search term.
648
        This needs user to have a administrator access in Synapse.
649
        """
650
        yield assert_requester_is_admin(self.auth, request)
!
651

652
        target_user = UserID.from_string(target_user_id)
!
653

654
        # To allow all users to get the users list
655
        # if not is_admin and target_user != auth_user:
656
        #     raise AuthError(403, "You are not a server admin")
657

658
        if not self.hs.is_mine(target_user):
Branches [[0, 659], [0, 661]] missed. !
659
            raise SynapseError(400, "Can only users a local user")
!
660

661
        term = parse_string(request, "term", required=True)
!
662
        logger.info("term: %s ", term)
!
663

664
        ret = yield self.handlers.admin_handler.search_users(term)
!
665
        return 200, ret
!
666

667

668
class DeleteGroupAdminRestServlet(RestServlet):
1×
669
    """Allows deleting of local groups
670
    """
671

672
    PATTERNS = historical_admin_path_patterns("/delete_group/(?P<group_id>[^/]*)")
1×
673

674
    def __init__(self, hs):
1×
UNCOV
675
        self.group_server = hs.get_groups_server_handler()
!
UNCOV
676
        self.is_mine_id = hs.is_mine_id
!
UNCOV
677
        self.auth = hs.get_auth()
!
678

679
    @defer.inlineCallbacks
1×
680
    def on_POST(self, request, group_id):
681
        requester = yield self.auth.get_user_by_req(request)
!
682
        yield assert_user_is_admin(self.auth, requester.user)
!
683

684
        if not self.is_mine_id(group_id):
Branches [[0, 685], [0, 687]] missed. !
685
            raise SynapseError(400, "Can only delete local groups")
!
686

687
        yield self.group_server.delete_group(group_id, requester.user.to_string())
!
688
        return 200, {}
!
689

690

691
class AccountValidityRenewServlet(RestServlet):
1×
692
    PATTERNS = historical_admin_path_patterns("/account_validity/validity$")
1×
693

694
    def __init__(self, hs):
1×
695
        """
696
        Args:
697
            hs (synapse.server.HomeServer): server
698
        """
UNCOV
699
        self.hs = hs
!
UNCOV
700
        self.account_activity_handler = hs.get_account_validity_handler()
!
UNCOV
701
        self.auth = hs.get_auth()
!
702

703
    @defer.inlineCallbacks
1×
704
    def on_POST(self, request):
705
        yield assert_requester_is_admin(self.auth, request)
!
706

707
        body = parse_json_object_from_request(request)
!
708

709
        if "user_id" not in body:
Branches [[0, 710], [0, 712]] missed. !
710
            raise SynapseError(400, "Missing property 'user_id' in the request body")
!
711

712
        expiration_ts = yield self.account_activity_handler.renew_account_for_user(
!
713
            body["user_id"],
714
            body.get("expiration_ts"),
715
            not body.get("enable_renewal_emails", True),
716
        )
717

718
        res = {"expiration_ts": expiration_ts}
!
719
        return 200, res
!
720

721

722
########################################################################################
723
#
724
# please don't add more servlets here: this file is already long and unwieldy. Put
725
# them in separate files within the 'admin' package.
726
#
727
########################################################################################
728

729

730
class AdminRestResource(JsonResource):
1×
731
    """The REST resource which gets mounted at /_synapse/admin"""
732

733
    def __init__(self, hs):
1×
UNCOV
734
        JsonResource.__init__(self, hs, canonical_json=False)
!
UNCOV
735
        register_servlets(hs, self)
!
736

737

738
def register_servlets(hs, http_server):
1×
739
    """
740
    Register all the admin servlets.
741
    """
UNCOV
742
    register_servlets_for_client_rest_resource(hs, http_server)
!
UNCOV
743
    PurgeRoomServlet(hs).register(http_server)
!
UNCOV
744
    SendServerNoticeServlet(hs).register(http_server)
!
UNCOV
745
    VersionServlet(hs).register(http_server)
!
UNCOV
746
    UserAdminServlet(hs).register(http_server)
!
747

748

749
def register_servlets_for_client_rest_resource(hs, http_server):
1×
750
    """Register only the servlets which need to be exposed on /_matrix/client/xxx"""
UNCOV
751
    WhoisRestServlet(hs).register(http_server)
!
UNCOV
752
    PurgeHistoryStatusRestServlet(hs).register(http_server)
!
UNCOV
753
    DeactivateAccountRestServlet(hs).register(http_server)
!
UNCOV
754
    PurgeHistoryRestServlet(hs).register(http_server)
!
UNCOV
755
    UsersRestServlet(hs).register(http_server)
!
UNCOV
756
    ResetPasswordRestServlet(hs).register(http_server)
!
UNCOV
757
    GetUsersPaginatedRestServlet(hs).register(http_server)
!
UNCOV
758
    SearchUsersRestServlet(hs).register(http_server)
!
UNCOV
759
    ShutdownRoomRestServlet(hs).register(http_server)
!
UNCOV
760
    UserRegisterServlet(hs).register(http_server)
!
UNCOV
761
    DeleteGroupAdminRestServlet(hs).register(http_server)
!
UNCOV
762
    AccountValidityRenewServlet(hs).register(http_server)
!
763

764
    # Load the media repo ones if we're using them. Otherwise load the servlets which
765
    # don't need a media repo (typically readonly admin APIs).
UNCOV
766
    if hs.config.can_load_media_repo:
Branches [[0, 767], [0, 769]] missed. !
UNCOV
767
        register_servlets_for_media_repo(hs, http_server)
!
768
    else:
769
        ListMediaInRoom(hs).register(http_server)
!
770

771
    # don't add more things here: new servlets should only be exposed on
772
    # /_synapse/admin so should not go here. Instead register them in AdminRestResource.
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2021 Coveralls, LLC