Factor out msisdn canonicalisation

Plus a couple of other minor fixes
This commit is contained in:
David Baker 2017-03-08 11:03:39 +00:00
parent 402a7bf63d
commit 88df6c0c9a
4 changed files with 11 additions and 41 deletions

View File

@ -321,7 +321,7 @@ class AuthHandler(BaseHandler):
defer.returnValue(True) defer.returnValue(True)
@defer.inlineCallbacks @defer.inlineCallbacks
def _check_threepid(self, medium, authdict, ): def _check_threepid(self, medium, authdict):
yield run_on_reactor() yield run_on_reactor()
if 'threepid_creds' not in authdict: if 'threepid_creds' not in authdict:

View File

@ -19,13 +19,13 @@ from synapse.api.errors import SynapseError, LoginError, Codes
from synapse.types import UserID from synapse.types import UserID
from synapse.http.server import finish_request from synapse.http.server import finish_request
from synapse.http.servlet import parse_json_object_from_request from synapse.http.servlet import parse_json_object_from_request
from synapse.util.msisdn import phone_number_to_msisdn
from .base import ClientV1RestServlet, client_path_patterns from .base import ClientV1RestServlet, client_path_patterns
import simplejson as json import simplejson as json
import urllib import urllib
import urlparse import urlparse
import phonenumbers
import logging import logging
from saml2 import BINDING_HTTP_POST from saml2 import BINDING_HTTP_POST
@ -61,8 +61,6 @@ def login_submission_legacy_convert(submission):
del submission["medium"] del submission["medium"]
del submission["address"] del submission["address"]
return submission
def login_id_thirdparty_from_phone(identifier): def login_id_thirdparty_from_phone(identifier):
""" """
@ -74,14 +72,8 @@ def login_id_thirdparty_from_phone(identifier):
""" """
if "country" not in identifier or "number" not in identifier: if "country" not in identifier or "number" not in identifier:
raise SynapseError(400, "Invalid phone-type identifier") raise SynapseError(400, "Invalid phone-type identifier")
phoneNumber = None
try: msisdn = phone_number_to_msisdn(identifier["country"], identifier["number"])
phoneNumber = phonenumbers.parse(identifier["number"], identifier["country"])
except phonenumbers.NumberParseException:
raise SynapseError(400, "Unable to parse phone number")
msisdn = phonenumbers.format_number(
phoneNumber, phonenumbers.PhoneNumberFormat.E164
)[1:]
return { return {
"type": "m.id.thirdparty", "type": "m.id.thirdparty",
@ -173,7 +165,7 @@ class LoginRestServlet(ClientV1RestServlet):
if "password" not in login_submission: if "password" not in login_submission:
raise SynapseError(400, "Missing parameter: password") raise SynapseError(400, "Missing parameter: password")
login_submission = login_submission_legacy_convert(login_submission) login_submission_legacy_convert(login_submission)
if "identifier" not in login_submission: if "identifier" not in login_submission:
raise SynapseError(400, "Missing param: identifier") raise SynapseError(400, "Missing param: identifier")
@ -182,7 +174,7 @@ class LoginRestServlet(ClientV1RestServlet):
if "type" not in identifier: if "type" not in identifier:
raise SynapseError(400, "Login identifier has no type") raise SynapseError(400, "Login identifier has no type")
# convert phone type identifiers to geberic threepids # convert phone type identifiers to generic threepids
if identifier["type"] == "m.id.phone": if identifier["type"] == "m.id.phone":
identifier = login_id_thirdparty_from_phone(identifier) identifier = login_id_thirdparty_from_phone(identifier)

View File

@ -20,13 +20,12 @@ from synapse.api.constants import LoginType
from synapse.api.errors import LoginError, SynapseError, Codes from synapse.api.errors import LoginError, SynapseError, Codes
from synapse.http.servlet import RestServlet, parse_json_object_from_request from synapse.http.servlet import RestServlet, parse_json_object_from_request
from synapse.util.async import run_on_reactor from synapse.util.async import run_on_reactor
from synapse.util.msisdn import phone_number_to_msisdn
from ._base import client_v2_patterns from ._base import client_v2_patterns
import logging import logging
import phonenumbers
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -87,14 +86,7 @@ class MsisdnPasswordRequestTokenRestServlet(RestServlet):
if absent: if absent:
raise SynapseError(400, "Missing params: %r" % absent, Codes.MISSING_PARAM) raise SynapseError(400, "Missing params: %r" % absent, Codes.MISSING_PARAM)
phoneNumber = None msisdn = phone_number_to_msisdn(body['country'], body['phone_number'])
try:
phoneNumber = phonenumbers.parse(body['phone_number'], body['country'])
except phonenumbers.NumberParseException:
raise SynapseError(400, "Unable to parse phone number")
msisdn = phonenumbers.format_number(
phoneNumber, phonenumbers.PhoneNumberFormat.E164
)[1:]
existingUid = yield self.hs.get_datastore().get_user_id_by_threepid( existingUid = yield self.hs.get_datastore().get_user_id_by_threepid(
'msisdn', msisdn 'msisdn', msisdn
@ -273,14 +265,7 @@ class MsisdnThreepidRequestTokenRestServlet(RestServlet):
if absent: if absent:
raise SynapseError(400, "Missing params: %r" % absent, Codes.MISSING_PARAM) raise SynapseError(400, "Missing params: %r" % absent, Codes.MISSING_PARAM)
phoneNumber = None msisdn = phone_number_to_msisdn(body['country'], body['phone_number'])
try:
phoneNumber = phonenumbers.parse(body['phone_number'], body['country'])
except phonenumbers.NumberParseException:
raise SynapseError(400, "Unable to parse phone number")
msisdn = phonenumbers.format_number(
phoneNumber, phonenumbers.PhoneNumberFormat.E164
)[1:]
existingUid = yield self.hs.get_datastore().get_user_id_by_threepid( existingUid = yield self.hs.get_datastore().get_user_id_by_threepid(
'msisdn', msisdn 'msisdn', msisdn

View File

@ -21,12 +21,12 @@ from synapse.api.auth import get_access_token_from_request, has_access_token
from synapse.api.constants import LoginType from synapse.api.constants import LoginType
from synapse.api.errors import SynapseError, Codes, UnrecognizedRequestError from synapse.api.errors import SynapseError, Codes, UnrecognizedRequestError
from synapse.http.servlet import RestServlet, parse_json_object_from_request from synapse.http.servlet import RestServlet, parse_json_object_from_request
from synapse.util.msisdn import phone_number_to_msisdn
from ._base import client_v2_patterns from ._base import client_v2_patterns
import logging import logging
import hmac import hmac
import phonenumbers
from hashlib import sha1 from hashlib import sha1
from synapse.util.async import run_on_reactor from synapse.util.async import run_on_reactor
@ -110,14 +110,7 @@ class MsisdnRegisterRequestTokenRestServlet(RestServlet):
if len(absent) > 0: if len(absent) > 0:
raise SynapseError(400, "Missing params: %r" % absent, Codes.MISSING_PARAM) raise SynapseError(400, "Missing params: %r" % absent, Codes.MISSING_PARAM)
phoneNumber = None msisdn = phone_number_to_msisdn(body['country'], body['phone_number'])
try:
phoneNumber = phonenumbers.parse(body['phone_number'], body['country'])
except phonenumbers.NumberParseException:
raise SynapseError(400, "Unable to parse phone number")
msisdn = phonenumbers.format_number(
phoneNumber, phonenumbers.PhoneNumberFormat.E164
)[1:]
existingUid = yield self.hs.get_datastore().get_user_id_by_threepid( existingUid = yield self.hs.get_datastore().get_user_id_by_threepid(
'msisdn', msisdn 'msisdn', msisdn