【问题标题】:How to validate an authentication token against firebase?如何针对 Firebase 验证身份验证令牌?
【发布时间】:2016-09-29 15:15:00
【问题描述】:

我不是指使用 firebase 进行自定义身份验证。我需要的与在应用程序服务器中生成令牌并允许在 firebase 中访问的自定义身份验证略有不同。 实际上,我正在尝试使用 e-mail 和 password 在 firebase 中进行身份验证,并且通过该身份验证能够访问某些应用程序服务器中的 restful 服务。 这可能吗 ? 我认为在 firebase 身份验证后,可以通过某种方式将令牌发送到应用程序服务器,并且该服务器将针对 firebase 验证身份验证令牌。

Client --------authenticates ------->> Firebase
Client <<--------auth token ---------- Firebase
Client --------- sends ------------->> Application server (NodeJS)
App Server ------- validates (auth token) ---->> Firebase

提前致谢。

【问题讨论】:

    标签: node.js firebase token firebase-authentication


    【解决方案1】:

    客户端 --------验证 ------->> Firebase

    import firebase from 'firebase/app';
    import 'firebase/auth';
    import 'firebase/firestore';
    
    const googleAuthProvider = new firebase.auth.GoogleAuthProvider();
    const auth = firebase.auth();
    
    const authenticates = await auth.signInWithPopup(googleAuthProvider).then(user => user).catch(err => err)
    

    客户端

    您将从authenticates响应中获取数据

    authtoken = authenticates.credential.idToken
    email = authenticates.user.email
    ...
    

    客户端 --------- 发送 ------------->> 应用服务器(NodeJS)

    const sends = await axios({
        method: 'post',
        url: `${API_BASE_URL}/request`,
        headers: {
            'Authorization': `Bearer ${authtoken}`,
        },
        data: {
            from: next_cursor,
            size: next_cursor + 100,
        }
    });
    

    应用服务器 ------- 验证(身份验证令牌)---->> Firebase

    当我们初始化 firebase 身份验证时,我们将拥有 app_oauth2_client_id

    import { OAuth2Client } from 'google-auth-library';
    
    const oauth2Client = new OAuth2Client(process.env.app_oauth2_client_id);
    
    function verifyOauth2Token(token) {
      const ticket = await oauth2Client.verifyIdToken({
        idToken: token,
        audience: [process.env.app_oauth2_client_id]
      });
      return ticket.getPayload();
    }
    
    const tokenInfo = await verifyOauth2Token(token);
    

    tokenInfor

    {
      iss: 'accounts.google.com',
      azp: '671303332471-5n8014rorllmd09n8mmadesc2qidpda5.apps.googleusercontent.com',
      aud: '671303332471-5n8014rorllmd09n8mmadesc2qidpda5.apps.googleusercontent.com',
      sub: '100037911230177975416',
      email: 'testapp@gmail.com',
      email_verified: true,
      at_hash: '3rxsMOftrr9NZWlBkYznuQ',
      iat: 1635842823,
      exp: 1635846423
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 iOS、Web 和 Android 中可用的异步 getToken 方法获取令牌

      网页:

      https://firebase.google.com/docs/reference/js/firebase.User#getToken

      iOS: https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_user#properties

      安卓: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser.html#public-constructor-summary

      并将该令牌发送到您的后端服务器,然后您可以使用服务器中的 verifyIdToken 方法来验证令牌并获取令牌的 uid

      服务器方法 https://firebase.google.com/docs/auth/server#verify_id_tokens

      【讨论】:

      • 这正是我想要的。谢谢。
      • verifyIdToken 只验证token,自定义服务器中的黑名单或禁用账户如何处理?
      猜你喜欢
      • 1970-01-01
      • 2022-07-16
      • 1970-01-01
      • 2019-06-28
      • 2019-03-05
      • 2021-12-17
      • 1970-01-01
      • 2021-09-23
      相关资源
      最近更新 更多