2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-05 13:01:18 -05:00
|
|
|
# Copyright 2014 - 2016 OpenMarket Ltd
|
2014-08-12 10:10:52 -04:00
|
|
|
#
|
|
|
|
# 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.
|
2014-08-12 22:14:34 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Contains functions for registering clients."""
|
2016-07-26 11:46:53 -04:00
|
|
|
import logging
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse import types
|
2019-05-20 06:20:08 -04:00
|
|
|
from synapse.api.constants import MAX_USERID_LENGTH, LoginType
|
2014-09-06 01:55:29 -04:00
|
|
|
from synapse.api.errors import (
|
2018-07-09 02:09:20 -04:00
|
|
|
AuthError,
|
|
|
|
Codes,
|
2019-03-19 07:38:59 -04:00
|
|
|
ConsentNotGivenError,
|
2018-07-09 02:09:20 -04:00
|
|
|
InvalidCaptchaError,
|
2019-03-05 09:25:33 -05:00
|
|
|
LimitExceededError,
|
2018-07-09 02:09:20 -04:00
|
|
|
RegistrationError,
|
|
|
|
SynapseError,
|
2014-09-06 01:55:29 -04:00
|
|
|
)
|
2019-02-20 02:47:31 -05:00
|
|
|
from synapse.config.server import is_threepid_reserved
|
2014-10-02 08:57:48 -04:00
|
|
|
from synapse.http.client import CaptchaServerHttpClient
|
2019-02-20 02:47:31 -05:00
|
|
|
from synapse.http.servlet import assert_params_in_dict
|
2019-02-18 11:49:38 -05:00
|
|
|
from synapse.replication.http.login import RegisterDeviceReplicationServlet
|
2019-02-20 02:47:31 -05:00
|
|
|
from synapse.replication.http.register import (
|
|
|
|
ReplicationPostRegisterActionsServlet,
|
|
|
|
ReplicationRegisterServlet,
|
|
|
|
)
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.types import RoomAlias, RoomID, UserID, create_requester
|
2018-08-10 09:50:21 -04:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2018-01-19 10:33:55 -05:00
|
|
|
from synapse.util.threepids import check_3pid_allowed
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2016-07-26 11:46:53 -04:00
|
|
|
from ._base import BaseHandler
|
2014-09-03 13:22:27 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RegistrationHandler(BaseHandler):
|
|
|
|
def __init__(self, hs):
|
2018-05-17 06:34:28 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
hs (synapse.server.HomeServer):
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
super(RegistrationHandler, self).__init__(hs)
|
2018-07-30 10:55:57 -04:00
|
|
|
self.hs = hs
|
2016-01-05 13:01:18 -05:00
|
|
|
self.auth = hs.get_auth()
|
2017-11-01 06:29:34 -04:00
|
|
|
self._auth_handler = hs.get_auth_handler()
|
2017-08-25 09:34:56 -04:00
|
|
|
self.profile_handler = hs.get_profile_handler()
|
2017-11-29 13:33:34 -05:00
|
|
|
self.user_directory_handler = hs.get_user_directory_handler()
|
2015-12-18 05:06:56 -05:00
|
|
|
self.captcha_client = CaptchaServerHttpClient(hs)
|
2019-02-20 02:47:31 -05:00
|
|
|
self.identity_handler = self.hs.get_handlers().identity_handler
|
2019-03-06 06:02:42 -05:00
|
|
|
self.ratelimiter = hs.get_registration_ratelimiter()
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-05 06:22:30 -05:00
|
|
|
self._next_generated_user_id = None
|
|
|
|
|
2017-02-02 05:53:36 -05:00
|
|
|
self.macaroon_gen = hs.get_macaroon_generator()
|
|
|
|
|
2018-03-26 07:06:48 -04:00
|
|
|
self._generate_user_id_linearizer = Linearizer(
|
2019-06-20 05:32:02 -04:00
|
|
|
name="_generate_user_id_linearizer"
|
2018-03-26 07:06:48 -04:00
|
|
|
)
|
2018-05-17 06:34:28 -04:00
|
|
|
self._server_notices_mxid = hs.config.server_notices_mxid
|
2018-03-26 07:02:20 -04:00
|
|
|
|
2019-02-18 07:12:57 -05:00
|
|
|
if hs.config.worker_app:
|
|
|
|
self._register_client = ReplicationRegisterServlet.make_client(hs)
|
2019-06-20 05:32:02 -04:00
|
|
|
self._register_device_client = RegisterDeviceReplicationServlet.make_client(
|
|
|
|
hs
|
2019-02-18 11:49:38 -05:00
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
self._post_registration_client = ReplicationPostRegisterActionsServlet.make_client(
|
|
|
|
hs
|
2019-02-20 02:47:31 -05:00
|
|
|
)
|
2019-02-18 11:49:38 -05:00
|
|
|
else:
|
|
|
|
self.device_handler = hs.get_device_handler()
|
2019-02-20 02:47:31 -05:00
|
|
|
self.pusher_pool = hs.get_pusherpool()
|
2019-02-18 07:12:57 -05:00
|
|
|
|
2015-04-16 14:56:44 -04:00
|
|
|
@defer.inlineCallbacks
|
2019-06-20 05:32:02 -04:00
|
|
|
def check_username(self, localpart, guest_access_token=None, assigned_user_id=None):
|
2017-11-09 16:57:24 -05:00
|
|
|
if types.contains_invalid_mxid_characters(localpart):
|
2015-04-16 14:56:44 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400,
|
2017-10-20 18:42:53 -04:00
|
|
|
"User ID can only contain characters a-z, 0-9, or '=_-./'",
|
2019-06-20 05:32:02 -04:00
|
|
|
Codes.INVALID_USERNAME,
|
2015-04-16 14:56:44 -04:00
|
|
|
)
|
|
|
|
|
2017-05-10 12:34:30 -04:00
|
|
|
if not localpart:
|
2019-06-20 05:32:02 -04:00
|
|
|
raise SynapseError(400, "User ID cannot be empty", Codes.INVALID_USERNAME)
|
2017-05-10 12:17:12 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
if localpart[0] == "_":
|
2016-07-27 12:54:26 -04:00
|
|
|
raise SynapseError(
|
2019-06-20 05:32:02 -04:00
|
|
|
400, "User ID may not begin with _", Codes.INVALID_USERNAME
|
2016-07-27 12:54:26 -04:00
|
|
|
)
|
|
|
|
|
2015-04-16 14:56:44 -04:00
|
|
|
user = UserID(localpart, self.hs.hostname)
|
|
|
|
user_id = user.to_string()
|
|
|
|
|
2016-03-16 15:36:57 -04:00
|
|
|
if assigned_user_id:
|
|
|
|
if user_id == assigned_user_id:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"A different user ID has already been registered for this session",
|
|
|
|
)
|
|
|
|
|
2017-10-20 18:37:22 -04:00
|
|
|
self.check_user_id_not_appservice_exclusive(user_id)
|
2015-04-16 14:56:44 -04:00
|
|
|
|
2019-05-20 06:20:08 -04:00
|
|
|
if len(user_id) > MAX_USERID_LENGTH:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
2019-06-20 05:32:02 -04:00
|
|
|
"User ID may not be longer than %s characters" % (MAX_USERID_LENGTH,),
|
|
|
|
Codes.INVALID_USERNAME,
|
2019-05-20 06:20:08 -04:00
|
|
|
)
|
|
|
|
|
2015-08-26 08:42:45 -04:00
|
|
|
users = yield self.store.get_users_by_id_case_insensitive(user_id)
|
|
|
|
if users:
|
2016-01-05 13:01:18 -05:00
|
|
|
if not guest_access_token:
|
|
|
|
raise SynapseError(
|
2019-06-20 05:32:02 -04:00
|
|
|
400, "User ID already taken.", errcode=Codes.USER_IN_USE
|
2016-01-05 13:01:18 -05:00
|
|
|
)
|
2016-12-06 10:31:37 -05:00
|
|
|
user_data = yield self.auth.get_user_by_access_token(guest_access_token)
|
2016-01-05 13:01:18 -05:00
|
|
|
if not user_data["is_guest"] or user_data["user"].localpart != localpart:
|
|
|
|
raise AuthError(
|
|
|
|
403,
|
|
|
|
"Cannot register taken user ID without valid guest "
|
|
|
|
"credentials for that user.",
|
|
|
|
errcode=Codes.FORBIDDEN,
|
|
|
|
)
|
2015-04-16 14:56:44 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
@defer.inlineCallbacks
|
2016-01-05 13:01:18 -05:00
|
|
|
def register(
|
|
|
|
self,
|
|
|
|
localpart=None,
|
|
|
|
password=None,
|
|
|
|
generate_token=True,
|
2016-01-06 06:38:09 -05:00
|
|
|
guest_access_token=None,
|
2016-07-05 12:30:22 -04:00
|
|
|
make_guest=False,
|
|
|
|
admin=False,
|
2018-08-31 05:49:14 -04:00
|
|
|
threepid=None,
|
2018-12-14 13:20:59 -05:00
|
|
|
user_type=None,
|
2018-12-07 08:44:46 -05:00
|
|
|
default_display_name=None,
|
2019-03-05 09:25:33 -05:00
|
|
|
address=None,
|
2019-03-28 11:48:07 -04:00
|
|
|
bind_emails=[],
|
2016-01-05 13:01:18 -05:00
|
|
|
):
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Registers a new client on the server.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
localpart : The local part of the user ID to register. If None,
|
2016-02-05 06:22:30 -05:00
|
|
|
one will be generated.
|
2018-08-01 10:54:06 -04:00
|
|
|
password (unicode) : The password to assign to this user so they can
|
2016-07-19 13:46:19 -04:00
|
|
|
login again. This can be None which means they cannot login again
|
|
|
|
via a password (e.g. the user is an application service user).
|
|
|
|
generate_token (bool): Whether a new access token should be
|
|
|
|
generated. Having this be True should be considered deprecated,
|
|
|
|
since it offers no means of associating a device_id with the
|
|
|
|
access_token. Instead you should call auth_handler.issue_access_token
|
|
|
|
after registration.
|
2018-12-14 13:20:59 -05:00
|
|
|
user_type (str|None): type of user. One of the values from
|
|
|
|
api.constants.UserTypes, or None for a normal user.
|
2018-12-07 08:44:46 -05:00
|
|
|
default_display_name (unicode|None): if set, the new user's displayname
|
|
|
|
will be set to this. Defaults to 'localpart'.
|
2019-03-26 13:48:30 -04:00
|
|
|
address (str|None): the IP address used to perform the registration.
|
2019-03-28 11:48:07 -04:00
|
|
|
bind_emails (List[str]): list of emails to bind to this account.
|
2014-08-12 10:10:52 -04:00
|
|
|
Returns:
|
|
|
|
A tuple of (user_id, access_token).
|
|
|
|
Raises:
|
|
|
|
RegistrationError if there was a problem registering.
|
|
|
|
"""
|
2018-08-13 13:00:23 -04:00
|
|
|
|
2018-08-31 05:49:14 -04:00
|
|
|
yield self.auth.check_auth_blocking(threepid=threepid)
|
2014-08-12 10:10:52 -04:00
|
|
|
password_hash = None
|
|
|
|
if password:
|
2019-02-18 11:49:38 -05:00
|
|
|
password_hash = yield self._auth_handler.hash(password)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
if localpart:
|
2016-01-05 13:01:18 -05:00
|
|
|
yield self.check_username(localpart, guest_access_token=guest_access_token)
|
2015-03-18 07:33:46 -04:00
|
|
|
|
2016-02-05 06:22:30 -05:00
|
|
|
was_guest = guest_access_token is not None
|
|
|
|
|
|
|
|
if not was_guest:
|
|
|
|
try:
|
|
|
|
int(localpart)
|
|
|
|
raise RegistrationError(
|
2019-06-20 05:32:02 -04:00
|
|
|
400, "Numeric user IDs are reserved for guest users."
|
2016-02-05 06:22:30 -05:00
|
|
|
)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2014-12-08 04:24:37 -05:00
|
|
|
user = UserID(localpart, self.hs.hostname)
|
2014-08-12 10:10:52 -04:00
|
|
|
user_id = user.to_string()
|
|
|
|
|
2018-12-07 08:44:46 -05:00
|
|
|
if was_guest:
|
|
|
|
# If the user was a guest then they already have a profile
|
|
|
|
default_display_name = None
|
|
|
|
|
|
|
|
elif default_display_name is None:
|
|
|
|
default_display_name = localpart
|
|
|
|
|
2015-11-04 12:29:07 -05:00
|
|
|
token = None
|
|
|
|
if generate_token:
|
2017-02-02 05:53:36 -05:00
|
|
|
token = self.macaroon_gen.generate_access_token(user_id)
|
2019-03-05 09:25:33 -05:00
|
|
|
yield self.register_with_store(
|
2014-10-30 07:10:17 -04:00
|
|
|
user_id=user_id,
|
2014-08-12 10:10:52 -04:00
|
|
|
token=token,
|
2016-01-05 13:01:18 -05:00
|
|
|
password_hash=password_hash,
|
2016-02-05 06:22:30 -05:00
|
|
|
was_guest=was_guest,
|
2016-01-06 12:44:10 -05:00
|
|
|
make_guest=make_guest,
|
2018-12-07 08:44:46 -05:00
|
|
|
create_profile_with_displayname=default_display_name,
|
2016-07-05 12:30:22 -04:00
|
|
|
admin=admin,
|
2018-12-14 13:20:59 -05:00
|
|
|
user_type=user_type,
|
2019-03-05 09:25:33 -05:00
|
|
|
address=address,
|
2014-10-30 07:10:17 -04:00
|
|
|
)
|
2017-11-29 13:33:34 -05:00
|
|
|
|
2017-12-04 09:58:39 -05:00
|
|
|
if self.hs.config.user_directory_search_all_users:
|
2017-11-29 13:33:34 -05:00
|
|
|
profile = yield self.store.get_profileinfo(localpart)
|
|
|
|
yield self.user_directory_handler.handle_local_profile_change(
|
|
|
|
user_id, profile
|
|
|
|
)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
else:
|
2016-02-05 06:22:30 -05:00
|
|
|
# autogen a sequential user ID
|
2014-08-12 10:10:52 -04:00
|
|
|
attempts = 0
|
|
|
|
token = None
|
2016-02-05 06:22:30 -05:00
|
|
|
user = None
|
|
|
|
while not user:
|
|
|
|
localpart = yield self._generate_user_id(attempts > 0)
|
|
|
|
user = UserID(localpart, self.hs.hostname)
|
|
|
|
user_id = user.to_string()
|
2016-02-11 12:37:38 -05:00
|
|
|
yield self.check_user_id_not_appservice_exclusive(user_id)
|
2016-02-05 06:22:30 -05:00
|
|
|
if generate_token:
|
2017-02-02 05:53:36 -05:00
|
|
|
token = self.macaroon_gen.generate_access_token(user_id)
|
2018-12-07 08:44:46 -05:00
|
|
|
if default_display_name is None:
|
|
|
|
default_display_name = localpart
|
2014-08-12 10:10:52 -04:00
|
|
|
try:
|
2019-03-05 09:25:33 -05:00
|
|
|
yield self.register_with_store(
|
2014-08-12 10:10:52 -04:00
|
|
|
user_id=user_id,
|
|
|
|
token=token,
|
2016-02-02 09:42:31 -05:00
|
|
|
password_hash=password_hash,
|
2016-06-17 14:14:16 -04:00
|
|
|
make_guest=make_guest,
|
2018-12-07 08:44:46 -05:00
|
|
|
create_profile_with_displayname=default_display_name,
|
2019-03-05 09:25:33 -05:00
|
|
|
address=address,
|
2016-02-02 09:42:31 -05:00
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
except SynapseError:
|
|
|
|
# if user id is taken, just generate another
|
2016-02-29 17:11:06 -05:00
|
|
|
user = None
|
2014-08-12 10:10:52 -04:00
|
|
|
user_id = None
|
|
|
|
token = None
|
|
|
|
attempts += 1
|
2018-11-28 06:24:57 -05:00
|
|
|
if not self.hs.config.user_consent_at_registration:
|
|
|
|
yield self._auto_join_rooms(user_id)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2019-03-28 11:48:07 -04:00
|
|
|
# Bind any specified emails to this account
|
|
|
|
current_time = self.hs.get_clock().time_msec()
|
|
|
|
for email in bind_emails:
|
|
|
|
# generate threepid dict
|
|
|
|
threepid_dict = {
|
|
|
|
"medium": "email",
|
|
|
|
"address": email,
|
|
|
|
"validated_at": current_time,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Bind email to new account
|
2019-06-20 05:32:02 -04:00
|
|
|
yield self._register_email_threepid(user_id, threepid_dict, None, False)
|
2019-03-28 11:48:07 -04:00
|
|
|
|
2018-11-28 06:24:57 -05:00
|
|
|
defer.returnValue((user_id, token))
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _auto_join_rooms(self, user_id):
|
|
|
|
"""Automatically joins users to auto join rooms - creating the room in the first place
|
|
|
|
if the user is the first to be created.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): The user to join
|
|
|
|
"""
|
2018-03-14 11:45:37 -04:00
|
|
|
# auto-join the user to any rooms we're supposed to dump them into
|
|
|
|
fake_requester = create_requester(user_id)
|
2018-10-04 12:00:27 -04:00
|
|
|
|
2018-12-14 13:20:59 -05:00
|
|
|
# try to create the room if we're the first real user on the server. Note
|
|
|
|
# that an auto-generated support user is not a real user and will never be
|
|
|
|
# the user to create the room
|
2018-10-12 13:17:36 -04:00
|
|
|
should_auto_create_rooms = False
|
2018-12-14 13:20:59 -05:00
|
|
|
is_support = yield self.store.is_support_user(user_id)
|
|
|
|
# There is an edge case where the first user is the support user, then
|
|
|
|
# the room is never created, though this seems unlikely and
|
|
|
|
# recoverable from given the support user being involved in the first
|
|
|
|
# place.
|
|
|
|
if self.hs.config.autocreate_auto_join_rooms and not is_support:
|
2018-10-04 12:00:27 -04:00
|
|
|
count = yield self.store.count_all_users()
|
2018-10-12 13:17:36 -04:00
|
|
|
should_auto_create_rooms = count == 1
|
2018-03-14 11:45:37 -04:00
|
|
|
for r in self.hs.config.auto_join_rooms:
|
|
|
|
try:
|
2018-10-12 13:17:36 -04:00
|
|
|
if should_auto_create_rooms:
|
2018-10-23 12:44:39 -04:00
|
|
|
room_alias = RoomAlias.from_string(r)
|
|
|
|
if self.hs.hostname != room_alias.domain:
|
|
|
|
logger.warning(
|
2019-06-20 05:32:02 -04:00
|
|
|
"Cannot create room alias %s, "
|
|
|
|
"it does not match server domain",
|
2018-10-24 09:39:23 -04:00
|
|
|
r,
|
2018-10-17 06:36:41 -04:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# create room expects the localpart of the room alias
|
2018-10-23 12:44:39 -04:00
|
|
|
room_alias_localpart = room_alias.localpart
|
2018-10-25 12:42:37 -04:00
|
|
|
|
|
|
|
# getting the RoomCreationHandler during init gives a dependency
|
|
|
|
# loop
|
|
|
|
yield self.hs.get_room_creation_handler().create_room(
|
2018-10-17 06:36:41 -04:00
|
|
|
fake_requester,
|
|
|
|
config={
|
|
|
|
"preset": "public_chat",
|
2019-06-20 05:32:02 -04:00
|
|
|
"room_alias_name": room_alias_localpart,
|
2018-10-17 06:36:41 -04:00
|
|
|
},
|
|
|
|
ratelimit=False,
|
2018-10-12 13:17:36 -04:00
|
|
|
)
|
2018-10-13 16:14:21 -04:00
|
|
|
else:
|
|
|
|
yield self._join_user_to_room(fake_requester, r)
|
2019-03-19 07:38:59 -04:00
|
|
|
except ConsentNotGivenError as e:
|
|
|
|
# Technically not necessary to pull out this error though
|
|
|
|
# moving away from bare excepts is a good thing to do.
|
|
|
|
logger.error("Failed to join new user to %r: %r", r, e)
|
2018-03-14 11:45:37 -04:00
|
|
|
except Exception as e:
|
|
|
|
logger.error("Failed to join new user to %r: %r", r, e)
|
|
|
|
|
2018-11-28 06:24:57 -05:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def post_consent_actions(self, user_id):
|
|
|
|
"""A series of registration actions that can only be carried out once consent
|
|
|
|
has been granted
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): The user to join
|
|
|
|
"""
|
|
|
|
yield self._auto_join_rooms(user_id)
|
2014-09-03 13:22:27 -04:00
|
|
|
|
2015-02-05 12:29:27 -05:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def appservice_register(self, user_localpart, as_token):
|
|
|
|
user = UserID(user_localpart, self.hs.hostname)
|
|
|
|
user_id = user.to_string()
|
2016-10-06 04:43:32 -04:00
|
|
|
service = self.store.get_app_service_by_token(as_token)
|
2015-02-05 12:29:27 -05:00
|
|
|
if not service:
|
2015-02-06 12:10:04 -05:00
|
|
|
raise AuthError(403, "Invalid application service token.")
|
2015-02-05 12:29:27 -05:00
|
|
|
if not service.is_interested_in_user(user_id):
|
|
|
|
raise SynapseError(
|
2019-06-20 05:32:02 -04:00
|
|
|
400,
|
|
|
|
"Invalid user localpart for this application service.",
|
|
|
|
errcode=Codes.EXCLUSIVE,
|
2015-02-05 12:29:27 -05:00
|
|
|
)
|
2016-02-11 12:37:38 -05:00
|
|
|
|
2016-03-10 10:58:22 -05:00
|
|
|
service_id = service.id if service.is_exclusive_user(user_id) else None
|
|
|
|
|
2016-02-11 12:37:38 -05:00
|
|
|
yield self.check_user_id_not_appservice_exclusive(
|
|
|
|
user_id, allowed_appservice=service
|
|
|
|
)
|
|
|
|
|
2019-03-05 09:25:33 -05:00
|
|
|
yield self.register_with_store(
|
2015-02-05 12:29:27 -05:00
|
|
|
user_id=user_id,
|
2016-03-10 10:58:22 -05:00
|
|
|
password_hash="",
|
|
|
|
appservice_id=service_id,
|
2018-12-07 08:44:46 -05:00
|
|
|
create_profile_with_displayname=user.localpart,
|
2015-02-05 12:29:27 -05:00
|
|
|
)
|
2016-07-19 13:46:19 -04:00
|
|
|
defer.returnValue(user_id)
|
2015-02-05 12:29:27 -05:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def check_recaptcha(self, ip, private_key, challenge, response):
|
2015-03-30 13:13:10 -04:00
|
|
|
"""
|
|
|
|
Checks a recaptcha is correct.
|
|
|
|
|
|
|
|
Used only by c/s api v1
|
|
|
|
"""
|
2014-09-15 07:42:36 -04:00
|
|
|
|
|
|
|
captcha_response = yield self._validate_captcha(
|
2019-06-20 05:32:02 -04:00
|
|
|
ip, private_key, challenge, response
|
2014-09-15 07:42:36 -04:00
|
|
|
)
|
|
|
|
if not captcha_response["valid"]:
|
2019-06-20 05:32:02 -04:00
|
|
|
logger.info(
|
|
|
|
"Invalid captcha entered from %s. Error: %s",
|
|
|
|
ip,
|
|
|
|
captcha_response["error_url"],
|
2014-09-15 07:42:36 -04:00
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
raise InvalidCaptchaError(error_url=captcha_response["error_url"])
|
2014-09-15 07:42:36 -04:00
|
|
|
else:
|
|
|
|
logger.info("Valid captcha entered from %s", ip)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def register_email(self, threepidCreds):
|
2015-04-15 10:50:38 -04:00
|
|
|
"""
|
|
|
|
Registers emails with an identity server.
|
|
|
|
|
|
|
|
Used only by c/s api v1
|
|
|
|
"""
|
2014-09-15 07:42:36 -04:00
|
|
|
|
|
|
|
for c in threepidCreds:
|
2019-06-20 05:32:02 -04:00
|
|
|
logger.info(
|
|
|
|
"validating threepidcred sid %s on id server %s",
|
|
|
|
c["sid"],
|
|
|
|
c["idServer"],
|
|
|
|
)
|
2014-09-15 07:42:36 -04:00
|
|
|
try:
|
2019-02-20 02:47:31 -05:00
|
|
|
threepid = yield self.identity_handler.threepid_from_creds(c)
|
2017-10-23 10:52:32 -04:00
|
|
|
except Exception:
|
2014-11-03 05:16:28 -05:00
|
|
|
logger.exception("Couldn't validate 3pid")
|
2014-09-15 07:42:36 -04:00
|
|
|
raise RegistrationError(400, "Couldn't validate 3pid")
|
|
|
|
|
|
|
|
if not threepid:
|
|
|
|
raise RegistrationError(400, "Couldn't validate 3pid")
|
2019-06-20 05:32:02 -04:00
|
|
|
logger.info(
|
|
|
|
"got threepid with medium '%s' and address '%s'",
|
|
|
|
threepid["medium"],
|
|
|
|
threepid["address"],
|
|
|
|
)
|
2014-09-15 07:42:36 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
if not check_3pid_allowed(self.hs, threepid["medium"], threepid["address"]):
|
|
|
|
raise RegistrationError(403, "Third party identifier is not allowed")
|
2018-01-18 19:53:58 -05:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def bind_emails(self, user_id, threepidCreds):
|
2015-04-16 14:56:44 -04:00
|
|
|
"""Links emails with a user ID and informs an identity server.
|
|
|
|
|
|
|
|
Used only by c/s api v1
|
|
|
|
"""
|
2014-09-15 07:42:36 -04:00
|
|
|
|
|
|
|
# Now we have a matrix ID, bind it to the threepids we were given
|
|
|
|
for c in threepidCreds:
|
|
|
|
# XXX: This should be a deferred list, shouldn't it?
|
2019-02-20 02:47:31 -05:00
|
|
|
yield self.identity_handler.bind_threepid(c, user_id)
|
2014-09-15 07:42:36 -04:00
|
|
|
|
2016-02-11 12:37:38 -05:00
|
|
|
def check_user_id_not_appservice_exclusive(self, user_id, allowed_appservice=None):
|
2018-05-17 06:34:28 -04:00
|
|
|
# don't allow people to register the server notices mxid
|
|
|
|
if self._server_notices_mxid is not None:
|
|
|
|
if user_id == self._server_notices_mxid:
|
|
|
|
raise SynapseError(
|
2019-06-20 05:32:02 -04:00
|
|
|
400, "This user ID is reserved.", errcode=Codes.EXCLUSIVE
|
2018-05-17 06:34:28 -04:00
|
|
|
)
|
|
|
|
|
2015-02-05 11:46:56 -05:00
|
|
|
# valid user IDs must not clash with any user ID namespaces claimed by
|
|
|
|
# application services.
|
2016-10-06 04:43:32 -04:00
|
|
|
services = self.store.get_app_services()
|
2015-02-05 11:46:56 -05:00
|
|
|
interested_services = [
|
2019-06-20 05:32:02 -04:00
|
|
|
s
|
|
|
|
for s in services
|
|
|
|
if s.is_interested_in_user(user_id) and s != allowed_appservice
|
2015-02-05 11:46:56 -05:00
|
|
|
]
|
2015-02-27 08:51:41 -05:00
|
|
|
for service in interested_services:
|
|
|
|
if service.is_exclusive_user(user_id):
|
|
|
|
raise SynapseError(
|
2019-06-20 05:32:02 -04:00
|
|
|
400,
|
|
|
|
"This user ID is reserved by an application service.",
|
|
|
|
errcode=Codes.EXCLUSIVE,
|
2015-02-27 08:51:41 -05:00
|
|
|
)
|
2015-02-05 11:46:56 -05:00
|
|
|
|
2016-02-05 06:22:30 -05:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _generate_user_id(self, reseed=False):
|
|
|
|
if reseed or self._next_generated_user_id is None:
|
2018-03-26 07:02:20 -04:00
|
|
|
with (yield self._generate_user_id_linearizer.queue(())):
|
2018-03-27 08:01:34 -04:00
|
|
|
if reseed or self._next_generated_user_id is None:
|
|
|
|
self._next_generated_user_id = (
|
|
|
|
yield self.store.find_next_generated_user_id_localpart()
|
|
|
|
)
|
2016-02-05 06:22:30 -05:00
|
|
|
|
|
|
|
id = self._next_generated_user_id
|
|
|
|
self._next_generated_user_id += 1
|
|
|
|
defer.returnValue(str(id))
|
2014-09-03 13:22:27 -04:00
|
|
|
|
2014-09-05 22:18:23 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _validate_captcha(self, ip_addr, private_key, challenge, response):
|
|
|
|
"""Validates the captcha provided.
|
2014-09-15 07:42:36 -04:00
|
|
|
|
2015-03-30 13:13:10 -04:00
|
|
|
Used only by c/s api v1
|
|
|
|
|
2014-09-05 22:18:23 -04:00
|
|
|
Returns:
|
|
|
|
dict: Containing 'valid'(bool) and 'error_url'(str) if invalid.
|
2014-09-15 07:42:36 -04:00
|
|
|
|
2014-09-05 22:18:23 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
response = yield self._submit_captcha(ip_addr, private_key, challenge, response)
|
2014-09-05 22:18:23 -04:00
|
|
|
# parse Google's response. Lovely format..
|
2019-06-20 05:32:02 -04:00
|
|
|
lines = response.split("\n")
|
2014-09-05 22:18:23 -04:00
|
|
|
json = {
|
2019-06-20 05:32:02 -04:00
|
|
|
"valid": lines[0] == "true",
|
|
|
|
"error_url": "http://www.recaptcha.net/recaptcha/api/challenge?"
|
|
|
|
+ "error=%s" % lines[1],
|
2014-09-05 22:18:23 -04:00
|
|
|
}
|
|
|
|
defer.returnValue(json)
|
2014-09-15 07:42:36 -04:00
|
|
|
|
2014-09-05 22:18:23 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _submit_captcha(self, ip_addr, private_key, challenge, response):
|
2015-03-30 13:13:10 -04:00
|
|
|
"""
|
|
|
|
Used only by c/s api v1
|
|
|
|
"""
|
2015-12-03 08:47:50 -05:00
|
|
|
data = yield self.captcha_client.post_urlencoded_get_raw(
|
2019-02-25 14:15:36 -05:00
|
|
|
"http://www.recaptcha.net:80/recaptcha/api/verify",
|
2014-09-15 07:42:36 -04:00
|
|
|
args={
|
2019-06-20 05:32:02 -04:00
|
|
|
"privatekey": private_key,
|
|
|
|
"remoteip": ip_addr,
|
|
|
|
"challenge": challenge,
|
|
|
|
"response": response,
|
|
|
|
},
|
2014-09-05 22:18:23 -04:00
|
|
|
)
|
|
|
|
defer.returnValue(data)
|
2015-08-20 06:35:56 -04:00
|
|
|
|
2016-04-20 10:21:40 -04:00
|
|
|
@defer.inlineCallbacks
|
2019-06-20 05:32:02 -04:00
|
|
|
def get_or_create_user(self, requester, localpart, displayname, password_hash=None):
|
2016-05-23 05:14:11 -04:00
|
|
|
"""Creates a new user if the user does not exist,
|
|
|
|
else revokes all previous access tokens and generates a new one.
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
localpart : The local part of the user ID to register. If None,
|
|
|
|
one will be randomly generated.
|
|
|
|
Returns:
|
|
|
|
A tuple of (user_id, access_token).
|
|
|
|
Raises:
|
|
|
|
RegistrationError if there was a problem registering.
|
2019-06-01 05:42:33 -04:00
|
|
|
|
|
|
|
NB this is only used in tests. TODO: move it to the test package!
|
2016-04-20 10:21:40 -04:00
|
|
|
"""
|
|
|
|
if localpart is None:
|
|
|
|
raise SynapseError(400, "Request must include user id")
|
2018-08-13 13:00:23 -04:00
|
|
|
yield self.auth.check_auth_blocking()
|
2016-04-20 10:21:40 -04:00
|
|
|
need_register = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self.check_username(localpart)
|
|
|
|
except SynapseError as e:
|
|
|
|
if e.errcode == Codes.USER_IN_USE:
|
|
|
|
need_register = False
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
user = UserID(localpart, self.hs.hostname)
|
|
|
|
user_id = user.to_string()
|
2017-02-02 05:53:36 -05:00
|
|
|
token = self.macaroon_gen.generate_access_token(user_id)
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
if need_register:
|
2019-03-05 09:25:33 -05:00
|
|
|
yield self.register_with_store(
|
2016-04-20 10:21:40 -04:00
|
|
|
user_id=user_id,
|
|
|
|
token=token,
|
2016-07-03 02:08:15 -04:00
|
|
|
password_hash=password_hash,
|
2018-12-07 08:44:46 -05:00
|
|
|
create_profile_with_displayname=user.localpart,
|
2016-04-20 10:21:40 -04:00
|
|
|
)
|
|
|
|
else:
|
2017-11-01 06:29:34 -04:00
|
|
|
yield self._auth_handler.delete_access_tokens_for_user(user_id)
|
2016-04-20 10:21:40 -04:00
|
|
|
yield self.store.add_access_token_to_user(user_id=user_id, token=token)
|
|
|
|
|
|
|
|
if displayname is not None:
|
|
|
|
logger.info("setting user display name: %s -> %s", user_id, displayname)
|
2017-08-25 09:34:56 -04:00
|
|
|
yield self.profile_handler.set_displayname(
|
2019-06-20 05:32:02 -04:00
|
|
|
user, requester, displayname, by_admin=True
|
2016-04-20 10:21:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue((user_id, token))
|
|
|
|
|
2016-02-24 09:41:25 -05:00
|
|
|
@defer.inlineCallbacks
|
2018-03-13 08:04:38 -04:00
|
|
|
def get_or_register_3pid_guest(self, medium, address, inviter_user_id):
|
|
|
|
"""Get a guest access token for a 3PID, creating a guest account if
|
|
|
|
one doesn't already exist.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
medium (str)
|
|
|
|
address (str)
|
|
|
|
inviter_user_id (str): The user ID who is trying to invite the
|
|
|
|
3PID
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred[(str, str)]: A 2-tuple of `(user_id, access_token)` of the
|
|
|
|
3PID guest account.
|
|
|
|
"""
|
2016-02-24 09:41:25 -05:00
|
|
|
access_token = yield self.store.get_3pid_guest_access_token(medium, address)
|
|
|
|
if access_token:
|
2019-06-20 05:32:02 -04:00
|
|
|
user_info = yield self.auth.get_user_by_access_token(access_token)
|
2016-02-24 09:41:25 -05:00
|
|
|
|
2018-03-13 08:04:38 -04:00
|
|
|
defer.returnValue((user_info["user"].to_string(), access_token))
|
|
|
|
|
|
|
|
user_id, access_token = yield self.register(
|
2019-06-20 05:32:02 -04:00
|
|
|
generate_token=True, make_guest=True
|
2016-02-24 09:41:25 -05:00
|
|
|
)
|
|
|
|
access_token = yield self.store.save_or_get_3pid_guest_access_token(
|
|
|
|
medium, address, access_token, inviter_user_id
|
|
|
|
)
|
2018-03-13 08:04:38 -04:00
|
|
|
|
|
|
|
defer.returnValue((user_id, access_token))
|
2018-03-14 11:45:37 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _join_user_to_room(self, requester, room_identifier):
|
|
|
|
room_id = None
|
|
|
|
room_member_handler = self.hs.get_room_member_handler()
|
|
|
|
if RoomID.is_valid(room_identifier):
|
|
|
|
room_id = room_identifier
|
|
|
|
elif RoomAlias.is_valid(room_identifier):
|
|
|
|
room_alias = RoomAlias.from_string(room_identifier)
|
|
|
|
room_id, remote_room_hosts = (
|
|
|
|
yield room_member_handler.lookup_room_alias(room_alias)
|
|
|
|
)
|
|
|
|
room_id = room_id.to_string()
|
|
|
|
else:
|
2019-06-20 05:32:02 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400, "%s was not legal room ID or room alias" % (room_identifier,)
|
|
|
|
)
|
2018-03-14 11:45:37 -04:00
|
|
|
|
|
|
|
yield room_member_handler.update_membership(
|
|
|
|
requester=requester,
|
|
|
|
target=requester.user,
|
|
|
|
room_id=room_id,
|
|
|
|
remote_room_hosts=remote_room_hosts,
|
|
|
|
action="join",
|
2018-09-15 17:27:41 -04:00
|
|
|
ratelimit=False,
|
2018-03-14 11:45:37 -04:00
|
|
|
)
|
2019-02-18 07:12:57 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
def register_with_store(
|
|
|
|
self,
|
|
|
|
user_id,
|
|
|
|
token=None,
|
|
|
|
password_hash=None,
|
|
|
|
was_guest=False,
|
|
|
|
make_guest=False,
|
|
|
|
appservice_id=None,
|
|
|
|
create_profile_with_displayname=None,
|
|
|
|
admin=False,
|
|
|
|
user_type=None,
|
|
|
|
address=None,
|
|
|
|
):
|
2019-02-18 07:12:57 -05:00
|
|
|
"""Register user in the datastore.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): The desired user ID to register.
|
|
|
|
token (str): The desired access token to use for this user. If this
|
|
|
|
is not None, the given access token is associated with the user
|
|
|
|
id.
|
|
|
|
password_hash (str|None): Optional. The password hash for this user.
|
|
|
|
was_guest (bool): Optional. Whether this is a guest account being
|
|
|
|
upgraded to a non-guest account.
|
|
|
|
make_guest (boolean): True if the the new user should be guest,
|
|
|
|
false to add a regular user account.
|
|
|
|
appservice_id (str|None): The ID of the appservice registering the user.
|
|
|
|
create_profile_with_displayname (unicode|None): Optionally create a
|
|
|
|
profile for the user, setting their displayname to the given value
|
|
|
|
admin (boolean): is an admin user?
|
|
|
|
user_type (str|None): type of user. One of the values from
|
|
|
|
api.constants.UserTypes, or None for a normal user.
|
2019-03-26 13:48:30 -04:00
|
|
|
address (str|None): the IP address used to perform the registration.
|
2019-02-18 07:12:57 -05:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred
|
|
|
|
"""
|
2019-03-05 09:25:33 -05:00
|
|
|
# Don't rate limit for app services
|
|
|
|
if appservice_id is None and address is not None:
|
|
|
|
time_now = self.clock.time()
|
|
|
|
|
|
|
|
allowed, time_allowed = self.ratelimiter.can_do_action(
|
2019-06-20 05:32:02 -04:00
|
|
|
address,
|
|
|
|
time_now_s=time_now,
|
2019-03-15 13:46:16 -04:00
|
|
|
rate_hz=self.hs.config.rc_registration.per_second,
|
|
|
|
burst_count=self.hs.config.rc_registration.burst_count,
|
2019-03-05 09:25:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if not allowed:
|
|
|
|
raise LimitExceededError(
|
2019-06-20 05:32:02 -04:00
|
|
|
retry_after_ms=int(1000 * (time_allowed - time_now))
|
2019-03-05 09:25:33 -05:00
|
|
|
)
|
|
|
|
|
2019-02-18 07:12:57 -05:00
|
|
|
if self.hs.config.worker_app:
|
|
|
|
return self._register_client(
|
|
|
|
user_id=user_id,
|
|
|
|
token=token,
|
|
|
|
password_hash=password_hash,
|
|
|
|
was_guest=was_guest,
|
|
|
|
make_guest=make_guest,
|
|
|
|
appservice_id=appservice_id,
|
|
|
|
create_profile_with_displayname=create_profile_with_displayname,
|
|
|
|
admin=admin,
|
|
|
|
user_type=user_type,
|
2019-03-05 09:25:33 -05:00
|
|
|
address=address,
|
2019-02-18 07:12:57 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
return self.store.register(
|
|
|
|
user_id=user_id,
|
|
|
|
token=token,
|
|
|
|
password_hash=password_hash,
|
|
|
|
was_guest=was_guest,
|
|
|
|
make_guest=make_guest,
|
|
|
|
appservice_id=appservice_id,
|
|
|
|
create_profile_with_displayname=create_profile_with_displayname,
|
|
|
|
admin=admin,
|
|
|
|
user_type=user_type,
|
|
|
|
)
|
2019-02-18 11:49:38 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2019-06-20 05:32:02 -04:00
|
|
|
def register_device(self, user_id, device_id, initial_display_name, is_guest=False):
|
2019-02-18 11:49:38 -05:00
|
|
|
"""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.
|
|
|
|
is_guest (bool): Whether this is a guest account
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred[tuple[str, str]]: Tuple of device ID and access token
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.hs.config.worker_app:
|
|
|
|
r = yield self._register_device_client(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
initial_display_name=initial_display_name,
|
|
|
|
is_guest=is_guest,
|
|
|
|
)
|
|
|
|
defer.returnValue((r["device_id"], r["access_token"]))
|
|
|
|
else:
|
|
|
|
device_id = yield self.device_handler.check_device_registered(
|
|
|
|
user_id, device_id, initial_display_name
|
|
|
|
)
|
|
|
|
if is_guest:
|
|
|
|
access_token = self.macaroon_gen.generate_access_token(
|
|
|
|
user_id, ["guest = true"]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
access_token = yield self._auth_handler.get_access_token_for_user_id(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_id, device_id=device_id
|
2019-02-18 11:49:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue((device_id, access_token))
|
2019-02-20 02:47:31 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2019-06-20 05:32:02 -04:00
|
|
|
def post_registration_actions(
|
|
|
|
self, user_id, auth_result, access_token, bind_email, bind_msisdn
|
|
|
|
):
|
2019-02-20 02:47:31 -05:00
|
|
|
"""A user has completed registration
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): The user ID that consented
|
|
|
|
auth_result (dict): The authenticated credentials of the newly
|
|
|
|
registered user.
|
|
|
|
access_token (str|None): The access token of the newly logged in
|
|
|
|
device, or None if `inhibit_login` enabled.
|
|
|
|
bind_email (bool): Whether to bind the email with the identity
|
2019-03-26 13:48:30 -04:00
|
|
|
server.
|
2019-02-20 02:47:31 -05:00
|
|
|
bind_msisdn (bool): Whether to bind the msisdn with the identity
|
2019-03-26 13:48:30 -04:00
|
|
|
server.
|
2019-02-20 02:47:31 -05:00
|
|
|
"""
|
|
|
|
if self.hs.config.worker_app:
|
|
|
|
yield self._post_registration_client(
|
|
|
|
user_id=user_id,
|
|
|
|
auth_result=auth_result,
|
|
|
|
access_token=access_token,
|
|
|
|
bind_email=bind_email,
|
|
|
|
bind_msisdn=bind_msisdn,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
if auth_result and LoginType.EMAIL_IDENTITY in auth_result:
|
|
|
|
threepid = auth_result[LoginType.EMAIL_IDENTITY]
|
|
|
|
# Necessary due to auth checks prior to the threepid being
|
|
|
|
# written to the db
|
|
|
|
if is_threepid_reserved(
|
|
|
|
self.hs.config.mau_limits_reserved_threepids, threepid
|
|
|
|
):
|
|
|
|
yield self.store.upsert_monthly_active_user(user_id)
|
|
|
|
|
|
|
|
yield self._register_email_threepid(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_id, threepid, access_token, bind_email
|
2019-02-20 02:47:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if auth_result and LoginType.MSISDN in auth_result:
|
|
|
|
threepid = auth_result[LoginType.MSISDN]
|
2019-06-20 05:32:02 -04:00
|
|
|
yield self._register_msisdn_threepid(user_id, threepid, bind_msisdn)
|
2019-02-20 02:47:31 -05:00
|
|
|
|
|
|
|
if auth_result and LoginType.TERMS in auth_result:
|
2019-06-20 05:32:02 -04:00
|
|
|
yield self._on_user_consented(user_id, self.hs.config.user_consent_version)
|
2019-02-20 02:47:31 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _on_user_consented(self, user_id, consent_version):
|
|
|
|
"""A user consented to the terms on registration
|
|
|
|
|
|
|
|
Args:
|
2019-03-26 13:48:30 -04:00
|
|
|
user_id (str): The user ID that consented.
|
2019-02-20 02:47:31 -05:00
|
|
|
consent_version (str): version of the policy the user has
|
|
|
|
consented to.
|
|
|
|
"""
|
|
|
|
logger.info("%s has consented to the privacy policy", user_id)
|
2019-06-20 05:32:02 -04:00
|
|
|
yield self.store.user_set_consent_version(user_id, consent_version)
|
2019-02-20 02:47:31 -05:00
|
|
|
yield self.post_consent_actions(user_id)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _register_email_threepid(self, user_id, threepid, token, bind_email):
|
|
|
|
"""Add an email address as a 3pid identifier
|
|
|
|
|
|
|
|
Also adds an email pusher for the email address, if configured in the
|
|
|
|
HS config
|
|
|
|
|
|
|
|
Also optionally binds emails to the given user_id on the identity server
|
|
|
|
|
|
|
|
Must be called on master.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): id of user
|
|
|
|
threepid (object): m.login.email.identity auth response
|
|
|
|
token (str|None): access_token for the user, or None if not logged
|
|
|
|
in.
|
|
|
|
bind_email (bool): true if the client requested the email to be
|
|
|
|
bound at the identity server
|
|
|
|
Returns:
|
|
|
|
defer.Deferred:
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
reqd = ("medium", "address", "validated_at")
|
2019-02-20 02:47:31 -05:00
|
|
|
if any(x not in threepid for x in reqd):
|
|
|
|
# This will only happen if the ID server returns a malformed response
|
|
|
|
logger.info("Can't add incomplete 3pid")
|
|
|
|
return
|
|
|
|
|
|
|
|
yield self._auth_handler.add_threepid(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_id, threepid["medium"], threepid["address"], threepid["validated_at"]
|
2019-02-20 02:47:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
# And we add an email pusher for them by default, but only
|
|
|
|
# if email notifications are enabled (so people don't start
|
|
|
|
# getting mail spam where they weren't before if email
|
|
|
|
# notifs are set up on a home server)
|
2019-06-20 05:32:02 -04:00
|
|
|
if (
|
|
|
|
self.hs.config.email_enable_notifs
|
|
|
|
and self.hs.config.email_notif_for_new_users
|
|
|
|
and token
|
|
|
|
):
|
2019-02-20 02:47:31 -05:00
|
|
|
# Pull the ID of the access token back out of the db
|
|
|
|
# It would really make more sense for this to be passed
|
|
|
|
# up when the access token is saved, but that's quite an
|
|
|
|
# invasive change I'd rather do separately.
|
2019-06-20 05:32:02 -04:00
|
|
|
user_tuple = yield self.store.get_user_by_access_token(token)
|
2019-02-20 02:47:31 -05:00
|
|
|
token_id = user_tuple["token_id"]
|
|
|
|
|
|
|
|
yield self.pusher_pool.add_pusher(
|
|
|
|
user_id=user_id,
|
|
|
|
access_token=token_id,
|
|
|
|
kind="email",
|
|
|
|
app_id="m.email",
|
|
|
|
app_display_name="Email Notifications",
|
|
|
|
device_display_name=threepid["address"],
|
|
|
|
pushkey=threepid["address"],
|
|
|
|
lang=None, # We don't know a user's language here
|
|
|
|
data={},
|
|
|
|
)
|
|
|
|
|
|
|
|
if bind_email:
|
|
|
|
logger.info("bind_email specified: binding")
|
2019-06-20 05:32:02 -04:00
|
|
|
logger.debug("Binding emails %s to %s" % (threepid, user_id))
|
2019-02-20 02:47:31 -05:00
|
|
|
yield self.identity_handler.bind_threepid(
|
2019-06-20 05:32:02 -04:00
|
|
|
threepid["threepid_creds"], user_id
|
2019-02-20 02:47:31 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.info("bind_email not specified: not binding email")
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _register_msisdn_threepid(self, user_id, threepid, bind_msisdn):
|
|
|
|
"""Add a phone number as a 3pid identifier
|
|
|
|
|
|
|
|
Also optionally binds msisdn to the given user_id on the identity server
|
|
|
|
|
|
|
|
Must be called on master.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): id of user
|
|
|
|
threepid (object): m.login.msisdn auth response
|
|
|
|
token (str): access_token for the user
|
|
|
|
bind_email (bool): true if the client requested the email to be
|
|
|
|
bound at the identity server
|
|
|
|
Returns:
|
|
|
|
defer.Deferred:
|
|
|
|
"""
|
|
|
|
try:
|
2019-06-20 05:32:02 -04:00
|
|
|
assert_params_in_dict(threepid, ["medium", "address", "validated_at"])
|
2019-02-20 02:47:31 -05:00
|
|
|
except SynapseError as ex:
|
|
|
|
if ex.errcode == Codes.MISSING_PARAM:
|
|
|
|
# This will only happen if the ID server returns a malformed response
|
|
|
|
logger.info("Can't add incomplete 3pid")
|
|
|
|
defer.returnValue(None)
|
|
|
|
raise
|
|
|
|
|
|
|
|
yield self._auth_handler.add_threepid(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_id, threepid["medium"], threepid["address"], threepid["validated_at"]
|
2019-02-20 02:47:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if bind_msisdn:
|
|
|
|
logger.info("bind_msisdn specified: binding")
|
|
|
|
logger.debug("Binding msisdn %s to %s", threepid, user_id)
|
|
|
|
yield self.identity_handler.bind_threepid(
|
2019-06-20 05:32:02 -04:00
|
|
|
threepid["threepid_creds"], user_id
|
2019-02-20 02:47:31 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.info("bind_msisdn not specified: not binding msisdn")
|