0

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!

Community
  • 1
  • 1
Lars
  • 710
  • 5
  • 19
  • If you want to block til completion, you could use a [`CountDownLatch`](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html) – Paul Samsotha May 01 '17 at 12:33
  • Thanks! I have been looking into the CountDownLatch a bit, but haven't quite been able to figure out how to implement it that way. But i'll definitely give it another go if you think it's possible that way around. – Lars May 01 '17 at 12:42

1 Answers1

0

I managed to make it work using the CountDownLatch. Big thanks too peeskillet!

For anyone interested, my solution looks like this:

    private void validateToken(String token, final ContainerRequestContext requestContext) throws Exception {

    final CountDownLatch latch = new CountDownLatch(1);

    FirebaseAuth.getInstance().verifyIdToken(token)
            .addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
                @Override
                public void onSuccess(FirebaseToken decodedToken) {
                    System.out.println("on success");
                    latch.countDown();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    System.out.println("on fail " + e);
                    requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
                    latch.countDown();
            }
        });
    latch.await();
}
Lars
  • 710
  • 5
  • 19