【问题标题】:Generates a signed JSON Web Token using a Google API Service Account in nodejs在 nodejs 中使用 Google API 服务帐户生成签名的 JSON Web 令牌
【发布时间】:2021-11-26 22:29:44
【问题描述】:

我有 express/nodejs api 与我们的 java 后端服务(不是谷歌 API)进行通信。我需要将 google JWT 令牌传递给 api 调用。

如何使用 Google API 服务帐户生成签名的 JSON Web 令牌。我看到的大多数示例都获得了访问令牌或刷新。

感谢您的帮助

【问题讨论】:

  • 1) 使用您想使用什么工具/库来创建 JWT。 JWT 只是两个 JSON 对象与签名连接在一起。 2)您将出于什么目的使用 JWT? (后端服务不够详细)这将决定创建什么样的JWT。 3) 大多数 Google API 不接受 JWT,需要 OAuth 访问令牌或 OIDC 身份令牌。如果后端执行自己的令牌验证,您可以使用后端支持的任何内容。 4)用细节编辑你的问题,你的概括描述不足以创建一个可靠和安全的答案。
  • 同意约翰的评论。另外,什么是生成令牌?在哪里?在 GCP 上,在你的工作站上?其他地方?

标签: node.js express google-cloud-platform


【解决方案1】:

看看JSON Web Tokens

【讨论】:

  • 谢谢,但我看不到任何示例生成 JWT 令牌的位置。它们都生成访问或刷新令牌
  • 你是对的。我很抱歉。我只是有同样的问题。这是一个 JWT,但 access_token 包含一个以 ya29 令牌开头的值(对吗?)而不是 ey 身份令牌。我使用 Golang,发现了一个返回身份令牌的 Google idtoken 包(作为 access_token 的值)。我假设 Google 有一个等效的 Node.JS 库。 Golangidtoken
  • 这是我在 Golang 中所做的 Node.JS 等价物,它使用 Google Cloud Run 服务的 audience 值(必需)创建身份令牌。见:github.com/googleapis/google-auth-library-nodejs/blob/main/…
【解决方案2】:

在本例中,这段 Java 代码用于生成创建 JWT 的函数,然后使用私钥文件对其进行签名,并返回签名后的 JWT。您可以在自己的代码中使用它。

/**
 * Generates a signed JSON Web Token using a Google API Service Account
 * utilizes com.auth0.jwt.
 */
public static String generateJwt(final String saKeyfile, final String saEmail,
    final String audience, final int expiryLength)
    throws FileNotFoundException, IOException {

  Date now = new Date();
  Date expTime = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expiryLength));

  // Build the JWT payload
  JWTCreator.Builder token = JWT.create()
      .withIssuedAt(now)
      // Expires after 'expiryLength' seconds
      .withExpiresAt(expTime)
      // Must match 'issuer' in the security configuration in your
      // swagger spec (e.g. service account email)
      .withIssuer(saEmail)
      // Must be either your Endpoints service name, or match the value
      // specified as the 'x-google-audience' in the OpenAPI document
      .withAudience(audience)
      // Subject and email should match the service account's email
      .withSubject(saEmail)
      .withClaim("email", saEmail);

  // Sign the JWT with a service account
  FileInputStream stream = new FileInputStream(saKeyfile);
  ServiceAccountCredentials cred = ServiceAccountCredentials.fromStream(stream);
  RSAPrivateKey key = (RSAPrivateKey) cred.getPrivateKey();
  Algorithm algorithm = Algorithm.RSA256(null, key);
  return token.sign(algorithm);
}

您可以查看 Google 的 authentication between services 文档。

编辑答案

我与一位 Google 开发人员进行了内部咨询,以检查是否有一段关于您的请求的 node.js 代码。 我对您的建议是使用以下信息进行测试。

https://github.com/googleapis/google-auth-library-nodejs/#working-with-id-tokens

https://github.com/googleapis/google-auth-library-nodejs/blob/main/samples/jwt.js

https://www.bezkoder.com/node-js-mongodb-auth-jwt/

【讨论】:

  • 谢谢.. 我有这个代码,但我找不到它的 nodejs 版本.. 我查看了“google-auth-library”库,但没有运气
猜你喜欢
  • 2021-12-06
  • 1970-01-01
  • 2013-05-25
  • 2022-10-18
  • 1970-01-01
  • 2019-01-05
  • 2022-10-18
  • 2015-02-11
  • 1970-01-01
相关资源
最近更新 更多