使用 Google Cloud 密钥管理服务签署 JSON Web 令牌

Jim*_*den 5 node.js jwt google-cloud-platform google-cloud-kms

编辑:我找到了答案。滚动到这个问题的底部。

我在 NodeJS 身份验证服务器上工作,我想使用 google 签名对 JSON Web 令牌 (JWT) 进行签名。

我正在使用 Google Cloud Key Management Service (KMS) 并创建了一个密钥环和一个非对称签名密钥。

这是我获取签名的代码:

signatureObject = await client.asymmetricSign({ name, digest })

signature = signatureObject["0"].signature
Run Code Online (Sandbox Code Playgroud)

我的 Google 签名对象如下所示:

在此处输入图片说明

我的问题:如何使用 Google 签名签署 JWT?

或者换句话说,我如何将 Google 签名连接到 JWT 的 (header.payload)?

JWT 应该如下所示:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ. (GoogleSignature)
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码:

签名:

async function sign(message, name) {
  hashedMessage = crypto.createHash('sha256').update(message).digest('base64');
  digest = { 'sha256': hashedMessage }

  signatureObject = await client.asymmetricSign({ name, digest }).catch((err) => console.log(err))
  signature = signatureObject["0"].signature
  signJWT(signature)
}
Run Code Online (Sandbox Code Playgroud)

创建 JWT:

function signJWT(signature) {
  header = {
    alg: "RS256",
    typ: "JWT"
  }

  payload = {
    sub: "1234567890",
    name: "John Doe",
    iat: 1516239022
  }

  JWT = base64url(JSON.stringify(header)) + "." +
        base64url(JSON.stringify(payload)) + "." + 
        ???signature??? ; // what goes here?
}
Run Code Online (Sandbox Code Playgroud)

验证:

async function validateSignature(message, signature) {
  // Get public key
  publicKeyObject = await client.getPublicKey({ name }).catch((err) => console.log(err))
  publicKey = publicKeyObject["0"].pem

  //Verify signature
  var verifier = crypto.createVerify('sha256');
  verifier.update(message)
  var ver = verifier.verify(publicKey, signature, 'base64')

  // Returns either true for a valid signature, or false for not valid.
  return ver
}
Run Code Online (Sandbox Code Playgroud)

答案:

我可以像这样使用 toString() 方法:

signatureString = signature.toString('base64');
Run Code Online (Sandbox Code Playgroud)

然后我可以通过使用获得原始签名八位字节流

var buffer = Buffer.from(theString, 'base64');
Run Code Online (Sandbox Code Playgroud)

Joh*_*ley 2

您没有在问题中发布代码,所以我不知道您如何构建 JWT 进行签名。

[代码添加到问题后编辑 2019 年 1 月 18 日]

您的代码正在向后进行签名。您正在创建签名并尝试将其附加到 JWT 标头 + 有效负载。您希望采用 JWT 标头 + 有效负载并对该数据进行签名,然后将签名附加到 JWT 以创建签名 JWT。

使用您的源代码的伪代码:

body_b64 = base64url(JSON.stringify(header)) + "." + base64url(JSON.stringify(payload))

signature = sign(body_b64, name);

jwt = body_b64 + '.' + base64url(signature)
Run Code Online (Sandbox Code Playgroud)

注意:我不确定签名返回的数据格式是什么signatureObject["0"].signature。在转换为 Base64 之前,您可能必须先对其进行转换。

[编辑结束]

示例数据:

智威汤逊头部:

{
    alg: RS256
    kid: 0123456789abcdef62afcbbf01234567890abcdef
    typ: JWT
}
Run Code Online (Sandbox Code Playgroud)

智威汤逊有效负载:

{
  "azp": "123456789012-gooddogsgotoheaven.apps.googleusercontent.com",
  "aud": "123456789012-gooddogsgotoheaven.apps.googleusercontent.com",
  "sub": "123456789012345678901",
  "scope": "https://www.googleapis.com/auth/cloud-platform",
  "exp": "1547806224",
  "expires_in": "3596",
  "email": "someone@example.com.com",
  "email_verified": "true",
  "access_type": "offline"
}
Run Code Online (Sandbox Code Playgroud)

算法:

SHA256withRSA
Run Code Online (Sandbox Code Playgroud)

创建签名 JWT (JWS):

第 1 步:获取 JWT 标头并转换为 Base-64。我们称之为 hdr_b64。

第 2 步:获取 JWT 有效负载并转换为 Base-64。我们称之为payload_b64。

步骤 3:将编码后的标头和有效负载连接起来,.中间用一个点:hdr_b64+ '.' +有效负载_b64`。我们称之为 body_b64。

步骤 4:通常,JWS 使用私钥通过 SHA256withRSA(通常称为“RS256”)进行签名:

signature = sign(body_b64, RS256, private_key)
Run Code Online (Sandbox Code Playgroud)

现在将签名转换为 Base-64。我们将此称为signature_b64。

创建最终的 JWS:

jws = body_b64 + '.' + 签名_b64。

建议:

您想使用 KMS 创建签名 JWT 吗?我不会推荐这个。访问 KMS 中存储的密钥需要付费。签名 JWT 使用私钥进行签名并使用公钥进行验证。您将如何发布公钥?您在访问私钥和公钥时需要什么性能级别(您签名和验证的频率)?

当您在 Google Cloud Platform 中创建服务帐户时,系统会为您创建一个密钥对。该密钥对具有一个 ID,其中包含 Internet 上可用的公钥,而私钥则存在于服务帐户 Json 凭证文件中。我将使用服务帐户来创建签名 JWT,而不是 KMS 中的密钥对。

用于创建和签名的 Python 示例代码:

def create_signed_jwt(pkey, pkey_id, email, scope):
    '''
    Create a Signed JWT from a service account Json credentials file
    This Signed JWT will later be exchanged for an Access Token
   '''

    import jwt

    # Google Endpoint for creating OAuth 2.0 Access Tokens from Signed-JWT
    auth_url = "https://www.googleapis.com/oauth2/v4/token"

    issued = int(time.time())
    expires = issued + expires_in   # expires_in is in seconds

    # Note: this token expires and cannot be refreshed. The token must be recreated

    # JWT Headers
    headers = {
        "kid": pkey_id, # This is the service account private key ID
        "alg": "RS256",
        "typ": "JWT"    # Google uses SHA256withRSA
    }

    # JWT Payload
    payload = {
            "iss": email,           # Issuer claim
            "sub": email,           # Issuer claim
            "aud": auth_url,        # Audience claim
            "iat": issued,          # Issued At claim
            "exp": expires,         # Expire time
            "scope": scope          # Permissions
    }

    # Encode the headers and payload and sign creating a Signed JWT (JWS)
    sig = jwt.encode(payload, pkey, algorithm="RS256", headers=headers)

    return sig
Run Code Online (Sandbox Code Playgroud)