在OpenID流中提交令牌请求时,如何解决“无效签名” API错误?

问题描述 投票:0回答:2

我正在为我的应用程序设置OpenID流,并希望通过Microsoft Active Directory测试私钥JWT客户端证书身份验证。也就是说,在请求ID和访问令牌时,我想使用证书而不是客户端机密来验证我的应用程序。但是,在发出令牌请求时,我收到以下错误:

{
   "error":"invalid_client",
   "error_description":"AADSTS700027: Client assertion contains an invalid signature. [Reason - The key was not found., Please visit 'https://developer.microsoft.com/en-us/graph/graph-explorer' and query for 'https://graph.microsoft.com/beta/applications/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' to see configured keys]\r\nTrace ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\r\nCorrelation ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\r\nTimestamp: 2019-09-26 22:24:19Z",
   "error_codes":[
      700027
   ],
   "timestamp":"2019-09-26 22:24:19Z",
   "trace_id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
   "correlation_id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
   "error_uri":"https://login.microsoftonline.com/error?code=700027"
}

我正在使用以下命令生成私钥和证书:

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes

我已经在Azure门户中将cert.pem上传到我的应用程序注册中。

在我的应用程序中,我正在使用Nimbus JOSE + JWT库来构造JWT,并使用Nimbus OAuth 2.0 SDK with OpenID Connect extensions来管理OpenID流。以下分别是每个软件包的Javadoc页面:

我已通过检查密钥和证书是否包含-----BEGIN PRIVATE KEY----------BEGIN CERTIFICATE-----标头以及相应的页脚来验证其为PEM格式。

根据错误,我访问了https://developer.microsoft.com/en-us/graph/graph-explorer,在左侧登录,然后使用给定的未编辑URL发送查询。这样做给了我错误:

{
"error": {
    "code": "Request_ResourceNotFound",
    "message": "Resource 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' does not exist or one of its queried reference-property objects are not present.",
    "innerError": {
        "request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "date": "2019-09-26T23:47:37"
    }
}

我当前的实现如下。

        val privateKeyString = File(keyFilePath).readText()
        val certificateString = File(certFilePath).readText()
        val certObject = JWK.parseFromPEMEncodedX509Cert(certificateString)
        val privateKeyJWK = JWK.parseFromPEMEncodedObjects(privateKeyString)
        val privateKey = RSAKey.parse(privateKeyJWK.toJSONObject())
        val privateKeyJWT = PrivateKeyJWT(
                ClientID(configuration.clientId), // clientId retrieved from the app reg on the azure portal
                providerMetadata.tokenEndpointURI, // login.microsoftonline.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/oauth2/token
                JWSAlgorithm.RS256,
                privateKey.toRSAPrivateKey(),
                certObject.keyID,
                null)

        val tokenReq = TokenRequest(
                providerMetadata.tokenEndpointURI,
                privateKeyJWT,
                AuthorizationCodeGrant(authCode, // authCode received from previous step of the OpenID flow
                        URI(configuration.redirectURI)) // the application's login page. This has been registered in
                                                        // the app reg on the azure portal.
        )

        val tokenHTTPResponse: HTTPResponse? = tokenReq.toHTTPRequest().send()
        val tokenResponse = OIDCTokenResponse.parse(tokenHTTPResponse) // response fails with the described error

这里有几个步骤可能会出错,但是我无法缩小范围:

  • 我的密钥生成可能是错误的。也许我使用的密钥和证书不是预期的格式?
  • 我使用Nimbus库解析密钥可能是错误的。在请求期间单步执行代码并检查数据对象似乎表明它正在解析文件中的所有组件。目前尚不清楚它是否正确解析了这些组件。
  • 我对私钥JWT的构造可能有缺陷。
  • 令牌请求的构造可能有缺陷。
  • 我在Azure门户上的应用程序注册的配置可能是错误的。

关于如何缩小范围或解决此问题的任何指示,将不胜感激!

oauth-2.0 active-directory openid nimbus-jose-jwt
2个回答
0
投票

为什么不起作用的简单答案是Azure Active Directory不支持私钥JWT身份验证。编写的代码有效,但仅适用于支持私钥JWT身份验证的服务。


0
投票

有一种更简单的方法可以从Nimbus JOSE + JWT库中生成JWK:

https://connect2id.com/products/nimbus-jose-jwt/examples/jwk-generation#rsa

您的OAuth 2.0客户端是否在令牌端点注册了private_key_jwt身份验证?

© www.soinside.com 2019 - 2024. All rights reserved.