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

Commit

Permalink
Move register_device into handler
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Feb 18, 2019
1 parent 8b9ae6d commit af691e4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 172 deletions.
51 changes: 47 additions & 4 deletions synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SynapseError,
)
from synapse.http.client import CaptchaServerHttpClient
from synapse.replication.http.login import RegisterDeviceReplicationServlet
from synapse.replication.http.register import ReplicationRegisterServlet
from synapse.types import RoomAlias, RoomID, UserID, create_requester
from synapse.util.async_helpers import Linearizer
Expand Down Expand Up @@ -64,6 +65,11 @@ def __init__(self, hs):

if hs.config.worker_app:
self._register_client = ReplicationRegisterServlet.make_client(hs)
self._register_device_client = (
RegisterDeviceReplicationServlet.make_client(hs)
)
else:
self.device_handler = hs.get_device_handler()

@defer.inlineCallbacks
def check_username(self, localpart, guest_access_token=None,
Expand Down Expand Up @@ -159,7 +165,7 @@ def register(
yield self.auth.check_auth_blocking(threepid=threepid)
password_hash = None
if password:
password_hash = yield self.auth_handler().hash(password)
password_hash = yield self._auth_handler.hash(password)

if localpart:
yield self.check_username(localpart, guest_access_token=guest_access_token)
Expand Down Expand Up @@ -516,9 +522,6 @@ def get_or_create_user(self, requester, localpart, displayname,

defer.returnValue((user_id, token))

def auth_handler(self):
return self.hs.get_auth_handler()

@defer.inlineCallbacks
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
Expand Down Expand Up @@ -628,3 +631,43 @@ def _register_with_store(self, user_id, token=None, password_hash=None,
admin=admin,
user_type=user_type,
)

@defer.inlineCallbacks
def register_device(self, user_id, device_id, initial_display_name,
is_guest=False):
"""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(
user_id, device_id=device_id,
)

defer.returnValue((device_id, access_token))
17 changes: 3 additions & 14 deletions synapse/replication/http/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class RegisterDeviceReplicationServlet(ReplicationEndpoint):

def __init__(self, hs):
super(RegisterDeviceReplicationServlet, self).__init__(hs)
self.auth_handler = hs.get_auth_handler()
self.device_handler = hs.get_device_handler()
self.macaroon_gen = hs.get_macaroon_generator()
self.registration_handler = hs.get_handlers().registration_handler

@staticmethod
def _serialize_payload(user_id, device_id, initial_display_name, is_guest):
Expand All @@ -62,19 +60,10 @@ def _handle_request(self, request, user_id):
initial_display_name = content["initial_display_name"]
is_guest = content["is_guest"]

device_id = yield self.device_handler.check_device_registered(
user_id, device_id, initial_display_name,
device_id, access_token = yield self.registration_handler.register_device(
user_id, device_id, initial_display_name, is_guest,
)

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(
user_id, device_id=device_id,
)

defer.returnValue((200, {
"device_id": device_id,
"access_token": access_token,
Expand Down
59 changes: 22 additions & 37 deletions synapse/rest/client/v1/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(self, hs):
self.jwt_algorithm = hs.config.jwt_algorithm
self.cas_enabled = hs.config.cas_enabled
self.auth_handler = self.hs.get_auth_handler()
self.device_handler = self.hs.get_device_handler()
self.registration_handler = hs.get_handlers().registration_handler
self.handlers = hs.get_handlers()
self._well_known_builder = WellKnownBuilder(hs)

Expand Down Expand Up @@ -220,11 +220,10 @@ def _do_other_login(self, login_submission):
login_submission,
)

device_id = yield self._register_device(
canonical_user_id, login_submission,
)
access_token = yield auth_handler.get_access_token_for_user_id(
canonical_user_id, device_id,
device_id = login_submission.get("device_id")
initial_display_name = login_submission.get("initial_device_display_name")
device_id, access_token = yield self.registration_handler.register_device(
canonical_user_id, device_id, initial_display_name,
)

result = {
Expand All @@ -246,10 +245,13 @@ def do_token_login(self, login_submission):
user_id = (
yield auth_handler.validate_short_term_login_token_and_get_user_id(token)
)
device_id = yield self._register_device(user_id, login_submission)
access_token = yield auth_handler.get_access_token_for_user_id(
user_id, device_id,

device_id = login_submission.get("device_id")
initial_display_name = login_submission.get("initial_device_display_name")
device_id, access_token = yield self.registration_handler.register_device(
user_id, device_id, initial_display_name,
)

result = {
"user_id": user_id, # may have changed
"access_token": access_token,
Expand Down Expand Up @@ -286,11 +288,10 @@ def do_jwt_login(self, login_submission):
auth_handler = self.auth_handler
registered_user_id = yield auth_handler.check_user_exists(user_id)
if registered_user_id:
device_id = yield self._register_device(
registered_user_id, login_submission
)
access_token = yield auth_handler.get_access_token_for_user_id(
registered_user_id, device_id,
device_id = login_submission.get("device_id")
initial_display_name = login_submission.get("initial_device_display_name")
device_id, access_token = yield self.registration_handler.register_device(
registered_user_id, device_id, initial_display_name,
)

result = {
Expand All @@ -299,12 +300,16 @@ def do_jwt_login(self, login_submission):
"home_server": self.hs.hostname,
}
else:
# TODO: we should probably check that the register isn't going
# to fonx/change our user_id before registering the device
device_id = yield self._register_device(user_id, login_submission)
user_id, access_token = (
yield self.handlers.registration_handler.register(localpart=user)
)

device_id = login_submission.get("device_id")
initial_display_name = login_submission.get("initial_device_display_name")
device_id, access_token = yield self.registration_handler.register_device(
registered_user_id, device_id, initial_display_name,
)

result = {
"user_id": user_id, # may have changed
"access_token": access_token,
Expand All @@ -313,26 +318,6 @@ def do_jwt_login(self, login_submission):

defer.returnValue(result)

def _register_device(self, user_id, login_submission):
"""Register a device for a user.
This is called after the user's credentials have been validated, but
before the access token has been issued.
Args:
(str) user_id: full canonical @user:id
(object) login_submission: dictionary supplied to /login call, from
which we pull device_id and initial_device_name
Returns:
defer.Deferred: (str) device_id
"""
device_id = login_submission.get("device_id")
initial_display_name = login_submission.get(
"initial_device_display_name")
return self.device_handler.check_device_registered(
user_id, device_id, initial_display_name
)


class CasRedirectServlet(RestServlet):
PATTERNS = client_path_patterns("/login/(cas|sso)/redirect")
Expand Down
49 changes: 2 additions & 47 deletions synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
parse_json_object_from_request,
parse_string,
)
from synapse.replication.http.login import RegisterDeviceReplicationServlet
from synapse.util.msisdn import phone_number_to_msisdn
from synapse.util.ratelimitutils import FederationRateLimiter
from synapse.util.threepids import check_3pid_allowed
Expand Down Expand Up @@ -193,13 +192,6 @@ def __init__(self, hs):
self.room_member_handler = hs.get_room_member_handler()
self.macaroon_gen = hs.get_macaroon_generator()

if self.hs.config.worker_app:
self._register_device_client = (
RegisterDeviceReplicationServlet.make_client(hs)
)
else:
self.device_handler = hs.get_device_handler()

@interactive_auth_handler
@defer.inlineCallbacks
def on_POST(self, request):
Expand Down Expand Up @@ -642,7 +634,7 @@ def _create_registration_details(self, user_id, params):
if not params.get("inhibit_login", False):
device_id = params.get("device_id")
initial_display_name = params.get("initial_device_display_name")
device_id, access_token = yield self._register_device(
device_id, access_token = yield self.registration_handler.register_device(
user_id, device_id, initial_display_name, is_guest=False,
)

Expand All @@ -652,43 +644,6 @@ def _create_registration_details(self, user_id, params):
})
defer.returnValue(result)

@defer.inlineCallbacks
def _register_device(self, user_id, device_id, initial_display_name,
is_guest):
"""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(
user_id, device_id=device_id,
)
defer.returnValue((device_id, access_token))

@defer.inlineCallbacks
def _do_guest_registration(self, params):
if not self.hs.config.allow_guest_access:
Expand All @@ -702,7 +657,7 @@ def _do_guest_registration(self, params):
# we have nowhere to store it.
device_id = synapse.api.auth.GUEST_DEVICE_ID
initial_display_name = params.get("initial_device_display_name")
device_id, access_token = yield self._register_device(
device_id, access_token = yield self.registration_handler.register_device(
user_id, device_id, initial_display_name, is_guest=True,
)

Expand Down
Loading

0 comments on commit af691e4

Please sign in to comment.