Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine if a Firebase user's UID is valid?

I am building a server route that I wish to restrict for use only by authenticated users. I plan to send a user.uid with a POST to this route, and I want to validate the UID is one that exists in Firebase. I know I can add UIDs manually in Firebase and check against this data, but is it possible to see what UIDs Firebase authentication is tracking? I think this approach would be better then checking against my own list.

The purpose for this is to ensure that these routes are not accessed with a phony UID (e.g. for malicious purposes).

like image 536
skwny Avatar asked Feb 18 '17 17:02

skwny


People also ask

How do you check if a user is authenticated in Firebase?

var user = firebase. auth(). currentUser; if (user) { // User is signed in. } else { // No user is signed in. }

Is Firebase uid unique?

Auth tokens These tokens contain basic profile information for a user, including the user's ID string, which is unique to the Firebase project.

Is Firebase uid secret?

When your app grows, you do not want to share the UID or pass it around. Also when setting firebase-rules, you'll be referring to UID, which should be kept private.

Does Firebase uid change?

As said by @user663031, the answer "no" is correct.


2 Answers

Validating a UID is not enough to block malicious users: 1) the attackers could pretend to be other users by sending other user's UID, and 2) UID never changes or expires, which means there is no way to enforce the users (or attackers) to re-authenticate.

What you need is to pass the Firebase token from client app to your server, and validate the token before accepting it.

  • The token is securely signed by Firebase private key. No other party can issue a valid Firebase token.

  • The token is valid for only one hour. Firebase server will check the account status (e.g. password change event) before issuing a new token.

  • The token payload contains UID and audience. You should verify audience is your own application.

You can use Firebase Admin SDK or third party libraries to verify a Firebase token. See Firebase doc for details.

like image 147
Jin Liu Avatar answered Oct 02 '22 12:10

Jin Liu


You can check whether a specific UID corresponds to a Firebase Authentication user in your project by using the Firebase Admin SDK on your server. From the Firebase documentation on retrieving user data:

admin.auth().getUser(uid)
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully fetched user data:", userRecord.toJSON());
  })
  .catch(function(error) {
    console.log("Error fetching user data:", error);
  });
like image 41
Frank van Puffelen Avatar answered Oct 02 '22 11:10

Frank van Puffelen