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

Commit

Permalink
Merge pull request #650 from matrix-org/dbkr/register_idempotent_with…
Browse files Browse the repository at this point in the history
…_username

Make registration idempotent, part 2
  • Loading branch information
dbkr committed Mar 17, 2016
2 parents 2ec3460 + 5670205 commit 384ee6e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
14 changes: 14 additions & 0 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ def add_oob_auth(self, stagetype, authdict, clientip):
defer.returnValue(True)
defer.returnValue(False)

def get_session_id(self, clientdict):
"""
Gets the session ID for a client given the client dictionary
:param clientdict: The dictionary sent by the client in the request
:return: The string session ID the client sent. If the client did not
send a session ID, returns None.
"""
sid = None
if clientdict and 'auth' in clientdict:
authdict = clientdict['auth']
if 'session' in authdict:
sid = authdict['session']
return sid

def set_session_data(self, session_id, key, value):
"""
Store a key-value pair into the sessions data associated with this
Expand Down
12 changes: 11 additions & 1 deletion synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def __init__(self, hs):
self._next_generated_user_id = None

@defer.inlineCallbacks
def check_username(self, localpart, guest_access_token=None):
def check_username(self, localpart, guest_access_token=None,
assigned_user_id=None):
yield run_on_reactor()

if urllib.quote(localpart.encode('utf-8')) != localpart:
Expand All @@ -60,6 +61,15 @@ def check_username(self, localpart, guest_access_token=None):
user = UserID(localpart, self.hs.hostname)
user_id = user.to_string()

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",
)

yield self.check_user_id_not_appservice_exclusive(user_id)

users = yield self.store.get_users_by_id_case_insensitive(user_id)
Expand Down
18 changes: 13 additions & 5 deletions synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,22 @@ def on_POST(self, request):

guest_access_token = body.get("guest_access_token", None)

session_id = self.auth_handler.get_session_id(body)
registered_user_id = None
if session_id:
# if we get a registered user id out of here, it means we previously
# registered a user for this session, so we could just return the
# user here. We carry on and go through the auth checks though,
# for paranoia.
registered_user_id = self.auth_handler.get_session_data(
session_id, "registered_user_id", None
)

if desired_username is not None:
yield self.registration_handler.check_username(
desired_username,
guest_access_token=guest_access_token
guest_access_token=guest_access_token,
assigned_user_id=registered_user_id,
)

if self.hs.config.enable_registration_captcha:
Expand All @@ -147,10 +159,6 @@ def on_POST(self, request):
defer.returnValue((401, result))
return

# have we already registered a user for this session
registered_user_id = self.auth_handler.get_session_data(
session_id, "registered_user_id", None
)
if registered_user_id is not None:
logger.info(
"Already registered user ID %r for this session",
Expand Down

0 comments on commit 384ee6e

Please sign in to comment.