繁体   English   中英

如何在 Firebase 身份验证中限制 email 域

[英]How to restrict email domains in Firebase Authentication

我有一个关于 firebase 身份验证的问题。 实际上,我正在为我的公司制作仪表板,并将其托管在 firebase 中。 我想将 email 身份验证仅限于我的公司域(例如:cat.com)。 但是我浏览了stackoverflow的答案,发现我可以在数据库中施加规则。 但问题是我将使用 Firebase Function 调用外部数据库来 fetcj 数据并将其提供给网站(仪表板)。 因此,没有特定领域的规则将适用于那里。 下面是我的仪表板架构的大纲

在此处输入图像描述

我怎样才能做到这一点? 我希望拥有“xxxx@cat.com”的人能够验证和查看仪表板数据

如果您的公司使用 GSuite 并通过“使用 Google 登录”登录

Firebase 的 Google 登录建立在顶级普通 Google 登录 + 大量自动化的基础上。其中包括当他们在 GCP 中创建新的 OAuth 2.0 客户端时的部分。 这将被命名为Web client (auto created by Google Service)

网络客户端

此客户端自动链接到OAuth 同意屏幕,您可以在其中提及您的应用程序的显示名称,并将其限制为组织中使用 Google 帐户的用户

在此处输入图像描述

如果贵公司使用 Email & 密码登录

最简单的方法是立即通过 firebase 后台身份验证触发器onCreate立即检查组织 email,如 Ben 的回答中所述。 如果该帐户不属于您的组织 - 立即将其删除。

这将暂时让恶意用户访问您的系统。 为了进一步保护,您可以为您的组织用户设置自定义声明(当他们注册时 - 在 firebase 函数中)并确保对 Firestore/实时数据库的每个请求都检查了这些自定义声明。 同样,您可以在调用数据库之前检查 firebase function 中的自定义声明

案例 1:用户已经创建了自己的账户,并且您希望将一个云 function 限制为特定的 email 地址。

您可以获取与云 function 调用相关的用户信息,并查看他们的 email。 然后,如果它们具有正确的 email 域,则可以调用外部数据库。 您还应该进行一些 UI 更改,以便用户在没有@cat.com时不会收到错误。


案例 2:将 Firebase 项目中的所有用户限制为包含@cat.com的电子邮件?

如果是这样,您不能直接在 firebase 身份验证中限制电子邮件,因此您必须将用户注册码粘贴在云 function 后面,在那里创建用户帐户。 然后,您可以在他们尝试注册时检查他们的 email。

您可以使用云 function 中的 Firebase 管理员 SDK 来执行此操作。 文档

admin.auth().createUser({
  email: 'user@example.com',
  emailVerified: false,
  phoneNumber: '+11234567890',
  password: 'secretPassword',
  displayName: 'John Doe',
  photoURL: 'http://www.example.com/12345678/photo.png',
  disabled: false
})
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log('Successfully created new user:', userRecord.uid);
  })
  .catch(function(error) {
    console.log('Error creating new user:', error);
  });

The client will call the cloud function with their desired email and password, and before calling this .createUser , and you can check for the correct email before creating the user with "dog@cat.com".toLowerCase().endsWith("cat.com")


或者,您可以为@frunkad 提到的用户设置自定义声明:为使用“@cat.com”电子邮件注册的用户提供额外权限,如下所示: Defining roles via Firebase Functions on user creation 但是,在 OP 的情况下,只有具有“@cat.com”的用户才能注册,因此自定义声明使问题过于复杂。

此外,使用 email 域作为访问控制形式听起来不是一个好主意。 在帐户创建过程中,您根据 email 手动添加对用户文档的访问权限。 当您想给某人一个 email 但不想让他们访问数据库时会发生什么?

在您的 firebase 安全规则中,只需粘贴此

这对我有用,我可以将登录的 Google 用户限制在我的 org 域

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.token.email.matches('.*@domain[.]com');
    }
  }
}

自 2022 年 8 月起,您可以编写阻止功能,如果不满足某些要求,可以阻止用户创建帐户。 链接的文档专门显示了这个例子:

 export const beforecreated = beforeUserCreated((event) => { const user = event.data; // (If the user is authenticating within a tenant context, the tenant ID can be determined from // user.tenantId or from event.resource, eg 'projects/project-id/tenant/tenant-id-1') // Only users of a specific domain can sign up. if (user?.email?.includes('@acme.com')) { throw new HttpsError('invalid-argument', "Unauthorized email"); } });

使用阻塞功能需要您使用Firebase Authentication with Identity Platform

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM