Wait for async calls

This commit is contained in:
Maxime Dor
2017-08-31 03:34:08 +02:00
parent 0033d0dc1d
commit d57aef36ea

View File

@@ -5,8 +5,6 @@ import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseCredential;
import com.google.firebase.auth.FirebaseCredentials;
import io.kamax.matrix.MatrixID;
import io.kamax.matrix._MatrixID;
import io.kamax.mxisd.auth.UserAuthResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
@@ -14,11 +12,17 @@ import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GoogleFirebaseAuthenticator implements AuthenticatorProvider {
private Logger log = LoggerFactory.getLogger(GoogleFirebaseAuthenticator.class);
private static final Pattern matrixIdLaxPattern = Pattern.compile("@(.*):(.+)");
private boolean isEnabled;
private FirebaseApp fbApp;
private FirebaseAuth fbAuth;
@@ -71,29 +75,39 @@ public class GoogleFirebaseAuthenticator implements AuthenticatorProvider {
final UserAuthResult result = new UserAuthResult();
try {
log.info("Trying to authenticate {}", id);
_MatrixID mxId = new MatrixID(id);
fbAuth.verifyIdToken(password).addOnSuccessListener(token -> {
if (!StringUtils.equals(mxId.getLocalPart(), token.getUid())) {
log.info("Failture to authenticate {}: Matrix ID localpart '{}' does not match Firebase UID '{}'", id, mxId.getLocalPart(), token.getUid());
result.failure();
}
log.info("Trying to authenticate {}", id);
Matcher m = matrixIdLaxPattern.matcher(id);
if (!m.matches()) {
log.warn("Could not validate {} as a Matrix ID", id);
result.failure();
}
log.info("{} was successfully authenticated", id);
result.success(id, token.getName());
}).addOnFailureListener(e -> {
if (e instanceof IllegalArgumentException) {
log.info("Failure to authenticate {}: invalid firebase token", id);
} else {
log.info("Failure to authenticate {}", id, e.getMessage());
log.debug("Exception", e);
}
String localpart = m.group(1);
CountDownLatch l = new CountDownLatch(1);
fbAuth.verifyIdToken(password).addOnSuccessListener(token -> {
if (!StringUtils.equals(localpart, token.getUid())) {
log.info("Failture to authenticate {}: Matrix ID localpart '{}' does not match Firebase UID '{}'", id, localpart, token.getUid());
result.failure();
});
} catch (IllegalArgumentException e) {
log.warn("Could not validate {} as a Matrix ID: {}", id, e.getMessage());
}
log.info("{} was successfully authenticated", id);
result.success(id, token.getName());
}).addOnFailureListener(e -> {
if (e instanceof IllegalArgumentException) {
log.info("Failure to authenticate {}: invalid firebase token", id);
} else {
log.info("Failure to authenticate {}: {}", id, e.getMessage(), e);
log.info("Exception", e);
}
result.failure();
}).addOnCompleteListener(t -> l.countDown());
try {
l.await(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.warn("Interrupted while waiting for Firebase auth check");
result.failure();
}