15

我正在尝试为我的组织实施 OpenID Connect 规范。我在测试依赖方应用程序中使用 Microsoft 的 OpenID Connect OWIN 实现来验证我的协议实现。

我公开了以下元数据文档:

{
  "issuer": "https://acs.contoso.com/",
  "authorization_endpoint": "http://localhost:53615/oauth2/auth",
  "token_endpoint": "http://localhost:53615/oauth2/token",
  "userinfo_endpoint": "http://localhost:53615/connect/userinfo",
  "jwks_uri": "http://localhost:53615/connect/keys",
  "ui_locales_supported": [
    "en-GB"
  ]
}

签名密钥作为本文档公开:

{
  "keys": [
    {
      "n": "xpXxl3M-YkZlzQJdArO1TfOGT2no-UL4dbZ7WuSCNIsSfyGDaqUXjMMHNyq9yD3vp-NCyk8kmn7d5XqHufnceXJM8q4xTrhN3lvywdBSbR-dwXsA-B-MJVgfiK0d_z-mxP9ew2Hj9-KkWbWCzsswlWp3gZ4mB4RGutB1IRSzXVIbvZ-MtKUb6XUDU4LDb_c1xCEXWZxhR-o1a1dLfObH2hHJ-w5y6odGlKtOFx4i4h0u7-Oj5R6k5b2YXEHM0IuYeN0u0sQvrTecokntGzPrvhnKy69I7Z_az5rC5kgloh25D9lTbe4vcRU7FXlYCFYDZsT0_IkGIXRi7brOS4f1ow",
      "e": "AQAB",
      "kty": "RSA",
      "use": "sig",
      "alg": "RS256",
      "kid": "F8A59280B3D13777CC7541B3218480984F421450"
    }
  ]
}

身份令牌是使用JwtSecurityToken类及其关联的处理程序生成的,使用X509SigningCredentials类。此代码代表令牌是如何构造的,并作为响应数据的参数返回给调用系统。

var credentials = new X509SigningCredentials(cert); // My certificate.
var issuedTime = DateTime.UtcNow;
var expiresTime = issuedTime.AddMinutes(5);
var epoch = new DateTime(1970, 01, 01, 0, 0, 0);

var claims = new[]
{
    new Claim("sub", Guid.NewGuid().ToString()),
    new Claim("iat" Math.Floor((issuedTime - epoch).TotalSeconds).ToString()),
    new Claim("nonce", nonce), // Value from client
}

var token = new JwtSecurityToken(
    "https://acs.contoso.com",
    client_id, // Value from client
    claims,
    new Lifetime(issuedTime, expiresTime),
    credentials);

var handler = new JwtSecurityTokenHandler();
parameters.Add("id_token", handler.WriteToken(token)); // Outgoing parameters.

当我尝试将签名的令牌传递回依赖方应用程序时,OWIN 中间件接受 POST 并尝试验证令牌的签名。这样做会引发以下异常:

SecurityTokenSignatureKeyNotFoundException:IDX10500:签名验证失败。无法解析 SecurityKeyIdentifier: 'SecurityKeyIdentifier (IsReadOnly = False, Count = 1, Clause[0] = X509ThumbprintKeyIdentifierClause(Hash = 0xF8A59280B3D13777CC7541B3218480984F421450))', 令牌: '{"typ":"JWT","alg":"RS25 "x5t":"-KWSgLPRN3fMdUGzIYSAmE9CFFA"}.{"iss":" https://test.accesscontrol.net/","aud":"test","nbf":1404917162,"exp":1404917462,"sub":"60eb55ec-0699-4068-bfa6-41666fc2b2e9","iat":"1404917162"} RawData: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LV1NnTFBSTjNmTWRVR3pJWVNBbUU5Q0ZGQSJ9. eyJpc3MiOiJodHRwczovL2Fjcy5zdXJlY2xvdWQuY29tLyIsImF1ZCI6InRlc3QiLCJuYmYiOjE0MDQ5MTcxNjIsImV4cCI6MTQwNDkxNzQ2Miwic3ViIjoiNjBlYjU1ZWMtMDY5OS00MDY4LWJmYTYtNDE2NjZmYzJiMmU5IiwiaWF0IjoiMTQwNDkxNzE2MiJ9.xkP0RwlX3CYfU0KhFsVvLJC94WK22DTqNTm71cfjiJ8VUHv3b2YhDqfq70N8mQEyiR8vTR6OQqnO6UqXqX4RXUs6ZkfK9Liv3n9NhCs97wJhP2jfefJYeScYtRmWcNNWSSL7vkm2JXQfwKOQTnOGp-ba04TtI6jVrjhOQXH43eCJ9vNuBUzdD-t8CAdmnbvH0nWpIB8kWbw5v8Sa0aQuxMjJYbLC_2Iw3X13dqnyVjp4fA7eSB8N7c1it0KEB-VKfUqiGD3VecyEZGGZbaGE8rvVet5QrY1lJ3V4yM8j6-xDc5Yndc4swOun0L3D6TYk-8gdVXUJDRjbv1ZuhZltsw'.

该组件仍然是预发布的,所以这可能是实现中的一个缺陷,但是我想假设这是我的错误,直到所有可能性都被排除。

有什么我正在做的事情显然是错误的,还是我应该做些什么来准确理解为什么签名无法验证?

4

2 回答 2

7

问题位于此处的异常消息中:

子句[0] = X509ThumbprintKeyIdentifierClause(哈希 = 0xF8A59280B3D13777CC7541B3218480984F421450)

令牌使用 X.509 证书的默认密钥标识符子句进行签名:其指纹。元数据仅公开 RSA 参数和名称标识符。当客户端检索元数据时,它使用此信息设置 RSA 密钥,而不是 X.509 指纹。

要更正此错误,必须更改签名凭据以包含正确的名称标识符:

var credentials = new X509CertificateCredentials(
    cert,
    new SecurityKeyIdentifier(
        new NamedKeySecurityKeyIdentifierClause(
            "kid",
            "F8A59280B3D13777CC7541B3218480984F421450")));

这包括签名中的预期标识符,并且签名验证成功。

于 2014-07-09T22:40:04.310 回答
0

我遇到过这个异常。在我的例子中,我们的应用程序正在缓存 Azure 环境中的签名密钥,因为它们不会经常更改,但没有刷新密钥的机制。由于签名密钥最终会轮换,我们收到了有效的 JWT,但有一个旧的签名密钥列表,在这种情况下,它们都无法验证 JWT 签名。

于 2021-01-14T21:07:21.563 回答