Use the correct formatting for MSISDN

This commit is contained in:
Maxime Dor
2017-09-26 04:26:39 +02:00
parent 1de0951733
commit 61addd297a

View File

@@ -26,6 +26,8 @@ import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseCredential; import com.google.firebase.auth.FirebaseCredential;
import com.google.firebase.auth.FirebaseCredentials; import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.auth.UserInfo; import com.google.firebase.auth.UserInfo;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import io.kamax.matrix.ThreePidMedium; import io.kamax.matrix.ThreePidMedium;
import io.kamax.matrix._MatrixID; import io.kamax.matrix._MatrixID;
import io.kamax.mxisd.ThreePid; import io.kamax.mxisd.ThreePid;
@@ -49,14 +51,7 @@ public class GoogleFirebaseAuthenticator implements AuthenticatorProvider {
private FirebaseApp fbApp; private FirebaseApp fbApp;
private FirebaseAuth fbAuth; private FirebaseAuth fbAuth;
private void waitOnLatch(BackendAuthResult result, CountDownLatch l, String purpose) { private PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
l.await(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.warn("Interrupted while waiting for " + purpose);
result.fail();
}
}
public GoogleFirebaseAuthenticator(boolean isEnabled) { public GoogleFirebaseAuthenticator(boolean isEnabled) {
this.isEnabled = isEnabled; this.isEnabled = isEnabled;
@@ -74,6 +69,42 @@ public class GoogleFirebaseAuthenticator implements AuthenticatorProvider {
} }
} }
private void waitOnLatch(BackendAuthResult result, CountDownLatch l, String purpose) {
try {
l.await(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.warn("Interrupted while waiting for " + purpose);
result.fail();
}
}
private void toEmail(BackendAuthResult result, String email) {
if (StringUtils.isBlank(email)) {
return;
}
result.withThreePid(new ThreePid(ThreePidMedium.Email.getId(), email));
}
private void toMsisdn(BackendAuthResult result, String phoneNumber) {
if (StringUtils.isBlank(phoneNumber)) {
return;
}
try {
String number = phoneUtil.format(
phoneUtil.parse(
phoneNumber,
null // No default region
),
PhoneNumberUtil.PhoneNumberFormat.E164
).substring(1); // We want without the leading +
result.withThreePid(new ThreePid(ThreePidMedium.PhoneNumber.getId(), number));
} catch (NumberParseException e) {
log.warn("Invalid phone number: {}", phoneNumber);
}
}
private FirebaseCredential getCreds(String credsPath) throws IOException { private FirebaseCredential getCreds(String credsPath) throws IOException {
if (StringUtils.isNotBlank(credsPath)) { if (StringUtils.isNotBlank(credsPath)) {
return FirebaseCredentials.fromCertificate(new FileInputStream(credsPath)); return FirebaseCredentials.fromCertificate(new FileInputStream(credsPath));
@@ -132,22 +163,12 @@ public class GoogleFirebaseAuthenticator implements AuthenticatorProvider {
CountDownLatch userRecordLatch = new CountDownLatch(1); CountDownLatch userRecordLatch = new CountDownLatch(1);
fbAuth.getUser(token.getUid()).addOnSuccessListener(user -> { fbAuth.getUser(token.getUid()).addOnSuccessListener(user -> {
try { try {
if (StringUtils.isNotBlank(user.getEmail())) { toEmail(result, user.getEmail());
result.withThreePid(new ThreePid(ThreePidMedium.Email.getId(), user.getEmail())); toMsisdn(result, user.getPhoneNumber());
}
if (StringUtils.isNotBlank(user.getPhoneNumber())) {
result.withThreePid(new ThreePid(ThreePidMedium.PhoneNumber.getId(), user.getPhoneNumber()));
}
for (UserInfo info : user.getProviderData()) { for (UserInfo info : user.getProviderData()) {
if (StringUtils.isNotBlank(info.getEmail())) { toEmail(result, info.getEmail());
result.withThreePid(new ThreePid(ThreePidMedium.Email.getId(), info.getEmail())); toMsisdn(result, info.getPhoneNumber());
}
if (StringUtils.isNotBlank(info.getPhoneNumber())) {
result.withThreePid(new ThreePid(ThreePidMedium.PhoneNumber.getId(), info.getPhoneNumber()));
}
} }
log.info("Got {} 3PIDs in profile", result.getProfile().getThreePids().size()); log.info("Got {} 3PIDs in profile", result.getProfile().getThreePids().size());