2

我正在尝试使用 node.js 中的 jsonwebtoken 验证 jose4j 生成的 json Web 令牌,我看到以下错误:

[错误:PEM_read_bio_PUBKEY 失败]

jose4j 代码基本上是直接从示例中提取的:

RsaJsonWebKey key = RsaJwkGenerator.generateJwk(2048);
key.setKeyId("global.authenticated");

byte[] raw = key.getKey().getEncoded();
Base64.Encoder encoder = Base64.getEncoder();
System.out.printf("Public Key [%s]\n", encoder.encodeToString(raw));

JwtClaims claims = new JwtClaims();
claims.setIssuer("global.gen");
claims.setAudience("global.cons");
claims.setExpirationTimeMinutesInTheFuture(12 * 60);
claims.setGeneratedJwtId();
claims.setIssuedAtToNow();
claims.setNotBeforeMinutesInThePast(2);
claims.setSubject("nim");
claims.setClaim("role", "tester");

JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKey(key.getPrivateKey());
jws.setKeyIdHeaderValue(key.getKeyId());
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
String token = jws.getCompactSerialization();
System.out.printf("Generated Token [%s]\n", token);


JwtConsumer jwtConsumer = new JwtConsumerBuilder()
    .setRequireExpirationTime() // the JWT must have an expiration time
    .setAllowedClockSkewInSeconds(30) // allow some leeway in validating time based claims to account for clock skew
    .setRequireSubject() // the JWT must have a subject claim
    .setExpectedIssuer("global.gen") // whom the JWT needs to have been issued by
    .setExpectedAudience("global.cons") // to whom the JWT is intended for
    .setVerificationKey(key.getKey()) // verify the signature with the public key
    .build(); // create the JwtConsumer instance

try {
  //  Validate the JWT and process it to the Claims
  JwtClaims jwtClaims = jwtConsumer.processToClaims(token);
  System.out.println("JWT validation succeeded! " + jwtClaims);
} catch (InvalidJwtException e) {
  // InvalidJwtException will be thrown, if the JWT failed processing or validation in anyway.
  // Hopefully with meaningful explanations(s) about what went wrong.
  System.out.println("Invalid JWT! " + e);
}

所以在内部,令牌验证得很好。但是,当我复制令牌和密钥时(例如以下来自运行),会报告上述错误:

var jwt        = require('jsonwebtoken'); // used to create, sign,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if (err)
    console.log(err);
  else
    console.log(decoded);
});

是否有一些我缺少的魔法(在 jose4j 或 jsonwebtoken 中)允许生成的令牌通过公钥进行验证?

附带说明一下,将令牌粘贴到 jwt.io 可以正确解码标头和有效负载,但是无法使用相同的公钥验证签名。我猜问题确实出在 jose4j 方面——但不确定。

4

2 回答 2

6

如果有人遇到类似问题,我会将此添加为答案。问题源于传递给 jsonwebtoken 的密钥格式。它不能只是一个纯文本公钥,它必须专门遵循 PEM 格式。因此,一旦我将密钥转换为 PEM 文件,特别是通过进行以下更改:

byte[] raw = key.getKey().getEncoded();
Base64.Encoder encoder = Base64.getMimeEncoder(64, new byte[]{'\n'});

然后用

-----BEGIN PUBLIC KEY-----
<Key>
-----END PUBLIC KEY-----

然后将生成的文件传递到 jsonwebtoken 允许身份验证继续进行。我没有意识到传递到验证步骤的证书应该如此严格地格式化(即使用行大小和包装器!)

注意:bouncycastle 包有一个PemObject类和一个PemWriter应该使编写文件更容易一些的类 - 但是我不想仅仅为此拉入另一个包。也许 jose4j 的维护者可以在他们的包中添加一个小类。

于 2015-09-24T06:42:32.227 回答
3

似乎jsonwebtoken不仅对-----BEGIN PUBLIC KEY-----页眉和页脚非常严格,而且对换行符也非常严格。

像一把钥匙

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkRWAQ0O9LgBoHNAB5m1X8e1sPTzKmBTPCFTSRTzw0AjZozIbN4nIp/3jnQHTbcY0Bf5MDWmtdheSK1a+ew34YcgN2b9Shr+3yZv9PJ97i7gRCqOnI7jbm7PXFBNw1I4aMYc6tV7TKFzvx6008/nYvN3Jey6Z8ItS/FLQRDkV9m/WQkhJpYgvmD6qiwj9d+un+moBQ5/PPgn7Qkg5GyxZUy9PsblUDSrIA0bEiv/wQOXCYUvL9OFzxTUSeIHpdGibhPQVxX3Jnpr293Iq/mOKn3ZO+xBID26m3L8+ik64wte041y1S4HHaE9Q082ai/uBduAwIHcJY5VAHborZYCSaQIDAQAB

必须看起来像这样才能使用jsonwebtoken(注意页眉、页脚和换行符):

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkRWAQ0O9LgBoHNAB5m1X
8e1sPTzKmBTPCFTSRTzw0AjZozIbN4nIp/3jnQHTbcY0Bf5MDWmtdheSK1a+ew34
YcgN2b9Shr+3yZv9PJ97i7gRCqOnI7jbm7PXFBNw1I4aMYc6tV7TKFzvx6008/nY
vN3Jey6Z8ItS/FLQRDkV9m/WQkhJpYgvmD6qiwj9d+un+moBQ5/PPgn7Qkg5GyxZ
Uy9PsblUDSrIA0bEiv/wQOXCYUvL9OFzxTUSeIHpdGibhPQVxX3Jnpr293Iq/mOK
n3ZO+xBID26m3L8+ik64wte041y1S4HHaE9Q082ai/uBduAwIHcJY5VAHborZYCS
aQIDAQAB
-----END PUBLIC KEY-----

这个功能为我完成了这项工作:

function base64toPem(base64)
{
    for(var result="", lines=0;result.length-lines < base64.length;lines++) {
        result+=base64.substr(result.length-lines,64)+"\n"
    }

    return "-----BEGIN PUBLIC KEY-----\n" + result + "-----END PUBLIC KEY-----";
}
于 2016-02-15T14:42:01.980 回答