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

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

17
from twisted.internet import defer
1×
18

19
from synapse.types import UserID
1×
20

21
logger = logging.getLogger(__name__)
1×
22

23

24
class ModuleApi(object):
1×
25
    """A proxy object that gets passed to password auth providers so they
26
    can register new users etc if necessary.
27
    """
28

29
    def __init__(self, hs, auth_handler):
1×
UNCOV
30
        self.hs = hs
!
31

UNCOV
32
        self._store = hs.get_datastore()
!
UNCOV
33
        self._auth = hs.get_auth()
!
UNCOV
34
        self._auth_handler = auth_handler
!
35

36
    def get_user_by_req(self, req, allow_guest=False):
1×
37
        """Check the access_token provided for a request
38

39
        Args:
40
            req (twisted.web.server.Request): Incoming HTTP request
41
            allow_guest (bool): True if guest users should be allowed. If this
42
                is False, and the access token is for a guest user, an
43
                AuthError will be thrown
44
        Returns:
45
            twisted.internet.defer.Deferred[synapse.types.Requester]:
46
                the requester for this request
47
        Raises:
48
            synapse.api.errors.AuthError: if no user by that token exists,
49
                or the token is invalid.
50
        """
51
        return self._auth.get_user_by_req(req, allow_guest)
!
52

53
    def get_qualified_user_id(self, username):
1×
54
        """Qualify a user id, if necessary
55

56
        Takes a user id provided by the user and adds the @ and :domain to
57
        qualify it, if necessary
58

59
        Args:
60
            username (str): provided user id
61

62
        Returns:
63
            str: qualified @user:id
64
        """
65
        if username.startswith("@"):
Branches [[0, 66], [0, 67]] missed. !
66
            return username
!
67
        return UserID(username, self.hs.hostname).to_string()
!
68

69
    def check_user_exists(self, user_id):
1×
70
        """Check if user exists.
71

72
        Args:
73
            user_id (str): Complete @user:id
74

75
        Returns:
76
            Deferred[str|None]: Canonical (case-corrected) user_id, or None
77
               if the user is not registered.
78
        """
79
        return self._auth_handler.check_user_exists(user_id)
!
80

81
    @defer.inlineCallbacks
1×
82
    def register(self, localpart, displayname=None, emails=[]):
1×
83
        """Registers a new user with given localpart and optional displayname, emails.
84

85
        Also returns an access token for the new user.
86

87
        Deprecated: avoid this, as it generates a new device with no way to
88
        return that device to the user. Prefer separate calls to register_user and
89
        register_device.
90

91
        Args:
92
            localpart (str): The localpart of the new user.
93
            displayname (str|None): The displayname of the new user.
94
            emails (List[str]): Emails to bind to the new user.
95

96
        Returns:
97
            Deferred[tuple[str, str]]: a 2-tuple of (user_id, access_token)
98
        """
99
        logger.warning(
!
100
            "Using deprecated ModuleApi.register which creates a dummy user device."
101
        )
102
        user_id = yield self.register_user(localpart, displayname, emails)
!
103
        _, access_token = yield self.register_device(user_id)
!
104
        return user_id, access_token
!
105

106
    def register_user(self, localpart, displayname=None, emails=[]):
1×
107
        """Registers a new user with given localpart and optional displayname, emails.
108

109
        Args:
110
            localpart (str): The localpart of the new user.
111
            displayname (str|None): The displayname of the new user.
112
            emails (List[str]): Emails to bind to the new user.
113

114
        Returns:
115
            Deferred[str]: user_id
116
        """
117
        return self.hs.get_registration_handler().register_user(
!
118
            localpart=localpart, default_display_name=displayname, bind_emails=emails
119
        )
120

121
    def register_device(self, user_id, device_id=None, initial_display_name=None):
1×
122
        """Register a device for a user and generate an access token.
123

124
        Args:
125
            user_id (str): full canonical @user:id
126
            device_id (str|None): The device ID to check, or None to generate
127
                a new one.
128
            initial_display_name (str|None): An optional display name for the
129
                device.
130

131
        Returns:
132
            defer.Deferred[tuple[str, str]]: Tuple of device ID and access token
133
        """
134
        return self.hs.get_registration_handler().register_device(
!
135
            user_id=user_id,
136
            device_id=device_id,
137
            initial_display_name=initial_display_name,
138
        )
139

140
    @defer.inlineCallbacks
1×
141
    def invalidate_access_token(self, access_token):
142
        """Invalidate an access token for a user
143

144
        Args:
145
            access_token(str): access token
146

147
        Returns:
148
            twisted.internet.defer.Deferred - resolves once the access token
149
               has been removed.
150

151
        Raises:
152
            synapse.api.errors.AuthError: the access token is invalid
153
        """
154
        # see if the access token corresponds to a device
155
        user_info = yield self._auth.get_user_by_access_token(access_token)
!
156
        device_id = user_info.get("device_id")
!
157
        user_id = user_info["user"].to_string()
!
158
        if device_id:
Branches [[0, 160], [0, 163]] missed. !
159
            # delete the device, which will also delete its access tokens
160
            yield self.hs.get_device_handler().delete_device(user_id, device_id)
!
161
        else:
162
            # no associated device. Just delete the access token.
163
            yield self._auth_handler.delete_access_token(access_token)
!
164

165
    def run_db_interaction(self, desc, func, *args, **kwargs):
1×
166
        """Run a function with a database connection
167

168
        Args:
169
            desc (str): description for the transaction, for metrics etc
170
            func (func): function to be run. Passed a database cursor object
171
                as well as *args and **kwargs
172
            *args: positional args to be passed to func
173
            **kwargs: named args to be passed to func
174

175
        Returns:
176
            Deferred[object]: result of func
177
        """
178
        return self._store.runInteraction(desc, func, *args, **kwargs)
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2019 Coveralls, LLC