Google 的 OAuth2 的 RSA private/public 密钥
RSA private/public keys for Google's OAuth2
我正在使用来自 Google Cloud Platform 的服务帐户进行服务器到服务器身份验证。根据 Google's documentation,请求令牌 (JWT) 必须基于 RSA SHA-256 算法,因此使用 RSA 认证私钥签名并使用其各自的 public 密钥解码。
如何让 google api 知道 public 密钥?
我知道 Google 有自己的 RSA certificates 但我不知道如何使用它们。
那么,我应该使用什么 RSA private/public 密钥来进行 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
您误解了Google 云服务帐户和 G Suite 域范围委派。
How do i let google api know the public key?
你不知道。 Google 已经从 Google 作为服务帐户 JSON 文件的一部分提供给您的私钥中知道了 public 密钥。您需要在 Google Cloud Console 中创建一个服务帐户,然后将密钥 material 下载为 JSON 文件。您不能创建自己的私钥。
此外,在您尝试使用 Admin API 时,还必须执行几个步骤。阅读以下 link:
处的文档
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(身份识别代理)一起使用。
我正在使用来自 Google Cloud Platform 的服务帐户进行服务器到服务器身份验证。根据 Google's documentation,请求令牌 (JWT) 必须基于 RSA SHA-256 算法,因此使用 RSA 认证私钥签名并使用其各自的 public 密钥解码。
如何让 google api 知道 public 密钥?
我知道 Google 有自己的 RSA certificates 但我不知道如何使用它们。
那么,我应该使用什么 RSA private/public 密钥来进行 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
您误解了Google 云服务帐户和 G Suite 域范围委派。
How do i let google api know the public key?
你不知道。 Google 已经从 Google 作为服务帐户 JSON 文件的一部分提供给您的私钥中知道了 public 密钥。您需要在 Google Cloud Console 中创建一个服务帐户,然后将密钥 material 下载为 JSON 文件。您不能创建自己的私钥。
此外,在您尝试使用 Admin API 时,还必须执行几个步骤。阅读以下 link:
处的文档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(身份识别代理)一起使用。