我正在为我的应用程序设置 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
我已cert.pem
在 azure 门户中上传到我的应用注册。
在我的应用程序中,我使用Nimbus JOSE + JWT库来构建 JWT 和带有 OpenID Connect 扩展的 Nimbus OAuth 2.0 SDK来管理 OpenID 流。以下是每个包的 Javadoc 页面:
- https://www.javadoc.io/doc/com.nimbusds/nimbus-jose-jwt/7.8
- https://www.javadoc.io/doc/com.nimbusds/oauth2-oidc-sdk/6.14
我通过检查它们是否包含-----BEGIN PRIVATE KEY-----
and-----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 门户上注册应用程序的配置可能有误。
任何有关如何缩小范围或解决此问题的指示将不胜感激!