【问题标题】:Firebase & Postman | Generate JWT for Google Identity OAuth 2.0 tokenFirebase 和邮递员 |为 Google Identity OAuth 2.0 令牌生成 JWT
【发布时间】:2020-12-11 13:34:53
【问题描述】:

我正在尝试在我的 Cloud Firestore 上执行一些管理任务(上传一些数据等)。我通读了文档here

使用 Google Identity OAuth 2.0 令牌和服务帐户来验证来自您的应用程序的请求,例如数据库管理请求。

这参考了文档here关于如何在生成 JWT 后进行授权 API 调用。我正在努力生成 JWT。

这是我尝试过的:

  1. 我形成了标题和声明集

    {"alg":"RS256","typ":"JWT"}.
    {
    "iss":"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",
    "scope":"https://www.googleapis.com/auth/prediction",
    "aud":"https://oauth2.googleapis.com/token",
    "exp":1328554385,
    "iat":1328550785
    }
    
  2. 前往https://jwt.io/ 使用 RSA256 生成 JWT。它要求提供公钥和私钥。我知道可以从我在https://console.cloud.google.com/iam-admin/serviceaccounts 下创建的服务帐户以 json 格式生成私钥。但是,我不确定从哪里获得公钥。 jwt.io 不生成 jwt。我只得到“无效签名”。

网络上有很多使用 SDK 或库的示例。但是,我看不到任何有关如何手动生成 JWT(如果可能的话)以将其与 Postman 一起使用的示例。有什么想法吗?

【问题讨论】:

  • 我写了一篇文章,展示了如何创建 JWT,对其进行签名,然后交换访问令牌。这也将解释所需的步骤。您可以使用 openssl openssl rsa -in private.pem -outform PEM -pubout -out public.pem 从私钥轻松获取公钥。但是,Google 使用公钥来验证您的私钥签名,您不需要它。 jhanley.com/… 这是创建访问令牌的快捷方式:cloud.google.com/sdk/gcloud/reference/auth/print-access-token
  • @JohnHanley 非常感谢。这将我推向了正确的方向。我现在设法生成访问令牌。我会把步骤写在答案中以供将来参考
  • 这能回答你的问题吗? Postman - JWT authentication using key file

标签: firebase google-cloud-platform jwt postman


【解决方案1】:

感谢@JohnHanley。我设法生成了令牌。这不完全是 Postman 的问题(我仍然依赖 jwt.io 来生成 jwt)。

  1. 在创建服务帐户here 之后。添加一个密钥并下载 p12 文件(不是 json)。默认密码是notasecret

  2. 将 p12 转换为 pem 并提取公钥:
    $openssl pkcs12 -in postman-admin-private.p12 -out postman-admin-private.pem -nodes
    $openssl rsa -in postman-admin-private.pem -outform PEM -pubout -out postman-admin-public.pem

  3. 打开两个 pem 文件并将私钥和公钥复制到 jwt.io(使用 RSA256 选项)

  4. 确保在iss 字段中使用服务帐户的电子邮件地址

  5. 这是邮递员中的请求:

    curl --location --request POST 'https://oauth2.googleapis.com/token?grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion='{generated jwt}'

下一步将完全在 Postman 中实现。我没有尝试过,但这个post 似乎是一个选项

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2021-04-14
    • 2023-03-13
    • 2014-11-17
    • 2021-01-29
    • 1970-01-01
    • 2013-01-05
    相关资源
    最近更新 更多