Wait for async calls
This commit is contained in:
@@ -5,8 +5,6 @@ import com.google.firebase.FirebaseOptions;
|
|||||||
import com.google.firebase.auth.FirebaseAuth;
|
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 io.kamax.matrix.MatrixID;
|
|
||||||
import io.kamax.matrix._MatrixID;
|
|
||||||
import io.kamax.mxisd.auth.UserAuthResult;
|
import io.kamax.mxisd.auth.UserAuthResult;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -14,11 +12,17 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
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 {
|
public class GoogleFirebaseAuthenticator implements AuthenticatorProvider {
|
||||||
|
|
||||||
private Logger log = LoggerFactory.getLogger(GoogleFirebaseAuthenticator.class);
|
private Logger log = LoggerFactory.getLogger(GoogleFirebaseAuthenticator.class);
|
||||||
|
|
||||||
|
private static final Pattern matrixIdLaxPattern = Pattern.compile("@(.*):(.+)");
|
||||||
|
|
||||||
private boolean isEnabled;
|
private boolean isEnabled;
|
||||||
private FirebaseApp fbApp;
|
private FirebaseApp fbApp;
|
||||||
private FirebaseAuth fbAuth;
|
private FirebaseAuth fbAuth;
|
||||||
@@ -71,12 +75,19 @@ public class GoogleFirebaseAuthenticator implements AuthenticatorProvider {
|
|||||||
|
|
||||||
final UserAuthResult result = new UserAuthResult();
|
final UserAuthResult result = new UserAuthResult();
|
||||||
|
|
||||||
try {
|
|
||||||
log.info("Trying to authenticate {}", id);
|
log.info("Trying to authenticate {}", id);
|
||||||
_MatrixID mxId = new MatrixID(id);
|
Matcher m = matrixIdLaxPattern.matcher(id);
|
||||||
|
if (!m.matches()) {
|
||||||
|
log.warn("Could not validate {} as a Matrix ID", id);
|
||||||
|
result.failure();
|
||||||
|
}
|
||||||
|
|
||||||
|
String localpart = m.group(1);
|
||||||
|
|
||||||
|
CountDownLatch l = new CountDownLatch(1);
|
||||||
fbAuth.verifyIdToken(password).addOnSuccessListener(token -> {
|
fbAuth.verifyIdToken(password).addOnSuccessListener(token -> {
|
||||||
if (!StringUtils.equals(mxId.getLocalPart(), token.getUid())) {
|
if (!StringUtils.equals(localpart, token.getUid())) {
|
||||||
log.info("Failture to authenticate {}: Matrix ID localpart '{}' does not match Firebase UID '{}'", id, mxId.getLocalPart(), token.getUid());
|
log.info("Failture to authenticate {}: Matrix ID localpart '{}' does not match Firebase UID '{}'", id, localpart, token.getUid());
|
||||||
result.failure();
|
result.failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,14 +97,17 @@ public class GoogleFirebaseAuthenticator implements AuthenticatorProvider {
|
|||||||
if (e instanceof IllegalArgumentException) {
|
if (e instanceof IllegalArgumentException) {
|
||||||
log.info("Failure to authenticate {}: invalid firebase token", id);
|
log.info("Failure to authenticate {}: invalid firebase token", id);
|
||||||
} else {
|
} else {
|
||||||
log.info("Failure to authenticate {}", id, e.getMessage());
|
log.info("Failure to authenticate {}: {}", id, e.getMessage(), e);
|
||||||
log.debug("Exception", e);
|
log.info("Exception", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
result.failure();
|
result.failure();
|
||||||
});
|
}).addOnCompleteListener(t -> l.countDown());
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
log.warn("Could not validate {} as a Matrix ID: {}", id, e.getMessage());
|
try {
|
||||||
|
l.await(30, TimeUnit.SECONDS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.warn("Interrupted while waiting for Firebase auth check");
|
||||||
result.failure();
|
result.failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user