使用 Microsoft Azure/Graph 进行 Firebase 自定义身份验证

2024-04-01

我正在使用 Microsoft Graph 构建一个企业应用程序来登录。成功签名后,我想使用要发送的令牌来对 Firebase Auth 进行身份验证(这样我就可以保护对数据库的访问)。

成功登录后收到的令牌不能直接用于 Firebase。

On the Firebase 自定义身份验证说明页面 https://firebase.google.com/docs/auth/ios/custom-auth?authuser=0它说:

获取项目的服务器密钥:

  1. 转到项目设置中的服务帐户页面。
  2. 单击服务帐户页面 Firebase Admin SDK 部分底部的生成新私钥。
  3. 新服务帐户的公钥/私钥对会自动保存在您的计算机上。将此文件复制到您的身份验证服务器。

第三点说需要向认证服务器输入密钥。这可以使用吗微软图谱 or Azure AD?

Firebase 为您提供的密钥是JSON文件。我查过微软应用程序注册门户它允许您编辑应用程序清单,但没有运气。

The JSON文件看起来像这样:

{
    "type": "service_account",
    "project_id": "APP_ID",
    "private_key_id": "KEY_ID_VALUE",
    "private_key": "-----BEGIN PRIVATE KEY----<KEY VALUE>-----END PRIVATE KEY-----\n",
    "client_email": "firebase-adminsdk-0ubvc@********.iam.gserviceaccount.com",
    "client_id": "XXXXXXXXXXXX",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-0ubvc%XXXXXXXX.iam.gserviceaccount.com"
}

我似乎找不到任何涉及此问题的 github 项目或 stackoverflow 线程。

如何使用 MS Graph 或 Azure AD 接收自定义令牌?


我现在已经完全解决了这个问题。通过广泛的研究和 Stackoverflow 上的大量帮助,我设法解决了这个问题。

UPDATE

数据库机密当前已弃用,并使用旧版 Firebase 令牌生成器。现在Firebase Admin进口就足够了。

所以,这些是我的发现:

1。你DO在创建 Firebase 令牌时需要将您的私钥发送到您的 Firebase 函数。在 Firebase 控制台中,您可以提取密钥并将文件重命名为service-account.json。这应该放在你的功能执行之前的文件夹Firebase deploy罢工>

  1. In your index.js文件,您可以通过输入以下代码获取您的服务文件:

    const admin = require('firebase-admin');
    
  2. 编写用于接受来自其他身份验证服务的信息的函数:

    // Create a Firebase token from any UID
    exports.createFirebaseToken = functions.https.onRequest((req, res) => {
    
      // The UID and other things we'll assign to the user.
      const uid = req.body.uid;
      const additionalClaims = {
        name: req.body.name,
        email: req.body.email
      };
    
      // Create or update the user account.
      const userCreationTask = admin.auth().updateUser(uid, additionalClaims).catch(error => {
    
        if (req.method === 'PUT') {
          res.status(403).send('Forbidden!');
        }
    
        if (req.method === 'GET') {
         res.status(403).send('Please use POST for this function');
        }
    
        // If user does not exists we create it.
        if (error.code === 'auth/user-not-found') {
          console.log(`Created user with UID:${uid}, Name: ${additionalClaims.name} and e-mail: ${additionalClaims.email}`);
          return admin.auth().createUser({
          uid: uid,
          displayName: additionalClaims.name,
          email: additionalClaims.email,
        });
            }
            throw error;
            console.log('Error!');
        });
    
    
        return Promise.all([userCreationTask]).then(() => {
          console.log('Function create token triggered');
          // Create a Firebase custom auth token.
          admin.auth().createCustomToken(uid, additionalClaims).then((token) => {
          console.log('Created Custom token for UID "', uid, '" Token:', token);
            res.status(200).send(token);
            return token
        });
      });
    });
    

回应非常重要res.status因为这将完成任务。单个return声明不会这样做。 一个完整的工作样本来自Firebase他们自己可以在github https://github.com/firebase/functions-samples/tree/master/linkedin-auth

  1. 您现在可以制作一个HTTP请求看起来像这样使用阿拉莫菲尔 and swift

    Alamofire.request("https://us-central1-<YOUR DATABASE REFERENCE>.cloudfunctions.net/createFirebaseToken", 
    method: .post, parameters: parameters, encoding: JSONEncoding.default).
    responseString(completionHandler: { (token) in
        // Handle the result here
    })
    

    在这种情况下,Parameters是一个常规的JSON文件,其中包含您要添加到用户 Firebase 帐户的所有内容。

  2. 重要的任何知道您的 cloudfunctions URL 的人都可以触发此令牌铸造。因此,请确保添加安全措施来处理此问题。并且也已经讨论过Stackoverflow 中的此线程 https://stackoverflow.com/a/50315013/6706769

  3. 当您的客户收到令牌时,您将进行自定义身份验证登录iOS https://firebase.google.com/docs/auth/ios/anonymous-auth or in Android https://firebase.google.com/docs/auth/android/custom-auth如文档中所述。

  4. 您现在已通过身份验证Firebase and 微软

  5. 我还添加了额外的安全层,通过检查ID我从微软得到的,是一样的ID存储在 Firebase 的经过身份验证的帐户中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Microsoft Azure/Graph 进行 Firebase 自定义身份验证 的相关文章

随机推荐