title: Firebase : Permission denied - while setValue()
body: <p>I am trying to build a chat application using firebase. </p>
  
  <p>The structure for message table : </p>
  
  <pre><code>message -
     $message_id 
      - $message_push_id 
        - message {
          sender : 3, 
          receiver : 58,
          token : token_of_sender,
          message : hi 
          ....}
  </code></pre>
  
  <p>message_id here is generated using the sender and receiver ids  <strong>"3_58"</strong></p>
  
  <p>I am using push to save messages into firebase. </p>
  
  <pre><code>{
      "rules": {
          ".read": true,
           "message":
          {
            "$messageid": {
              "$messagepushid":
              {
                ".read": true,
                ".write": "auth != null  &amp;&amp; !data.exists()",
                ".indexOn": ["token", "userid", "receiverid", "sent_time"],
                ".validate": "auth.token == newData.child('token').val() &amp;&amp; newData.hasChildren(['token', 'userid', 'receiverid', 'text'])"
              }
            }
          }
      }
  }
  </code></pre>
  
  <p>I have already generated token using custom token generator : </p>
  
  <pre><code>Firebase firebase = getFirebase();
  Map&lt;String, Object&gt; authPayload = new HashMap&lt;String, Object&gt;();
  authPayload.put("uid", user.getUserid());
  authPayload.put("token", user.getToken());
  TokenGenerator tokenGenerator = new TokenGenerator(Constants.FIREBASE_KEY);
  TokenOptions tokenOptions = new TokenOptions();
  tokenOptions.setAdmin(false);
  final String firebaseToken = tokenGenerator.createToken(authPayload, tokenOptions);
  firebase.authWithCustomToken(firebaseToken, new Firebase.AuthResultHandler() {
      @Override
      public void onAuthenticated(AuthData authData) {
          Log.d("Auth", "Success : " + authData.toString());
          Log.d("Auth", "Token : " + firebaseToken);
          SharedPrefs.setFirebaseUserToken(getActivity(), firebaseToken);
      }
  
      @Override
      public void onAuthenticationError(FirebaseError
      firebaseError) {
          firebaseError.toException().printStackTrace();
      }
  });
  </code></pre>
  
  <p>I am trying to push a new message but I am getting error :</p>
  
  <p><strong>RepoOperation﹕ setValue at /message/3_58/-Jy2We4cqLjuQNF6Oyhs failed: FirebaseError: Permission denied</strong></p>
  
  <p>I am unable to figure out where I am going wrong.</p>
  
  <p>This is the code to send chat :</p>
  
  <pre><code>mConversationReferenceFireBase = mFireBase.child("message").child(mConversationId);
      Chat conversation = new Chat( mToken, mUserId, mReceiverId, message );
      mConversationReferenceFireBase.push().setValue(conversation, new Firebase.CompletionListener() {
          @Override
          public void onComplete(FirebaseError firebaseError, Firebase firebase) {
              if (firebaseError != null) {
                  Log.e("Conversation", firebaseError.toString());
              }
          }
      });
  </code></pre>
  
  <p>mConversationId = 3_58</p>
  
  <p>The token here is generated for a user. We have a separate server to maintain the user accounts. The token is being used to upload/ download any files, the firebase is used as Chat Server. </p>
  
  <p>With the rules set to .read = true and .write = true; everything works, however when I am attempting to have an authentication performed, it results in the error mentioned above. I've tried using the token from token generator, to check if I may possibly be using the wrong token.</p>
  
  <p>I am following this example to generate token for firebase auth :</p>
  
  <p><a href="https://www.firebase.com/docs/web/guide/login/custom.html" rel="nofollow">https://www.firebase.com/docs/web/guide/login/custom.html</a> </p>
  
  <p>Since storing a firebase secret key is bad in terms of security, what other alternative can be followed to generate a token for authentication? </p>