Firebase Admin SDK - 如何使用 OAuth2 刷新 token 进行初始化并与 Web 应用程序安全通信?

标签 firebase oauth-2.0 firebase-authentication firebase-admin

我正在开发一个网络应用程序(使用 Angular ),它与 Firebase 一起使用。 .

我安装了 ngx-auth-firebaseui这是一个易于使用的库,我用来执行用户登录。由于我需要自定义 API,我还开发了 ExpressJs使用 Firebase Admin SDK 的服务器。

我现在调用 ExpressJs API 时没有任何安全性(因为我仍在本地环境中)。

为了使用 Firebase Admin SDK,我遵循了官方文档,其中说明:

Once you have created a Firebase project, you can initialize the SDK with an authorization strategy that combines your service account file together with Google Application Default Credentials.

Firebase projects support Google service accounts, which you can use to call Firebase server APIs from your app server or trusted environment. If you're developing code locally or deploying your application on-premises, you can use credentials obtained via this service account to authorize server requests.

[...] When authorizing via a service account, you have two choices for providing the credentials to your application. You can either set the GOOGLE_APPLICATION_CREDENTIALS environment variable, or you can explicitly pass the path to the service account key in code. The first option is more secure and is strongly recommended.

我在本地设置了 GOOGLE_APPLICATION_CREDENTIALS 属性,一切正常。生产环境中是否也需要设置或者有其他方法吗?

我还想使用 OAuth2 来保护 Web 应用程序和服务器 API 之间的通信,但我不知道如何将其集成到身份验证流程中。

此外,Firebase 文档指出:

The Admin SDKs also provide a credential which allows you to authenticate with a Google OAuth2 refresh token:

var refreshToken; // Get refresh token from OAuth2 flow

admin.initializeApp({
  credential: admin.credential.refreshToken(refreshToken),
  databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});

但如果我这样做,我会收到错误:

.../node_modules/firebase-admin/lib/auth/credential.js:47
    var tmp = from[key] || from[alt];
                  ^

TypeError: Cannot read property 'clientId' of undefined

最佳答案

问题 1:Fireabase Admin SDK 初始化 - 如何处理本地与生产环境?

回答:使用 Firebase Admin SDK 需要在本地开发环境以及生产服务器环境中进行初始化。在这两种环境中,您可以设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量,也可以在代码中显式传递服务帐户 key 的路径。


问题 2:如何使用 OAuth2 在 Wep 应用程序和服务器 API 之间进行安全通信?

答案:Verify Id Tokens

After a successful sign-in, send the user's ID token to your server using HTTPS. Then, on the server, verify the integrity and authenticity of the ID token and retrieve the uid from it. You can use the uid transmitted in this way to securely identify the currently signed-in user on your server.


问题 3:如何使用 Firebase Admin SDK 通过 Google OAuth2 刷新 token 进行身份验证?

来自 Manage User Sessions 的回答

Firebase Authentication sessions are long lived. Every time a user signs in, the user credentials are sent to the Firebase Authentication backend and exchanged for a Firebase ID token (a JWT) and refresh token.

在客户端(Web 应用)上,Firebase 用户有 refreshToken属性来检索当前刷新 token 。

但是,初始化 Admin SDK 的标准方法是设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量或在代码中显式传递服务帐户 key 的路径。

关于Firebase Admin SDK - 如何使用 OAuth2 刷新 token 进行初始化并与 Web 应用程序安全通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733072/

相关文章:

java - 如何获取子级内部的 dataSnapshot.getChildrenCount()

java - android studio firebase gettoken id

java - 如何使用匿名身份验证将数据保存到Android中的Firestore数据库?

node.js - 使用服务帐户将 Firebase 应用程序部署到 Heroku(使用 dotenv 的环境变量)

javascript - 如何从经过 Firebase 身份验证的用户那里获取提供商访问 token ?

android - 如何在 Firebase 中从 android 应用程序创建一个空表?

javascript - Firebase 通过键从查找中删除

代表流程的 azure 广告范围始终重新审视所有同意的范围

Salesforce OAuth 2.0 API : get refresh_token with authorization URL launched in a normal browser

java - Oauth2 Java 配置 JDBC