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

Commit

Permalink
Update ModuleApi to avoid register(generate_token=True) (#5640)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Feb 17, 2020
2 parents 1af97bb + 4b1f7fe commit 4ede3f0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.d/5640.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update ModuleApi to avoid register(generate_token=True).
64 changes: 56 additions & 8 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
# 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.
import logging

from twisted.internet import defer

from synapse.types import UserID

logger = logging.getLogger(__name__)


class ModuleApi(object):
"""A proxy object that gets passed to password auth providers so they
Expand Down Expand Up @@ -76,25 +80,69 @@ def check_user_exists(self, user_id):

@defer.inlineCallbacks
def register(self, localpart, displayname=None, emails=[]):
"""Registers a new user with given localpart and optional
displayname, emails.
"""Registers a new user with given localpart and optional displayname, emails.
Also returns an access token for the new user.
Deprecated: avoid this, as it generates a new device with no way to
return that device to the user. Prefer separate calls to register_user and
register_device.
Args:
localpart (str): The localpart of the new user.
displayname (str|None): The displayname of the new user.
emails (List[str]): Emails to bind to the new user.
Returns:
Deferred: a 2-tuple of (user_id, access_token)
Deferred[tuple[str, str]]: a 2-tuple of (user_id, access_token)
"""
# Register the user
reg = self.hs.get_registration_handler()
user_id, access_token = yield reg.register(
localpart=localpart, default_display_name=displayname, bind_emails=emails
logger.warning(
"Using deprecated ModuleApi.register which creates a dummy user device."
)

user_id = yield self.register_user(localpart, displayname, emails)
_, access_token = yield self.register_device(user_id)
defer.returnValue((user_id, access_token))

@defer.inlineCallbacks
def register_user(self, localpart, displayname=None, emails=[]):
"""Registers a new user with given localpart and optional displayname, emails.
Args:
localpart (str): The localpart of the new user.
displayname (str|None): The displayname of the new user.
emails (List[str]): Emails to bind to the new user.
Returns:
Deferred[str]: user_id
"""
user_id, _ = yield self.hs.get_registration_handler().register(
localpart=localpart,
default_display_name=displayname,
bind_emails=emails,
generate_token=False,
)

defer.returnValue(user_id)

def register_device(self, user_id, device_id=None, initial_display_name=None):
"""Register a device for a user and generate an access token.
Args:
user_id (str): full canonical @user:id
device_id (str|None): The device ID to check, or None to generate
a new one.
initial_display_name (str|None): An optional display name for the
device.
Returns:
defer.Deferred[tuple[str, str]]: Tuple of device ID and access token
"""
return self.hs.get_registration_handler().register_device(
user_id=user_id,
device_id=device_id,
initial_display_name=initial_display_name,
)

@defer.inlineCallbacks
def invalidate_access_token(self, access_token):
"""Invalidate an access token for a user
Expand Down

0 comments on commit 4ede3f0

Please sign in to comment.