【问题标题】:Verification of ID token using FusionAuth- JWT library, getting PEMDecoderException: "Unexpected PEM Format"使用 FusionAuth-JWT 库验证 ID 令牌,得到 PEMDecoderException: "Unexpected PEM Format"
【发布时间】:2023-03-12 00:52:01
【问题描述】:

我正在使用 fusionauth-jwt 库来验证由 RSA SHA 256 密钥签名的 ID 令牌。 在我下面的代码中,首先尝试使用 JWKS json 获取公钥,然后尝试创建一个 Verifier 实例以便我可以验证我的 id 令牌。

List<JSONWebKey> keys = JSONWebKeySetHelper.retrieveKeysFromJWKS("http://localhost:9011/.well-known/jwks.json");

        Map<String, Verifier> publicKeyVerifiers = new HashMap<String,Verifier>();
        for (JSONWebKey key : keys) {
            String publicKey = key.x5c.get(0); //getting x5c element
            Verifier verifier = RSAVerifier.newVerifier(publicKey); // Creating RSA verifier instance where getting issue
            String kid = key.kid;
            publicKeyVerifiers.put(kid, verifier);
        }

JWT jwtDecoded = JWT.getDecoder().decode(idToken, publicKeyVerifiers);

在创建验证程序实例时遇到问题,因为 x5c 元素包含 Base64Encoded 值,而不是以“-----BEGIN”开头的 .pem 格式值

类“io.fusionauth.pem.PEMDecoder.java”需要“-----BEGIN”,它在 key.x5c 中不存在,因此抛出异常“throw new PEMDecoderException(new InvalidParameterException(”Unexpected PEM格式"));

来自“io.fusionauth.pem.PEMDecoder.java”的部分代码

public PEM decode(String encodedKey) {
        Objects.requireNonNull(encodedKey);

        try {
            if (encodedKey.contains("-----BEGIN RSA PUBLIC KEY-----")) {
                return this.decode_PKCS_1_Public(encodedKey);
            } else if (encodedKey.contains("-----BEGIN PUBLIC KEY-----")) {
                return this.decode_X_509(encodedKey);
            } else if (encodedKey.contains("-----BEGIN CERTIFICATE-----")) {
                return new PEM(CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(this.getKeyBytes(encodedKey, "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----"))));
            } else if (encodedKey.contains("-----BEGIN RSA PRIVATE KEY-----")) {
                return this.decode_PKCS_1_Private(encodedKey);
            } else if (encodedKey.contains("-----BEGIN PRIVATE KEY-----")) {
                return this.decode_PKCS_8(encodedKey);
            } else if (encodedKey.contains("-----END EC PRIVATE KEY-----")) {
                return this.decode_EC_privateKey(encodedKey);
            } else {
                throw new PEMDecoderException(new InvalidParameterException("Unexpected PEM Format"));
            }
        } catch (InvalidKeyException | InvalidKeySpecException | IOException | NoSuchAlgorithmException | CertificateException var3) {
            throw new PEMDecoderException(var3);
        }
    }

【问题讨论】:

    标签: authentication oauth-2.0 jwt authorization fusionauth


    【解决方案1】:

    您应该能够获取x5c JWK 属性的第一个元素,然后简单地在其周围添加 PEM 信封。

    -----BEGIN CERTIFICATE-----
    ...x5c[0]
    -----END CERTIFICATE-----
    

    这将生成有效的 X.509 编码 PEM 证书。只需确保在-----BEGIN CERTIFICATE----------END CERTIFICATE----- 之前添加新行。

    这应该足够了。如果不是这样,下一步就是将x5c[0] 每 64 个八位字节用换行符拆分一次。

    -----BEGIN CERTIFICATE-----
    ...x5c[0]{0,64}
    ...x5c[0]{64,128}
    ...etc
    -----END CERTIFICATE-----
    

    如果失败,x5c[0] 不会按照最初指定的方式进行编码。

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 1970-01-01
      • 2015-06-27
      • 2017-06-25
      • 2017-01-06
      • 2017-07-31
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多