【发布时间】:2021-09-07 09:36:55
【问题描述】:
我一直在互联网上尝试各种解决方案,但所有解决方案都以各种方式出现故障。我写信是希望 StackOverflow 上熟悉类似工作流程的人或 Microsoft.IdentityModel.JsonWebTokens 团队的人可以介入以提供帮助。
我要做的是解密由 JSONWebKey (JWK) 加密的 OAuth 访问令牌,以便我可以读取声明数据。现在我被困在解密步骤上。我在 OS X 上使用 C# 和 Visual Studio for Mac。我尝试过的方法包括使用较旧的 JWT 库并尝试通过迂回方式创建各种 RSA 对象。但是,我想做的是如下所示:
using System;
using System.Linq;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
using Microsoft.AspNetCore.WebUtilities;
namespace jwtdecoder {
class Program {
static void Main(string[] args) {
/* Exact string of a token retrieved from the IDP/OAuth server. */
String token = "BASE64EncodedTokenGoesHere";
/* Trust me that we have a valid JWKS string here that has all the properties mentioned in the RSA params below
* and uses RSA OAEP 256 */
String jwks = "JWKSSTRINGGOESHERE";
/* Get our basic objects from the Strings above. */
JsonWebKeySet exampleJWKS = new JsonWebKeySet(jwks);
JsonWebKey exampleJWK = exampleJWKS.Keys.First();
JsonWebToken exampleJWT = new JsonWebToken(token);
/* Create RSA from Elements in JWK */
RSAParameters rsap = new RSAParameters{
Modulus = WebEncoders.Base64UrlDecode(exampleJWK.N),
Exponent = WebEncoders.Base64UrlDecode(exampleJWK.E),
D = WebEncoders.Base64UrlDecode(exampleJWK.D),
P = WebEncoders.Base64UrlDecode(exampleJWK.P),
Q = WebEncoders.Base64UrlDecode(exampleJWK.Q),
DP = WebEncoders.Base64UrlDecode(exampleJWK.DP),
DQ = WebEncoders.Base64UrlDecode(exampleJWK.DQ),
InverseQ = WebEncoders.Base64UrlDecode(exampleJWK.QI)
};
RSA rsa = RSA.Create();
rsa.ImportParameters(rsap);
RsaSecurityKey rsakey = new RsaSecurityKey(rsa);
/* Create a JSON Token Handler and Try Decrypting the Token */
JsonWebTokenHandler exampleHandler = new JsonWebTokenHandler();
TokenValidationParameters validationParameters = new TokenValidationParameters {
ValidateAudience = false,
ValidateIssuer = false,
RequireSignedTokens = false, /* Have also tried with this set to True */
TokenDecryptionKey = rsakey
};
String clearToken = exampleHandler.DecryptToken(exampleJWT, validationParameters);
/* The line above results in an error
* "IDX10609: Decryption failed. No Keys tried: token: 'System.String'."
*/
}
}
}
我的 IDP/OAuth 服务器是 Micro Focus / NetIQ Access Manager,使用我在其外部生成的 JSON Web 密钥(并作为新的资源服务器导入)。但是,我认为细节与问题没有太大关系。当我在 C# 中创建 JSONWebKey 和 JSONWebToken 对象时,我看到了我希望看到的属性,并且没有任何异常。我还可以使用非 C# 工具解码(而不是解密)令牌字符串,并查看我期望的标头属性等。
具体来说,对于上面代码中的exampleJWT对象,header如下:
{{ "alg": "RSA-OAEP-256", "enc": "A128CBC-HS256", "typ": "JWT", "cty": "JWT", "zip": "DEF", "kid": "5BbVY7F77gz9LWE4tUjXwNFt9qhINvWBR7Pkm1ZJlEA" }}
exampleJWT 对象还具有以下属性,其值看起来要么已编码要么已加密:EncodedHeader、EncodedToken、EncryptedKey、CipherText 和 AuthenticationTag 目前该对象中没有明文索赔数据或日期数据。
我的想法是我没有为 Decrypt 方法正确设置我的代码以正确应用 JSONWebToken RSA 信息,以便可以将令牌转换为明文。然而,正如我所说,我使用其他 C# 类和方法尝试了许多不同的方法,但没有一个更成功。我很想知道我对这个过程的根本误解。提前谢谢你。
在下面回答 Michal 的问题:四个点(base-64 编码时),所以五个部分。
【问题讨论】:
-
从这里的描述听起来你想要解码令牌并验证签名,而不是解密它,但也许我没有看到任何东西。快速检查一下 - 您的 JWT 是由点分隔的三个部分还是五个部分?
-
是的,现在更清楚的是,您确实在解密令牌。你试过调试吗?在代码中的某处放置断点并检查库中发生的情况。令牌是否正确传递? (收到
System.string的消息有点奇怪,还是正常的?)