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
|
|
|
|
import urllib
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2014-09-06 01:55:29 -04:00
|
|
|
from synapse.api.errors import (
|
2015-04-17 11:46:45 -04:00
|
|
|
AuthError, Codes, SynapseError, RegistrationError, InvalidCaptchaError
|
2014-09-06 01:55:29 -04:00
|
|
|
)
|
2014-10-02 08:57:48 -04:00
|
|
|
from synapse.http.client import CaptchaServerHttpClient
|
2016-07-26 11:46:53 -04:00
|
|
|
from synapse.types import UserID
|
|
|
|
from synapse.util.async import run_on_reactor
|
|
|
|
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):
|
|
|
|
super(RegistrationHandler, self).__init__(hs)
|
|
|
|
|
2016-01-05 13:01:18 -05:00
|
|
|
self.auth = hs.get_auth()
|
2015-12-18 05:06:56 -05:00
|
|
|
self.captcha_client = CaptchaServerHttpClient(hs)
|
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()
|
|
|
|
|
2015-04-16 14:56:44 -04:00
|
|
|
@defer.inlineCallbacks
|
2016-03-16 15:36:57 -04:00
|
|
|
def check_username(self, localpart, guest_access_token=None,
|
|
|
|
assigned_user_id=None):
|
2015-04-16 14:56:44 -04:00
|
|
|
yield run_on_reactor()
|
|
|
|
|
2016-01-20 10:40:25 -05:00
|
|
|
if urllib.quote(localpart.encode('utf-8')) != localpart:
|
2015-04-16 14:56:44 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400,
|
2016-01-22 09:59:49 -05:00
|
|
|
"User ID can only contain characters a-z, 0-9, or '_-./'",
|
2016-01-15 05:06:34 -05:00
|
|
|
Codes.INVALID_USERNAME
|
2015-04-16 14:56:44 -04:00
|
|
|
)
|
|
|
|
|
2017-05-10 12:34:30 -04:00
|
|
|
if not localpart:
|
2017-05-10 12:17:12 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"User ID cannot be empty",
|
|
|
|
Codes.INVALID_USERNAME
|
|
|
|
)
|
|
|
|
|
2016-07-27 12:54:26 -04:00
|
|
|
if localpart[0] == '_':
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"User ID may not begin with _",
|
|
|
|
Codes.INVALID_USERNAME
|
|
|
|
)
|
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
2016-02-11 12:37:38 -05:00
|
|
|
yield self.check_user_id_not_appservice_exclusive(user_id)
|
2015-04-16 14:56:44 -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(
|
|
|
|
400,
|
|
|
|
"User ID already taken.",
|
|
|
|
errcode=Codes.USER_IN_USE,
|
|
|
|
)
|
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,
|
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.
|
2014-08-12 10:10:52 -04:00
|
|
|
password (str) : 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.
|
2014-08-12 10:10:52 -04:00
|
|
|
Returns:
|
|
|
|
A tuple of (user_id, access_token).
|
|
|
|
Raises:
|
|
|
|
RegistrationError if there was a problem registering.
|
|
|
|
"""
|
2014-12-08 04:24:37 -05:00
|
|
|
yield run_on_reactor()
|
2014-08-12 10:10:52 -04:00
|
|
|
password_hash = None
|
|
|
|
if password:
|
2015-08-26 10:59:32 -04:00
|
|
|
password_hash = 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(
|
|
|
|
400,
|
|
|
|
"Numeric user IDs are reserved for guest users."
|
|
|
|
)
|
|
|
|
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()
|
|
|
|
|
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)
|
2014-10-30 07:10:17 -04:00
|
|
|
yield self.store.register(
|
|
|
|
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,
|
2016-06-17 14:14:16 -04:00
|
|
|
create_profile_with_localpart=(
|
2016-06-17 14:18:53 -04:00
|
|
|
# If the user was a guest then they already have a profile
|
2016-06-17 14:14:16 -04:00
|
|
|
None if was_guest else user.localpart
|
|
|
|
),
|
2016-07-05 12:30:22 -04:00
|
|
|
admin=admin,
|
2014-10-30 07:10:17 -04:00
|
|
|
)
|
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)
|
2014-08-12 10:10:52 -04:00
|
|
|
try:
|
|
|
|
yield self.store.register(
|
|
|
|
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,
|
|
|
|
create_profile_with_localpart=user.localpart,
|
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
|
|
|
|
|
2015-12-17 18:04:53 -05:00
|
|
|
# We used to generate default identicons here, but nowadays
|
|
|
|
# we want clients to generate their own as part of their branding
|
|
|
|
# rather than there being consistent matrix-wide ones, so we don't.
|
2015-02-10 11:30:48 -05:00
|
|
|
|
2014-09-03 13:22:27 -04:00
|
|
|
defer.returnValue((user_id, token))
|
|
|
|
|
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(
|
2015-02-06 12:10:04 -05: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
|
|
|
|
)
|
|
|
|
|
2015-02-05 12:29:27 -05:00
|
|
|
yield self.store.register(
|
|
|
|
user_id=user_id,
|
2016-03-10 10:58:22 -05:00
|
|
|
password_hash="",
|
|
|
|
appservice_id=service_id,
|
2016-06-17 14:14:16 -04:00
|
|
|
create_profile_with_localpart=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(
|
|
|
|
ip,
|
|
|
|
private_key,
|
|
|
|
challenge,
|
|
|
|
response
|
|
|
|
)
|
|
|
|
if not captcha_response["valid"]:
|
|
|
|
logger.info("Invalid captcha entered from %s. Error: %s",
|
|
|
|
ip, captcha_response["error_url"])
|
|
|
|
raise InvalidCaptchaError(
|
|
|
|
error_url=captcha_response["error_url"]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.info("Valid captcha entered from %s", ip)
|
|
|
|
|
2015-07-07 08:10:30 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def register_saml2(self, localpart):
|
|
|
|
"""
|
|
|
|
Registers email_id as SAML2 Based Auth.
|
|
|
|
"""
|
|
|
|
if urllib.quote(localpart) != localpart:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"User ID must only contain characters which do not"
|
|
|
|
" require URL encoding."
|
2016-02-02 12:18:50 -05:00
|
|
|
)
|
2015-07-07 08:10:30 -04:00
|
|
|
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)
|
2017-02-02 05:53:36 -05:00
|
|
|
token = self.macaroon_gen.generate_access_token(user_id)
|
2015-07-07 08:10:30 -04:00
|
|
|
try:
|
|
|
|
yield self.store.register(
|
|
|
|
user_id=user_id,
|
|
|
|
token=token,
|
2016-06-17 14:14:16 -04:00
|
|
|
password_hash=None,
|
|
|
|
create_profile_with_localpart=user.localpart,
|
2015-07-07 08:10:30 -04:00
|
|
|
)
|
2016-03-07 15:13:10 -05:00
|
|
|
except Exception as e:
|
2015-07-07 08:10:30 -04:00
|
|
|
yield self.store.add_access_token_to_user(user_id, token)
|
|
|
|
# Ignore Registration errors
|
|
|
|
logger.exception(e)
|
|
|
|
defer.returnValue((user_id, token))
|
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
@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:
|
|
|
|
logger.info("validating theeepidcred sid %s on id server %s",
|
|
|
|
c['sid'], c['idServer'])
|
|
|
|
try:
|
2015-04-16 14:56:44 -04:00
|
|
|
identity_handler = self.hs.get_handlers().identity_handler
|
|
|
|
threepid = yield identity_handler.threepid_from_creds(c)
|
2014-09-15 07:42:36 -04:00
|
|
|
except:
|
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")
|
2014-11-20 12:41:56 -05:00
|
|
|
logger.info("got threepid with medium '%s' and address '%s'",
|
2014-09-15 07:42:36 -04:00
|
|
|
threepid['medium'], threepid['address'])
|
|
|
|
|
|
|
|
@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:
|
2015-04-16 14:56:44 -04:00
|
|
|
identity_handler = self.hs.get_handlers().identity_handler
|
2014-09-15 07:42:36 -04:00
|
|
|
# XXX: This should be a deferred list, shouldn't it?
|
2015-04-16 14:56:44 -04:00
|
|
|
yield 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):
|
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 = [
|
2016-02-11 12:37:38 -05: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(
|
|
|
|
400, "This user ID is reserved by an application service.",
|
|
|
|
errcode=Codes.EXCLUSIVE
|
|
|
|
)
|
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:
|
|
|
|
self._next_generated_user_id = (
|
|
|
|
yield self.store.find_next_generated_user_id_localpart()
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
2014-09-15 07:42:36 -04:00
|
|
|
response = yield self._submit_captcha(ip_addr, private_key, challenge,
|
2014-09-06 01:55:29 -04:00
|
|
|
response)
|
2014-09-05 22:18:23 -04:00
|
|
|
# parse Google's response. Lovely format..
|
|
|
|
lines = response.split('\n')
|
|
|
|
json = {
|
|
|
|
"valid": lines[0] == 'true',
|
2014-09-15 07:42:36 -04:00
|
|
|
"error_url": "http://www.google.com/recaptcha/api/challenge?" +
|
2014-09-06 01:55:29 -04:00
|
|
|
"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(
|
2014-11-20 12:41:56 -05:00
|
|
|
"http://www.google.com:80/recaptcha/api/verify",
|
2014-09-15 07:42:36 -04:00
|
|
|
args={
|
|
|
|
'privatekey': private_key,
|
2014-09-05 22:18:23 -04:00
|
|
|
'remoteip': ip_addr,
|
|
|
|
'challenge': challenge,
|
|
|
|
'response': response
|
|
|
|
}
|
|
|
|
)
|
|
|
|
defer.returnValue(data)
|
2015-08-20 06:35:56 -04:00
|
|
|
|
2016-04-20 10:21:40 -04:00
|
|
|
@defer.inlineCallbacks
|
2016-11-28 04:55:21 -05:00
|
|
|
def get_or_create_user(self, requester, localpart, displayname,
|
2016-07-04 09:07:11 -04:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
yield run_on_reactor()
|
|
|
|
|
|
|
|
if localpart is None:
|
|
|
|
raise SynapseError(400, "Request must include user id")
|
|
|
|
|
|
|
|
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:
|
|
|
|
yield self.store.register(
|
|
|
|
user_id=user_id,
|
|
|
|
token=token,
|
2016-07-03 02:08:15 -04:00
|
|
|
password_hash=password_hash,
|
2016-06-17 14:14:16 -04:00
|
|
|
create_profile_with_localpart=user.localpart,
|
2016-04-20 10:21:40 -04:00
|
|
|
)
|
|
|
|
else:
|
2016-05-23 05:14:11 -04:00
|
|
|
yield self.store.user_delete_access_tokens(user_id=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)
|
|
|
|
profile_handler = self.hs.get_handlers().profile_handler
|
|
|
|
yield profile_handler.set_displayname(
|
2016-10-04 16:32:58 -04:00
|
|
|
user, requester, displayname, by_admin=True,
|
2016-04-20 10:21:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue((user_id, token))
|
|
|
|
|
2015-08-20 06:35:56 -04:00
|
|
|
def auth_handler(self):
|
2016-06-02 08:31:45 -04:00
|
|
|
return self.hs.get_auth_handler()
|
2016-02-24 09:41:25 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def guest_access_token_for(self, medium, address, inviter_user_id):
|
|
|
|
access_token = yield self.store.get_3pid_guest_access_token(medium, address)
|
|
|
|
if access_token:
|
|
|
|
defer.returnValue(access_token)
|
|
|
|
|
|
|
|
_, access_token = yield self.register(
|
|
|
|
generate_token=True,
|
|
|
|
make_guest=True
|
|
|
|
)
|
|
|
|
access_token = yield self.store.save_or_get_3pid_guest_access_token(
|
|
|
|
medium, address, access_token, inviter_user_id
|
|
|
|
)
|
|
|
|
defer.returnValue(access_token)
|