Search code examples
firebase-authenticationfirebase-adminktor

Firebase Admin SDK always fails with "Connection Refused"


I'm new to using ktor and have been trying to get started by leveraging Firebase Auth for a new project. To get started I followed the details of this blog post and found it very informative. However, I cannot get the sample app to run on my personal device. In fact when I'm running my ktor app I can't get any Firebase calls to succeed. They all fail with this stack trace:

com.google.firebase.auth.FirebaseAuthException: Unknown error while making a remote service call: Connection refused (Connection refused)
        at com.google.firebase.auth.internal.AuthErrorHandler.createException(AuthErrorHandler.java:122)
        at com.google.firebase.auth.internal.AuthErrorHandler.createException(AuthErrorHandler.java:35)
        at com.google.firebase.internal.AbstractHttpErrorHandler.handleIOException(AbstractHttpErrorHandler.java:63)
        at com.google.firebase.internal.ErrorHandlingHttpClient.send(ErrorHandlingHttpClient.java:110)
        at com.google.firebase.auth.internal.AuthHttpClient.sendRequest(AuthHttpClient.java:75)
        at com.google.firebase.auth.FirebaseUserManager.lookupUserAccount(FirebaseUserManager.java:238)
        at com.google.firebase.auth.FirebaseUserManager.getUserById(FirebaseUserManager.java:121)
        at com.google.firebase.auth.RevocationCheckDecorator.validateDisabledOrRevoked(RevocationCheckDecorator.java:59)
        at com.google.firebase.auth.RevocationCheckDecorator.verifyToken(RevocationCheckDecorator.java:54)
        at com.google.firebase.auth.AbstractFirebaseAuth$3.execute(AbstractFirebaseAuth.java:307)
        at com.google.firebase.auth.AbstractFirebaseAuth$3.execute(AbstractFirebaseAuth.java:304)
        at com.google.firebase.internal.CallableOperation.call(CallableOperation.java:36)
        at com.google.firebase.auth.AbstractFirebaseAuth.verifyIdToken(AbstractFirebaseAuth.java:269)
        at com.google.firebase.auth.AbstractFirebaseAuth.verifyIdToken(AbstractFirebaseAuth.java:241)
        at com.plusmobileapps.firebaseadminsample.firebase.FirebaseAuthKt$verifyFirebaseIdToken$token$1.invokeSuspend(FirebaseAuth.kt:80)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412)
        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255)
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237)
        at java.base/java.net.Socket.connect(Socket.java:609)
        at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:177)
        at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:508)
        at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:603)
        at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:276)
        at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:375)
        at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:396)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1253)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1015)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1367)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1342)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:113)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012)
        at com.google.firebase.internal.ErrorHandlingHttpClient.send(ErrorHandlingHttpClient.java:96)
        ... 19 more

I checked out the sample git repository from the blog post, added my service account json, built and ran the project without issue. However, when I GET http://0.0.0.0:8080/authenticated in Postman (using Bearer Token auth) this line FirebaseAuth.getInstance().verifyIdToken(authHeader.blob) throws the stack trace shown above.

I tried a few other things to figure out what might be going on such as using ktor to make a an http request on its own and I was able to GET https://www.google.com/ and send that back so it seems that my ktor app can make http requests. I also tried calling FirebaseAuth within a regular route in the app and that also threw the same exception shown above.

It seems like this is probably a Firebase Admin sdk issue. But I didn't change the version in the build.gradle and I'm using my personal device, and don't have any firewalls or proxies set up that would prevent the Admin SDK from being able to make http calls.


Solution

  • After quite a bit of debugging I finally figured out what the issue was. I had previously enabled Firebase Auth Emulator by setting FIREBASE_AUTH_EMULATOR_HOST and although I had tried unsetting it with the unset command in zsh I needed to delete this line from my ~/.zshrc. Then after restarting IntelliJ it finally worked.

    export FIREBASE_AUTH_EMULATOR_HOST="localhost:9099"