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

Changes for unique emails #211

Merged
merged 7 commits into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Codes(object):
TOO_LARGE = "M_TOO_LARGE"
EXCLUSIVE = "M_EXCLUSIVE"
THREEPID_AUTH_FAILED = "M_THREEPID_AUTH_FAILED"
THREEPID_IN_USE = "THREEPID_IN_USE"


class CodeMessageException(RuntimeError):
Expand Down
25 changes: 25 additions & 0 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,28 @@ def bind_threepid(self, creds, mxid):
except CodeMessageException as e:
data = json.loads(e.msg)
defer.returnValue(data)

@defer.inlineCallbacks
def requestEmailToken(self, id_server, email, client_secret, send_attempt, **kwargs):
yield run_on_reactor()
http_client = SimpleHttpClient(self.hs)

params = {
'email': email,
'client_secret': client_secret,
'send_attempt': send_attempt,
}
params.update(kwargs)

try:
data = yield http_client.post_urlencoded_get_json(
"https://%s%s" % (
id_server,
"/_matrix/identity/api/v1/validate/email/requestToken"
),
params
)
defer.returnValue(data)
except CodeMessageException as e:
logger.info("Proxied requestToken failed: %r", e)
raise e
28 changes: 28 additions & 0 deletions synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def __init__(self, hs):
@defer.inlineCallbacks
def on_POST(self, request):
yield run_on_reactor()

if '/register/email/requestToken' in request.path:
ret = yield self.onEmailTokenRequest(request)
defer.returnValue(ret)

body = parse_json_dict_from_request(request)

# we do basic sanity checks here because the auth layer will store these
Expand Down Expand Up @@ -209,6 +214,29 @@ def _create_registration_details(self, user_id, token):
"home_server": self.hs.hostname,
}

@defer.inlineCallbacks
def onEmailTokenRequest(self, request):
body = parse_json_dict_from_request(request)

required = ['id_server', 'client_secret', 'email', 'send_attempt']
absent = []
for k in required:
if k not in body:
absent.append(k)

if len(absent) > 0:
raise SynapseError(400, "Missing params: %r" % absent, Codes.MISSING_PARAM)

existingUid = yield self.hs.get_datastore().get_user_id_by_threepid(
'email', body['email']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be be accessing keys in the body before checking the absent list?

)

if existingUid is not None:
raise SynapseError(400, "Email is already in use", Codes.THREEPID_IN_USE)

ret = yield self.identity_handler.requestEmailToken(**body)
defer.returnValue((200, ret))


def register_servlets(hs, http_server):
RegisterRestServlet(hs).register(http_server)
2 changes: 1 addition & 1 deletion synapse/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

# Remember to update this number every time a change is made to database
# schema files, so the users will be informed on server restarts.
SCHEMA_VERSION = 21
SCHEMA_VERSION = 22

dir_path = os.path.abspath(os.path.dirname(__file__))

Expand Down
19 changes: 19 additions & 0 deletions synapse/storage/schema/delta/22/user_threepids_unique.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE IF NOT EXISTS user_threepids2 (
user_id TEXT NOT NULL,
medium TEXT NOT NULL,
address TEXT NOT NULL,
validated_at BIGINT NOT NULL,
added_at BIGINT NOT NULL,
CONSTRAINT medium_address UNIQUE (medium, address)
);

INSERT INTO user_threepids2
SELECT * FROM user_threepids WHERE added_at IN (
SELECT max(added_at) FROM user_threepids GROUP BY medium, address
)
;

DROP TABLE user_threepids;
ALTER TABLE user_threepids2 RENAME TO user_threepids;

CREATE INDEX user_threepids_user_id ON user_threepids(user_id);
3 changes: 2 additions & 1 deletion tests/rest/client/v2_alpha/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def setUp(self):
self.request_data = ""
self.request = Mock(
content=Mock(read=Mock(side_effect=lambda: self.request_data)),
path='/_matrix/api/v2_alpha/register'
)
self.request.args = {}

Expand Down Expand Up @@ -131,4 +132,4 @@ def test_POST_disabled_registration(self):
})
self.registration_handler.register = Mock(return_value=("@user:id", "t"))
d = self.servlet.on_POST(self.request)
return self.assertFailure(d, SynapseError)
return self.assertFailure(d, SynapseError)