How to impersonate the database authentication from Firebase Cloud Function with admin privileges?

假装没事ソ 提交于 2020-08-08 04:31:25

问题


I have a set of Firebase Functions that works as an API for an application.

These Firebase Functions connect to the database with admin privileges as this:

const serviceAccount = require(`./config/xxx-firebase-adminsdk.json`);
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: DATABASE_URL,
});

I was able to create a custom token and I'm receiving/decoding it in the server but I cannot make use of the security rules because the Firebase function is authenticated with admin privileges.

Is there a way of impersonating the credentials or maybe pass a different user when accessing the database?


回答1:


You can initialize the admin SDK with user-level privileges as described in the documentation:

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://databaseName.firebaseio.com",
  databaseAuthVariableOverride: {
    uid: "the-user-id"
  }
}, "some-other-name");

"some-other-name" is required if you've already initialized the default instance. See initializeApp() for more details.

You obviously have to know the UID of the authenticated user in order to make this work. Also, it should be clear that you have to init the SDK every time you want to do something with a different user, which means you're going to have to init on every function call where the UID could be different.



来源:https://stackoverflow.com/questions/47780062/how-to-impersonate-the-database-authentication-from-firebase-cloud-function-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!