I am following this (pretty great) guide to trying to implement an Authorization filter for my REST backend. However, I'd like to use the authorization module from Firebase, so my 'verify token' should verify tokens with firebase.
I've tried to implement it like this:
private void validateToken(String token, final ContainerRequestContext requestContext) throws Exception {
FirebaseAuth.getInstance().verifyIdToken(token)
.addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
@Override
public void onSuccess(FirebaseToken decodedToken) {
System.out.println("success");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
System.out.println("fail" + e);
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}
}).addOnCompleteListener(new OnCompleteListener<FirebaseToken>() {
@Override
public void onComplete(@NonNull Task<FirebaseToken> task) {
}
});
}
But the problem is the listeners are of course running in a different thread, so as it looks now, the request will go through and the onFailure listener won't be able to abort it.
I've made it work just by adding a sleep timer in the bottom, however I'd like a better solution. I've tried to lock the main thread using synchronized
, trying to unlock it in the onCompleteListener, but haven't been able to make it work.
Is there any nice way to work around this?
Thanks in advance!