【问题标题】:RSA private/public keys for Google's OAuth2用于 Google 的 OAuth2 的 RSA 私钥/公钥
【发布时间】:2020-03-14 09:24:25
【问题描述】:

我正在使用来自 Google Cloud Platform 的服务帐户进行服务器到服务器身份验证。根据Google's documentation,请求令牌 (JWT) 必须基于 RSA SHA-256 算法,因此使用 RSA 证书私钥签名并使用其各自的公钥解码。

如何让 google api 知道公钥?

我知道 Google 有自己的 RSA certificates,但我不知道如何准确使用它们。

那么,我应该使用什么 RSA 私钥/公钥来进行 google 的 oauth2 令牌编码?

以下是代码摘录:

import jwt from 'jsonwebtoken'
const axios = require('axios')
const fs = require('fs')
const {
  google
} = require('googleapis')

// oauth2 token uri
const GSUITE_AUD = 'https://oauth2.googleapis.com/token'

const payload = {
  iss: 'example@example.iam.gserviceaccount.com',
  scope: 'https://www.googleapis.com/auth/admin.directory.user',
  aud: GSUITE_AUD,
}

const options = {
  algorithm: 'RS256',
  expiresIn: '10m'
}

const fetchGSuiteUsersUtil = () => {
  let path = require('path'),
    privateKeyFilePath = path.join(__dirname, 'private.key')

  const privateKey = fs.readFileSync(privateKeyFilePath, 'utf-8')

  const request_token = jwt.sign(payload, privateKey, options)

  authorize(request_token, listUsers)

  function authorize(request_token, callback) {
    const TOKEN_URI = GSUITE_AUD
    const data = {
      grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      assertion: request_token
    }
    axios.post(TOKEN_URI, data)
      .then((res) => {
        console.log(`statusCode: ${res.statusCode}`)
        console.log(res)
        // callback(res)
      })
      .catch((error) => {
        /**
         * Here i get the error 'Invalid JWT signature' and sounds logic
         * since it doesn't know the public key for the RSA certificated
         * private key that i used
         * 
         */
        console.error(error)
      })
  }

  function listUsers(auth) {
    const service = google.admin({
      version: 'directory_v1',
      auth,
    })
    service.users.list({
        customer: 'my_customer',
        orderBy: 'email',
        maxResults: 500,
        query: 'orgUnitPath=/'
      },
      async(err, res) => {
        // Do things once i get the token..
      })
  }

}

export default fetchGSuiteUsersUtil

【问题讨论】:

    标签: node.js google-cloud-platform oauth-2.0 google-workspace google-cloud-iam


    【解决方案1】:

    您误解了 Google 云服务帐户和 G Suite 域范围委派。

    如何让 google api 知道公钥?

    你没有。 Google 已经从 Google 作为服务帐户 JSON 文件的一部分提供给您的私钥中知道了公钥。您需要在 Google Cloud Console 中创建一个服务帐号,然后将密钥材料下载为 JSON 文件。您不能创建自己的私钥。

    此外,由于您尝试使用 Admin API,因此必须遵循几个步骤。阅读以下链接中的文档:

    Perform G Suite Domain-Wide Delegation of Authority

    我写的这篇文章解释了使用服务帐户私钥、创建签名 JWT 和交换 OAuth 访问令牌的过程。

    Google Cloud – Creating OAuth Access Tokens for REST API Calls

    还有一点。在您的代码中,您指定了 audience。如果您为令牌指定受众,您将不会获得 OAuth 访问令牌。您将获得一个 OIDC 身份令牌,用于基于身份的授权。一个示例是使用带有 Google Cloud IAP(身份识别代理)的身份令牌。

    【讨论】:

    • 太棒了。正如您所说,通过使用服务帐户 JSON 文件中的 private_key 解决了问题。为了使用 Admin SDK,我已经完成了前面的所有步骤,因为它已经在运行,但是它正在使用 GSuite 的管理员用户而不是服务帐户进行身份验证。还必须在 JWT 请求令牌中提供 sub 字段:(应用程序为其请求委托访问的用户的电子邮件地址)[developers.google.com/identity/protocols/oauth2/…
    猜你喜欢
    • 2012-01-28
    • 2020-04-07
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2017-12-23
    相关资源
    最近更新 更多