NodeJS 示例 - Firebase Cloud Functions - 实例化 Admin SDK Directory 服务对象

Cha*_*add 5 node.js jwt service-accounts google-admin-sdk google-cloud-functions

目标

使用googleapis与火力地堡云功能,能在这个摹套房域中所有用户的列表。

如何实例化 Admin SDK Directory 服务对象。我没有看到 NodeJS 示例,也不清楚如何使用googleapis.

语境

此代码从 Firebase Cloud Functions 运行,并且似乎可以通过身份验证。现在,如何//TODO在以下代码中设置服务对象:

// Firebase Admin SDK
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)

// Google APIs
const googleapis = require('googleapis')
const drive = googleapis.drive('v3')
const gsuiteAdmin = googleapis.admin('directory_v1')

// Service Account Key - JSON
let privatekey = require("./privatekey.json")

let jwtClient = new googleapis.auth.JWT(
    privatekey.client_email,
    null,
    privatekey.private_key,
    ['https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/admin.directory.user'])

// Firebase Cloud Functions - REST
exports.authorize = functions.https.onRequest((request, response) => {
    //authenticate request
    jwtClient.authorize(function (err, tokens) {
        if (err) {
            console.log(err)
            return
        } else {
            console.log("Successfully connected!")
        }

        // TODO
        // USE SERVICE OBJECT HERE??
        // WHAT DOES IT LOOK LIKE?

        response.send("Successfully connected!")
    })
})
Run Code Online (Sandbox Code Playgroud)

Cha*_*add 2

操作顺序:

  1. 在 Google Cloud Console 中创建服务帐号凭据
  2. 将域范围委派添加到服务帐户
  3. 在 G Suite 中授权 API - 安全 - 高级
  4. 返回服务帐户并下载.json密钥文件

.json太早下载了密钥文件,例如,在 G Suite 中授权 API 之前。使用 DwD 设置服务帐户,然后在 G Suite API 中授权 API,然后下载密钥.json文件的顺序很重要。

这个例子

// Firebase Admin SDK
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)

// Google APIs
const googleapis = require('googleapis')
const drive = googleapis.drive('v3')
const directory = googleapis.admin('directory_v1')

// Service Account Key - JSON
let privatekey = require("./privatekey.json")
let impersonator = 'example@example.com'

let jwtClient = new googleapis.auth.JWT(
    privatekey.client_email,
    null, // not using path option
    privatekey.private_key,
    ['https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/admin.directory.user',
        'https://www.googleapis.com/auth/admin.directory.user.readonly'],
    impersonator
)

// Firebase Cloud Functions - REST
exports.getUsers = functions.https.onRequest((request, response) => {
    //authenticate request
    jwtClient.authorize(function (err, tokens) {
        if (err) {
            console.log(err)
            return
        } else {
            console.log("Successfully connected!")
        }
        //Google Drive API
        directory.users.list ({
            auth: jwtClient,
            domain: 'example.com',
            maxResults: 10,
            orderBy: 'email',
            viewType: 'domain_public'
          }, function(err, res) {
            if (err) {
              console.log('The API returned an error: ' + err)
              return;
            }
            var users = res.users;
            if (users.length == 0) {
              console.log('No users in the domain.');
            } else {
              console.log('Users:');
              for (var i = 0; i < users.length; i++) {
                var user = users[i];
                console.log('%s (%s)', user.primaryEmail, user.name.fullName)
              }
              response.send(users)
            }        
        })
    })
})
Run Code Online (Sandbox Code Playgroud)

更新

上面的例子并不安全。云功能(尤其是 G Suite 域范围委派)不应响应 http 请求,除非它们来自您的应用程序。在此示例中,云函数用于admin.auth().verifyIdToken(idToken)...验证请求是否已通过 Firebase 进行身份验证。

如果您没有正确处理 G Suite DwD 云功能,您可能会面临将 G Suite API 暴露给公众的风险。