Properly handle phone numbers

This commit is contained in:
Maxime Dor
2017-08-13 00:16:21 +02:00
parent 5a683b41ef
commit 884583881b
4 changed files with 35 additions and 10 deletions

View File

@@ -60,6 +60,9 @@ dependencies {
// JSON // JSON
compile 'com.google.code.gson:gson:2.8.1' compile 'com.google.code.gson:gson:2.8.1'
// Phone numbers validation
compile 'com.googlecode.libphonenumber:libphonenumber:8.7.1'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
} }

View File

@@ -25,7 +25,6 @@ import com.google.gson.JsonObject
import io.kamax.mxisd.controller.v1.io.SessionEmailTokenRequestJson import io.kamax.mxisd.controller.v1.io.SessionEmailTokenRequestJson
import io.kamax.mxisd.controller.v1.io.SessionPhoneTokenRequestJson import io.kamax.mxisd.controller.v1.io.SessionPhoneTokenRequestJson
import io.kamax.mxisd.exception.BadRequestException import io.kamax.mxisd.exception.BadRequestException
import io.kamax.mxisd.exception.NotImplementedException
import io.kamax.mxisd.lookup.ThreePid import io.kamax.mxisd.lookup.ThreePid
import io.kamax.mxisd.mapping.MappingManager import io.kamax.mxisd.mapping.MappingManager
import org.apache.commons.io.IOUtils import org.apache.commons.io.IOUtils
@@ -79,10 +78,14 @@ class SessionController {
} }
@RequestMapping(value = "/_matrix/identity/api/v1/validate/{medium}/submitToken") @RequestMapping(value = "/_matrix/identity/api/v1/validate/{medium}/submitToken")
String validate(HttpServletRequest request) { String validate(HttpServletRequest request,
@RequestParam String sid,
@RequestParam("client_secret") String secret, @RequestParam String token) {
log.info("Requested: {}?{}", request.getRequestURL(), request.getQueryString()) log.info("Requested: {}?{}", request.getRequestURL(), request.getQueryString())
throw new NotImplementedException() mgr.validate(sid, secret, token)
return "{}"
} }
@RequestMapping(value = "/_matrix/identity/api/v1/3pid/getValidated3pid") @RequestMapping(value = "/_matrix/identity/api/v1/3pid/getValidated3pid")
@@ -123,7 +126,7 @@ class SessionController {
} catch (BadRequestException e) { } catch (BadRequestException e) {
log.info("requested session was not validated") log.info("requested session was not validated")
obj = new JsonObject() JsonObject obj = new JsonObject()
obj.addProperty("errcode", "M_SESSION_NOT_VALIDATED") obj.addProperty("errcode", "M_SESSION_NOT_VALIDATED")
obj.addProperty("error", e.getMessage()) obj.addProperty("error", e.getMessage())
response.setStatus(HttpStatus.SC_BAD_REQUEST) response.setStatus(HttpStatus.SC_BAD_REQUEST)

View File

@@ -1,22 +1,29 @@
package io.kamax.mxisd.controller.v1.io; package io.kamax.mxisd.controller.v1.io;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
public class SessionPhoneTokenRequestJson extends GenericTokenRequestJson { public class SessionPhoneTokenRequestJson extends GenericTokenRequestJson {
private static PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
private String country; private String country;
private String phone_number; private String phone_number;
@Override @Override
public String getMedium() { public String getMedium() {
return "email"; return "msisdn";
} }
@Override @Override
public String getValue() { public String getValue() {
return phone_number; try {
Phonenumber.PhoneNumber num = phoneUtil.parse(phone_number, country);
return phoneUtil.format(num, PhoneNumberUtil.PhoneNumberFormat.E164).replace("+", "");
} catch (NumberParseException e) {
throw new IllegalArgumentException("Invalid phone number");
} }
public String getCountry() {
return country;
} }
} }

View File

@@ -60,6 +60,18 @@ public class MappingManager {
return sid; return sid;
} }
public void validate(String sid, String secret, String token) {
Session s = sessions.get(sid);
if (s == null || !StringUtils.equals(s.secret, secret)) {
throw new BadRequestException("sid or secret are not valid");
}
// TODO actually check token
s.isValidated = true;
s.validationTimestamp = Instant.now();
}
public Optional<ThreePid> getValidated(String sid, String secret) { public Optional<ThreePid> getValidated(String sid, String secret) {
Session s = sessions.get(sid); Session s = sessions.get(sid);
if (s != null && StringUtils.equals(s.secret, secret)) { if (s != null && StringUtils.equals(s.secret, secret)) {