Failed to validate MAC

572 views
Skip to first unread message

udoojo...@gmail.com

unread,
Sep 14, 2016, 9:55:42 AM9/14/16
to Firebase Google Group
I tried to test the endpoint to read data from firebase database, the images below show how I configured the rules, I fetch the token by using a java SDK(code below), but I got an error returned


 {
  "error": "Failed to validate MAC."
}

how I fetch token: 
    public static String getFireBaseToken(long uid){
        Map<String, Object> authPayload = new HashMap<String, Object>();
        authPayload.put("uid", String.valueOf(uid));
        TokenOptions tokenOptions = new TokenOptions();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, 90);
        tokenOptions.setExpires(cal.getTime());
        
        return (new TokenGenerator(FIREBASE_SECRECT))
                .createToken(authPayload, tokenOptions);
    }
    

how I configure the rules for the database:

{
  "rules": {
    "conversations":{
      "$uid1":{
        "$uid2":{
          "messages":{
            ".read": "auth !== null && ($uid1 === auth.uid || $uid2 === auth.uid)",
            ".write": "auth !== null",
            "$messageId":{
              ".validate": "$uid1 < $uid2 && newData.child('timestamp').exists()",
              "timestamp": {
                 ".validate": "newData.val().length > 0"
              },
              "text":{
                ".validate": "newData.exists() && newData.val().length > 0"
              },
              "id":{
                 ".validate": "newData.val() === auth.uid"
              }
          }
          }
        }
      }
    }
  }
}


I tried to use GET method: 

I get 400(bad request)

udoojo...@gmail.com

unread,
Sep 14, 2016, 1:11:15 PM9/14/16
to Firebase Google Group
I also tried something below, it still gives a error, but different:  { "error" : "Expected an ID token, but was given a custom token." }

       Map<String, Object> auth = new HashMap<String, Object>();
        auth.put("uid", "95");
//System.out.println(FirebaseAuth.getInstance().createCustomToken("96"));
    FirebaseOptions options = new FirebaseOptions.Builder()
      .setServiceAccount(getClass().getResourceAsStream("/Udoo-318b2b3dc4db.json"))
      .setDatabaseUrl("https://udoo-518ec.firebaseio.com/")
      .setDatabaseAuthVariableOverride(auth)
            .build();
      String token = FirebaseAuth.getInstance(FirebaseApp.initializeApp(options)).createCustomToken("95");
      System.out.println(token);
    
    } 

Jacob Wenger

unread,
Sep 19, 2016, 8:08:22 PM9/19/16
to fireba...@googlegroups.com
Hey there,

I think you are mixing a few different SDK versions and token types.

First, make sure you are using the 3.x.x SDKs to mint tokens and well as to authenticate clients. Do not use our legacy Java token minting library if you are intending to use the 3.x.x SDKs on client devices. As noted in bold at the top of that README, the legacy token minter is intended for versions 1.x.x and 2.x.x of the Firebase SDK.

Next, make sure you know the difference between ID tokens and custom tokens. ID tokens are used to communicate to Firebase services such as the Realtime Database and Firebase Storage. Custom tokens are meant to authenticate client devices via the signInWithCustomToken() method. I would suggest reading through the server auth docs for a richer understanding of how this works.

If you still have issues, please provide version information for the Firebase SDK you are using as well as what method you are calling which produces the error you got.

Cheers,
Jacob

    
    } 

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/c933a810-ffba-4c8c-86a0-c10deccc99c8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

udoojo...@gmail.com

unread,
Sep 19, 2016, 11:16:44 PM9/19/16
to Firebase Google Group
Thanks, we fixed it already.
Reply all
Reply to author
Forward
0 new messages