2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -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
|
|
|
"""This module contains REST servlets to do with registration: /register"""
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2014-09-05 20:58:06 -04:00
|
|
|
from synapse.api.errors import SynapseError, Codes
|
2014-09-15 05:23:20 -04:00
|
|
|
from synapse.api.constants import LoginType
|
2016-09-09 11:29:10 -04:00
|
|
|
from synapse.api.auth import get_access_token_from_request
|
2016-03-08 06:45:50 -05:00
|
|
|
from .base import ClientV1RestServlet, client_path_patterns
|
2014-09-15 07:42:36 -04:00
|
|
|
import synapse.util.stringutils as stringutils
|
2016-03-09 06:26:26 -05:00
|
|
|
from synapse.http.servlet import parse_json_object_from_request
|
2016-10-04 16:32:58 -04:00
|
|
|
from synapse.types import create_requester
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-12-08 04:24:37 -05:00
|
|
|
from synapse.util.async import run_on_reactor
|
|
|
|
|
2014-09-23 10:58:44 -04:00
|
|
|
from hashlib import sha1
|
|
|
|
import hmac
|
2014-09-15 07:42:36 -04:00
|
|
|
import logging
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-09-23 14:07:16 -04:00
|
|
|
# We ought to be using hmac.compare_digest() but on older pythons it doesn't
|
|
|
|
# exist. It's a _really minor_ security flaw to use plain string comparison
|
|
|
|
# because the timing attack is so obscured by all the other code here it's
|
|
|
|
# unlikely to make much difference
|
|
|
|
if hasattr(hmac, "compare_digest"):
|
|
|
|
compare_digest = hmac.compare_digest
|
|
|
|
else:
|
2016-02-02 12:18:50 -05:00
|
|
|
def compare_digest(a, b):
|
|
|
|
return a == b
|
2014-09-23 14:07:16 -04:00
|
|
|
|
|
|
|
|
2015-01-23 09:09:51 -05:00
|
|
|
class RegisterRestServlet(ClientV1RestServlet):
|
2014-09-15 07:42:36 -04:00
|
|
|
"""Handles registration with the home server.
|
|
|
|
|
|
|
|
This servlet is in control of the registration flow; the registration
|
|
|
|
handler doesn't have a concept of multi-stages or sessions.
|
|
|
|
"""
|
|
|
|
|
2015-12-02 10:45:04 -05:00
|
|
|
PATTERNS = client_path_patterns("/register$", releases=(), include_in_unstable=False)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
def __init__(self, hs):
|
2016-07-19 05:21:42 -04:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
hs (synapse.server.HomeServer): server
|
|
|
|
"""
|
2014-09-15 07:42:36 -04:00
|
|
|
super(RegisterRestServlet, self).__init__(hs)
|
|
|
|
# sessions are stored as:
|
|
|
|
# self.sessions = {
|
|
|
|
# "session_id" : { __session_dict__ }
|
|
|
|
# }
|
|
|
|
# TODO: persistent storage
|
|
|
|
self.sessions = {}
|
2016-02-03 09:42:01 -05:00
|
|
|
self.enable_registration = hs.config.enable_registration
|
2016-07-19 13:46:19 -04:00
|
|
|
self.auth_handler = hs.get_auth_handler()
|
2016-08-12 05:03:19 -04:00
|
|
|
self.handlers = hs.get_handlers()
|
2014-09-15 07:42:36 -04:00
|
|
|
|
2014-09-15 05:23:20 -04:00
|
|
|
def on_GET(self, request):
|
2014-09-15 07:42:36 -04:00
|
|
|
if self.hs.config.enable_registration_captcha:
|
2014-10-30 07:10:17 -04:00
|
|
|
return (
|
|
|
|
200,
|
|
|
|
{"flows": [
|
2014-09-15 07:42:36 -04:00
|
|
|
{
|
|
|
|
"type": LoginType.RECAPTCHA,
|
2014-10-30 07:10:17 -04:00
|
|
|
"stages": [
|
|
|
|
LoginType.RECAPTCHA,
|
|
|
|
LoginType.EMAIL_IDENTITY,
|
|
|
|
LoginType.PASSWORD
|
|
|
|
]
|
2014-09-15 07:42:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": LoginType.RECAPTCHA,
|
|
|
|
"stages": [LoginType.RECAPTCHA, LoginType.PASSWORD]
|
|
|
|
}
|
2014-10-30 07:10:17 -04:00
|
|
|
]}
|
|
|
|
)
|
2014-09-15 07:42:36 -04:00
|
|
|
else:
|
2014-10-30 07:10:17 -04:00
|
|
|
return (
|
|
|
|
200,
|
|
|
|
{"flows": [
|
2014-09-15 07:42:36 -04:00
|
|
|
{
|
|
|
|
"type": LoginType.EMAIL_IDENTITY,
|
2014-10-30 07:10:17 -04:00
|
|
|
"stages": [
|
|
|
|
LoginType.EMAIL_IDENTITY, LoginType.PASSWORD
|
|
|
|
]
|
2014-09-15 07:42:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": LoginType.PASSWORD
|
|
|
|
}
|
2014-10-30 07:10:17 -04:00
|
|
|
]}
|
|
|
|
)
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_POST(self, request):
|
2016-03-09 06:26:26 -05:00
|
|
|
register_json = parse_json_object_from_request(request)
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2014-10-30 07:10:17 -04:00
|
|
|
session = (register_json["session"]
|
|
|
|
if "session" in register_json else None)
|
2014-09-15 11:05:51 -04:00
|
|
|
login_type = None
|
|
|
|
if "type" not in register_json:
|
|
|
|
raise SynapseError(400, "Missing 'type' key.")
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
try:
|
2014-09-15 05:23:20 -04:00
|
|
|
login_type = register_json["type"]
|
2015-02-20 06:39:53 -05:00
|
|
|
|
|
|
|
is_application_server = login_type == LoginType.APPLICATION_SERVICE
|
2015-03-13 11:23:37 -04:00
|
|
|
is_using_shared_secret = login_type == LoginType.SHARED_SECRET
|
|
|
|
|
|
|
|
can_register = (
|
2016-02-03 09:42:01 -05:00
|
|
|
self.enable_registration
|
2015-03-13 11:23:37 -04:00
|
|
|
or is_application_server
|
|
|
|
or is_using_shared_secret
|
|
|
|
)
|
|
|
|
if not can_register:
|
2015-02-20 06:39:53 -05:00
|
|
|
raise SynapseError(403, "Registration has been disabled")
|
|
|
|
|
2014-09-15 05:23:20 -04:00
|
|
|
stages = {
|
|
|
|
LoginType.RECAPTCHA: self._do_recaptcha,
|
|
|
|
LoginType.PASSWORD: self._do_password,
|
2015-02-05 12:29:27 -05:00
|
|
|
LoginType.EMAIL_IDENTITY: self._do_email_identity,
|
2015-03-13 11:23:37 -04:00
|
|
|
LoginType.APPLICATION_SERVICE: self._do_app_service,
|
|
|
|
LoginType.SHARED_SECRET: self._do_shared_secret,
|
2014-09-15 05:23:20 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
session_info = self._get_session_info(request, session)
|
|
|
|
logger.debug("%s : session info %s request info %s",
|
|
|
|
login_type, session_info, register_json)
|
|
|
|
response = yield stages[login_type](
|
|
|
|
request,
|
|
|
|
register_json,
|
|
|
|
session_info
|
|
|
|
)
|
|
|
|
|
|
|
|
if "access_token" not in response:
|
|
|
|
# isn't a final response
|
|
|
|
response["session"] = session_info["id"]
|
2014-09-15 05:23:20 -04:00
|
|
|
|
|
|
|
defer.returnValue((200, response))
|
2014-09-15 07:42:36 -04:00
|
|
|
except KeyError as e:
|
|
|
|
logger.exception(e)
|
2014-10-30 07:10:17 -04:00
|
|
|
raise SynapseError(400, "Missing JSON keys for login type %s." % (
|
|
|
|
login_type,
|
|
|
|
))
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
def on_OPTIONS(self, request):
|
|
|
|
return (200, {})
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
def _get_session_info(self, request, session_id):
|
|
|
|
if not session_id:
|
|
|
|
# create a new session
|
|
|
|
while session_id is None or session_id in self.sessions:
|
|
|
|
session_id = stringutils.random_string(24)
|
|
|
|
self.sessions[session_id] = {
|
|
|
|
"id": session_id,
|
|
|
|
LoginType.EMAIL_IDENTITY: False,
|
|
|
|
LoginType.RECAPTCHA: False
|
|
|
|
}
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
return self.sessions[session_id]
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
def _save_session(self, session):
|
|
|
|
# TODO: Persistent storage
|
|
|
|
logger.debug("Saving session %s", session)
|
|
|
|
self.sessions[session["id"]] = session
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
def _remove_session(self, session):
|
|
|
|
logger.debug("Removing session %s", session)
|
|
|
|
self.sessions.pop(session["id"])
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2014-09-15 09:52:39 -04:00
|
|
|
@defer.inlineCallbacks
|
2014-09-15 07:42:36 -04:00
|
|
|
def _do_recaptcha(self, request, register_json, session):
|
|
|
|
if not self.hs.config.enable_registration_captcha:
|
|
|
|
raise SynapseError(400, "Captcha not required.")
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2014-09-23 10:58:44 -04:00
|
|
|
yield self._check_recaptcha(request, register_json, session)
|
2014-09-23 09:29:08 -04:00
|
|
|
|
|
|
|
session[LoginType.RECAPTCHA] = True # mark captcha as done
|
|
|
|
self._save_session(session)
|
|
|
|
defer.returnValue({
|
|
|
|
"next": [LoginType.PASSWORD, LoginType.EMAIL_IDENTITY]
|
|
|
|
})
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-09-23 10:58:44 -04:00
|
|
|
def _check_recaptcha(self, request, register_json, session):
|
|
|
|
if ("captcha_bypass_hmac" in register_json and
|
|
|
|
self.hs.config.captcha_bypass_secret):
|
|
|
|
if "user" not in register_json:
|
|
|
|
raise SynapseError(400, "Captcha bypass needs 'user'")
|
|
|
|
|
|
|
|
want = hmac.new(
|
|
|
|
key=self.hs.config.captcha_bypass_secret,
|
|
|
|
msg=register_json["user"],
|
|
|
|
digestmod=sha1,
|
|
|
|
).hexdigest()
|
|
|
|
|
|
|
|
# str() because otherwise hmac complains that 'unicode' does not
|
|
|
|
# have the buffer interface
|
|
|
|
got = str(register_json["captcha_bypass_hmac"])
|
|
|
|
|
2014-09-23 14:07:16 -04:00
|
|
|
if compare_digest(want, got):
|
2014-09-23 10:58:44 -04:00
|
|
|
session["user"] = register_json["user"]
|
2014-09-23 09:29:08 -04:00
|
|
|
defer.returnValue(None)
|
|
|
|
else:
|
2014-10-30 07:10:17 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400, "Captcha bypass HMAC incorrect",
|
|
|
|
errcode=Codes.CAPTCHA_NEEDED
|
|
|
|
)
|
2014-09-23 09:29:08 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
challenge = None
|
|
|
|
user_response = None
|
|
|
|
try:
|
|
|
|
challenge = register_json["challenge"]
|
|
|
|
user_response = register_json["response"]
|
|
|
|
except KeyError:
|
|
|
|
raise SynapseError(400, "Captcha response is required",
|
|
|
|
errcode=Codes.CAPTCHA_NEEDED)
|
|
|
|
|
2014-09-26 11:36:24 -04:00
|
|
|
ip_addr = self.hs.get_ip_from_request(request)
|
2014-09-03 13:22:27 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
handler = self.handlers.registration_handler
|
2014-09-15 07:42:36 -04:00
|
|
|
yield handler.check_recaptcha(
|
|
|
|
ip_addr,
|
|
|
|
self.hs.config.recaptcha_private_key,
|
|
|
|
challenge,
|
|
|
|
user_response
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _do_email_identity(self, request, register_json, session):
|
|
|
|
if (self.hs.config.enable_registration_captcha and
|
|
|
|
not session[LoginType.RECAPTCHA]):
|
|
|
|
raise SynapseError(400, "Captcha is required.")
|
|
|
|
|
|
|
|
threepidCreds = register_json['threepidCreds']
|
|
|
|
handler = self.handlers.registration_handler
|
2014-11-20 12:41:56 -05:00
|
|
|
logger.debug("Registering email. threepidcreds: %s" % (threepidCreds))
|
2014-09-15 07:42:36 -04:00
|
|
|
yield handler.register_email(threepidCreds)
|
|
|
|
session["threepidCreds"] = threepidCreds # store creds for next stage
|
|
|
|
session[LoginType.EMAIL_IDENTITY] = True # mark email as done
|
|
|
|
self._save_session(session)
|
|
|
|
defer.returnValue({
|
|
|
|
"next": LoginType.PASSWORD
|
|
|
|
})
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _do_password(self, request, register_json, session):
|
2014-12-08 04:24:37 -05:00
|
|
|
yield run_on_reactor()
|
2014-09-15 07:42:36 -04:00
|
|
|
if (self.hs.config.enable_registration_captcha and
|
|
|
|
not session[LoginType.RECAPTCHA]):
|
|
|
|
# captcha should've been done by this stage!
|
|
|
|
raise SynapseError(400, "Captcha is required.")
|
|
|
|
|
2014-09-23 10:58:44 -04:00
|
|
|
if ("user" in session and "user" in register_json and
|
|
|
|
session["user"] != register_json["user"]):
|
2014-10-30 07:10:17 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400, "Cannot change user ID during registration"
|
|
|
|
)
|
2014-09-23 10:58:44 -04:00
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
password = register_json["password"].encode("utf-8")
|
2015-03-18 07:33:46 -04:00
|
|
|
desired_user_id = (
|
|
|
|
register_json["user"].encode("utf-8")
|
|
|
|
if "user" in register_json else None
|
|
|
|
)
|
|
|
|
|
2014-09-15 07:42:36 -04:00
|
|
|
handler = self.handlers.registration_handler
|
2014-08-12 10:10:52 -04:00
|
|
|
(user_id, token) = yield handler.register(
|
|
|
|
localpart=desired_user_id,
|
2014-09-15 07:42:36 -04:00
|
|
|
password=password
|
|
|
|
)
|
|
|
|
|
|
|
|
if session[LoginType.EMAIL_IDENTITY]:
|
2014-11-20 13:00:10 -05:00
|
|
|
logger.debug("Binding emails %s to %s" % (
|
|
|
|
session["threepidCreds"], user_id)
|
|
|
|
)
|
2014-09-15 07:42:36 -04:00
|
|
|
yield handler.bind_emails(user_id, session["threepidCreds"])
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
result = {
|
|
|
|
"user_id": user_id,
|
|
|
|
"access_token": token,
|
|
|
|
"home_server": self.hs.hostname,
|
|
|
|
}
|
2014-09-15 07:42:36 -04:00
|
|
|
self._remove_session(session)
|
|
|
|
defer.returnValue(result)
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2015-02-05 12:29:27 -05:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _do_app_service(self, request, register_json, session):
|
2016-09-09 11:29:10 -04:00
|
|
|
as_token = get_access_token_from_request(request)
|
|
|
|
|
2015-02-05 12:29:27 -05:00
|
|
|
if "user" not in register_json:
|
|
|
|
raise SynapseError(400, "Expected 'user' key.")
|
|
|
|
|
|
|
|
user_localpart = register_json["user"].encode("utf-8")
|
|
|
|
|
|
|
|
handler = self.handlers.registration_handler
|
2016-07-19 13:46:19 -04:00
|
|
|
user_id = yield handler.appservice_register(
|
2015-02-05 12:29:27 -05:00
|
|
|
user_localpart, as_token
|
|
|
|
)
|
2016-07-19 13:46:19 -04:00
|
|
|
token = yield self.auth_handler.issue_access_token(user_id)
|
2015-02-05 12:29:27 -05:00
|
|
|
self._remove_session(session)
|
|
|
|
defer.returnValue({
|
|
|
|
"user_id": user_id,
|
|
|
|
"access_token": token,
|
|
|
|
"home_server": self.hs.hostname,
|
|
|
|
})
|
|
|
|
|
2015-03-13 11:23:37 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _do_shared_secret(self, request, register_json, session):
|
|
|
|
yield run_on_reactor()
|
|
|
|
|
2015-03-18 07:30:04 -04:00
|
|
|
if not isinstance(register_json.get("mac", None), basestring):
|
2015-03-13 11:23:37 -04:00
|
|
|
raise SynapseError(400, "Expected mac.")
|
2015-03-18 07:30:04 -04:00
|
|
|
if not isinstance(register_json.get("user", None), basestring):
|
2015-03-13 11:23:37 -04:00
|
|
|
raise SynapseError(400, "Expected 'user' key.")
|
2015-03-18 07:30:04 -04:00
|
|
|
if not isinstance(register_json.get("password", None), basestring):
|
2015-03-13 11:23:37 -04:00
|
|
|
raise SynapseError(400, "Expected 'password' key.")
|
|
|
|
|
|
|
|
if not self.hs.config.registration_shared_secret:
|
|
|
|
raise SynapseError(400, "Shared secret registration is not enabled")
|
|
|
|
|
|
|
|
user = register_json["user"].encode("utf-8")
|
2016-07-05 12:18:19 -04:00
|
|
|
password = register_json["password"].encode("utf-8")
|
|
|
|
admin = register_json.get("admin", None)
|
2015-03-13 11:23:37 -04:00
|
|
|
|
2016-07-06 06:16:10 -04:00
|
|
|
# Its important to check as we use null bytes as HMAC field separators
|
|
|
|
if "\x00" in user:
|
|
|
|
raise SynapseError(400, "Invalid user")
|
|
|
|
if "\x00" in password:
|
|
|
|
raise SynapseError(400, "Invalid password")
|
|
|
|
|
2015-03-13 11:23:37 -04:00
|
|
|
# str() because otherwise hmac complains that 'unicode' does not
|
|
|
|
# have the buffer interface
|
|
|
|
got_mac = str(register_json["mac"])
|
|
|
|
|
|
|
|
want_mac = hmac.new(
|
|
|
|
key=self.hs.config.registration_shared_secret,
|
|
|
|
digestmod=sha1,
|
2016-07-05 12:18:19 -04:00
|
|
|
)
|
|
|
|
want_mac.update(user)
|
2016-07-06 06:04:44 -04:00
|
|
|
want_mac.update("\x00")
|
2016-07-05 12:18:19 -04:00
|
|
|
want_mac.update(password)
|
2016-07-06 06:04:44 -04:00
|
|
|
want_mac.update("\x00")
|
2016-07-05 12:18:19 -04:00
|
|
|
want_mac.update("admin" if admin else "notadmin")
|
|
|
|
want_mac = want_mac.hexdigest()
|
2015-03-13 11:23:37 -04:00
|
|
|
|
|
|
|
if compare_digest(want_mac, got_mac):
|
|
|
|
handler = self.handlers.registration_handler
|
|
|
|
user_id, token = yield handler.register(
|
|
|
|
localpart=user,
|
|
|
|
password=password,
|
2016-07-05 12:30:22 -04:00
|
|
|
admin=bool(admin),
|
2015-03-13 11:23:37 -04:00
|
|
|
)
|
|
|
|
self._remove_session(session)
|
|
|
|
defer.returnValue({
|
|
|
|
"user_id": user_id,
|
|
|
|
"access_token": token,
|
|
|
|
"home_server": self.hs.hostname,
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
raise SynapseError(
|
2015-03-16 09:11:42 -04:00
|
|
|
403, "HMAC incorrect",
|
2015-03-13 11:23:37 -04:00
|
|
|
)
|
|
|
|
|
2014-09-15 05:23:20 -04:00
|
|
|
|
2016-04-20 10:21:40 -04:00
|
|
|
class CreateUserRestServlet(ClientV1RestServlet):
|
|
|
|
"""Handles user creation via a server-to-server interface
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = client_path_patterns("/createUser$", releases=())
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(CreateUserRestServlet, self).__init__(hs)
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.direct_user_creation_max_duration = hs.config.user_creation_max_duration
|
2016-08-12 05:03:19 -04:00
|
|
|
self.handlers = hs.get_handlers()
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_POST(self, request):
|
|
|
|
user_json = parse_json_object_from_request(request)
|
|
|
|
|
2016-09-09 11:29:10 -04:00
|
|
|
access_token = get_access_token_from_request(request)
|
2016-10-06 04:43:32 -04:00
|
|
|
app_service = self.store.get_app_service_by_token(
|
2016-09-09 11:29:10 -04:00
|
|
|
access_token
|
2016-04-20 10:21:40 -04:00
|
|
|
)
|
|
|
|
if not app_service:
|
|
|
|
raise SynapseError(403, "Invalid application service token.")
|
|
|
|
|
2016-10-04 16:32:58 -04:00
|
|
|
requester = create_requester(app_service.sender)
|
2016-04-20 10:21:40 -04:00
|
|
|
|
2016-10-04 16:32:58 -04:00
|
|
|
logger.debug("creating user: %s", user_json)
|
|
|
|
response = yield self._do_create(requester, user_json)
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
defer.returnValue((200, response))
|
|
|
|
|
|
|
|
def on_OPTIONS(self, request):
|
|
|
|
return 403, {}
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-10-04 16:32:58 -04:00
|
|
|
def _do_create(self, requester, user_json):
|
2016-04-20 10:21:40 -04:00
|
|
|
yield run_on_reactor()
|
|
|
|
|
|
|
|
if "localpart" not in user_json:
|
|
|
|
raise SynapseError(400, "Expected 'localpart' key.")
|
|
|
|
|
|
|
|
if "displayname" not in user_json:
|
|
|
|
raise SynapseError(400, "Expected 'displayname' key.")
|
|
|
|
|
|
|
|
if "duration_seconds" not in user_json:
|
|
|
|
raise SynapseError(400, "Expected 'duration_seconds' key.")
|
|
|
|
|
|
|
|
localpart = user_json["localpart"].encode("utf-8")
|
|
|
|
displayname = user_json["displayname"].encode("utf-8")
|
|
|
|
duration_seconds = 0
|
|
|
|
try:
|
|
|
|
duration_seconds = int(user_json["duration_seconds"])
|
|
|
|
except ValueError:
|
|
|
|
raise SynapseError(400, "Failed to parse 'duration_seconds'")
|
|
|
|
if duration_seconds > self.direct_user_creation_max_duration:
|
|
|
|
duration_seconds = self.direct_user_creation_max_duration
|
2016-07-04 09:07:11 -04:00
|
|
|
password_hash = user_json["password_hash"].encode("utf-8") \
|
|
|
|
if user_json.get("password_hash") else None
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
handler = self.handlers.registration_handler
|
|
|
|
user_id, token = yield handler.get_or_create_user(
|
2016-10-04 16:32:58 -04:00
|
|
|
requester=requester,
|
2016-04-20 10:21:40 -04:00
|
|
|
localpart=localpart,
|
|
|
|
displayname=displayname,
|
2016-07-08 10:53:18 -04:00
|
|
|
duration_in_ms=(duration_seconds * 1000),
|
2016-07-03 02:08:15 -04:00
|
|
|
password_hash=password_hash
|
2016-04-20 10:21:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue({
|
|
|
|
"user_id": user_id,
|
|
|
|
"access_token": token,
|
|
|
|
"home_server": self.hs.hostname,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
def register_servlets(hs, http_server):
|
|
|
|
RegisterRestServlet(hs).register(http_server)
|
2016-04-20 10:21:40 -04:00
|
|
|
CreateUserRestServlet(hs).register(http_server)
|