如何计算服务帐户的OAuth签名?

时间:2019-04-04 09:04:23

标签: java oauth jwt signature

我想使用HTTP / REST方法get a service account access token使用Google API,但是OAuth向我发送了签名错误。我正在使用Java。

我收到了以下JSON字符串:

{
    "error": "invalid_grant",
    "error_description": "Invalid JWT Signature."
}

据我所知,在文档中,我需要组成一个带有标题,声明集和签名的JWT(JSON Web令牌)。我设法获得了正确的标题和声明集,但显然我的签名计算不正确。

我的进口商品:

import java.security.KeyFactory;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.PrivateKey;

这是标题计算:

String header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
byte[] bytes = header.getBytes();
header = Base64.getEncoder().encodeToString(bytes);

我的声明集

long time_milis = System.currentTimeMillis()/1000L;
String claim_set = "{\n"
     + "  \"iss\":\"something@something.iam.gserviceaccount.com\",\n"
     + "  \"scope\":\"https://www.googleapis.com/auth/devstorage.read_write\",\n"
     + "  \"aud\":\"https://www.googleapis.com/oauth2/v4/token\",\n"
     + "  \"exp\":" + (time_milis + 300) + ",\n"
     + "  \"iat\":" + time_milis + "\n"
     + "}";
bytes = claim_set.getBytes();
claim_set = Base64.getEncoder().encodeToString(bytes);

为我的签名尝试做好准备! (出于明显的安全原因,我屏蔽了私钥):

String signature = header + "." + claim_set;
String privateKeyString = "-----BEGIN PRIVATE KEY-----\nturlututu123\n-----END PRIVATE KEY-----\n";
privateKeyString = private_key.replaceAll("-----END PRIVATE KEY-----", "").replaceAll("-----BEGIN PRIVATE KEY-----", "").replaceAll("\n", "");

Signature privateSignature = Signature.getInstance("SHA256withRSA");

KeyFactory keyFactory=KeyFactory.getInstance("RSA");

PrivateKey privateKey = keyFactory.generatePrivate(
    new PKCS8EncodedKeySpec(
        Base64.getDecoder().decode(privateKeyString)
    )
);

privateSignature.initSign(privateKey);

privateSignature.update(signature.getBytes("UTF-8"));
bytes = privateSignature.sign();
signature = Base64.getEncoder().encodeToString(bytes);

我在POST模式下的https://www.googleapis.com/oauth2/v4/token发送请求,并带有以下标头:

"Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded"

并在体内:

"grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=" + header + "." + claim_set + "." + signature

我真的很困,不知道如何前进。

1 个答案:

答案 0 :(得分:1)

最后,我找到了答案!这是一个编码问题。实际上,当我对字符串进行编码时,我调用了Base64.getEncoder()编码器而不是Base64.getUrlEncoder()编码器。现在我们可以更改所有脚本。

这是标题计算:

String header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
byte[] bytes = header.getBytes();
header = Base64.getUrlEncoder().encodeToString(bytes);

我的声明集

long time_milis = System.currentTimeMillis()/1000L;
String claim_set = "{\n"
     + "  \"iss\":\"something@something.iam.gserviceaccount.com\",\n"
     + "  \"scope\":\"https://www.googleapis.com/auth/devstorage.read_write\",\n"
     + "  \"aud\":\"https://www.googleapis.com/oauth2/v4/token\",\n"
     + "  \"exp\":" + (time_milis + 300) + ",\n"
     + "  \"iat\":" + time_milis + "\n"
     + "}";
bytes = claim_set.getBytes();
claim_set = Base64.getUrlEncoder().encodeToString(bytes);

签名(出于明显的安全原因,我屏蔽了私钥):

String signature = header + "." + claim_set;
String privateKeyString = "-----BEGIN PRIVATE KEY-----\nturlututu123\n-----END PRIVATE KEY-----\n";
privateKeyString = private_key.replaceAll("-----END PRIVATE KEY-----", "").replaceAll("-----BEGIN PRIVATE KEY-----", "").replaceAll("\n", "");

Signature privateSignature = Signature.getInstance("SHA256withRSA");

KeyFactory keyFactory=KeyFactory.getInstance("RSA");

PrivateKey privateKey = keyFactory.generatePrivate(
    new PKCS8EncodedKeySpec(
        Base64.getDecoder().decode(privateKeyString)
    )
);

privateSignature.initSign(privateKey);

privateSignature.update(signature.getBytes("UTF-8"));
bytes = privateSignature.sign();
signature = Base64.getUrlEncoder().encodeToString(bytes);

感谢this post,在调试过程中对我有帮助。