简体   繁体   中英

Firebase admin sdk: IllegalArgumentException, Preconditions.checkArgument

I am getting the error below:

java.lang.IllegalArgumentException
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
    at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
    at com.google.api.client.json.webtoken.JsonWebSignature$Parser.parse(JsonWebSignature.java:599)
    at com.google.firebase.auth.FirebaseToken.parse(FirebaseToken.java:44)
    at com.google.firebase.auth.FirebaseAuth$4.execute(FirebaseAuth.java:456)
    at com.google.firebase.auth.FirebaseAuth$4.execute(FirebaseAuth.java:449)
    at com.google.firebase.internal.CallableOperation.call(CallableOperation.java:36)
    at com.google.firebase.auth.FirebaseAuth.verifyIdToken(FirebaseAuth.java:413)
    at d.d.pamper.test.ApplicationTest.testFire2(ApplicationTest.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I realize the stack is not very helpful. I've debugged inside the Admin SDK. It reads my service json correctly, the FirebaseAuth.getInstance() is not null, I can see my project id get set correctly.

This is how I'm initializing:

if(FirebaseApp.getApps().size() > 0) {
            System.out.println("initialized already ");
            return;
        }

        FirebaseOptions options = null;
        try {
            System.out.println("try initializing");
            options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(gservicesConfig.getInputStream()))
                    .setDatabaseUrl("https://pamperanywhere.firebaseio.com")
                    .build();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FirebaseApp.initializeApp(options);
        return;

This is how I'm testing my token:

@Test
public void testFire2() {
    String idToken = "REFRESH TOKEN FROM FIREBASE";
    if(!StringUtils.isEmpty(idToken)) {
        FirebaseToken firebaseToken = null;
        if(FirebaseAuth.getInstance() == null) {
            System.out.println("FirebaseAuth.getInstance() == null");
        }
        try {
            firebaseToken = FirebaseAuth.getInstance().verifyIdToken(idToken, true);
        } catch (IllegalArgumentException e) {
            System.out.println("exception+IllegalArgumentException");
            e.printStackTrace();
        } catch (FirebaseAuthException e) {
            System.out.println("exception+FirebaseAuthException");
            e.printStackTrace();
        }
        if (firebaseToken == null) {
            System.out.println("firebaseToken == null");
        }
    }
}

I'm not sure why its throwing IllegalArgumentException . I'm able to login to the application fine (front end), I see my refresh token (front end) but on the Admin side its giving me this issue. I am sure I'm passing the correct token because I can login and see the token (front end).

Maven is:

    <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>6.2.0</version>
    </dependency>

Thank you for your help.

An ID token should be a valid JWT with 3 segments (separated by dots). Refresh tokens are certainly not that. Therefore the underlying token parser is failing with the above assertion error. Here's the relevant bit of code from the Google API client. Basically it cannot find any dots in the given token string.

public JsonWebSignature parse(String tokenString) throws IOException {
      // split on the dots
      int firstDot = tokenString.indexOf('.');
      Preconditions.checkArgument(firstDot != -1);
      ...

There's a listener:

firebase.auth().onIdTokenChanged((user: firebase.User) => {
      console.log("onIdTokenChanged");
      if(user && user.uid) {
        user.getIdToken(false)
        .then((data: any) => {
          console.log("data: ", data);
          this.idToken = data;
        });
      }      
    });

this gets invoked every one hour. I thought I had to implement this logic myself! https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onIdTokenChanged

From official; above is from Ionic2/Angular2, I have that in a dataServiceProvider 's constructor.

firebase.auth().onIdTokenChanged(function(user) {
  if (user) {
    // User is signed in or token was refreshed.
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM