找不到 node-oidc-provider 授权代码

node-oidc-provider authorization code not found

所以我正在尝试使用 panda 的 node-oidc-provider 库,使用打字稿来实现 openID Connect 服务器。

我在尝试将 authorization_code 换成授权令牌时遇到问题。基本上它说找不到代码,虽然我可以在 mongo 数据库中看到该文档,并在库使用的适配器的查找函数中放置一个 console.log 来检索代码,但确实如此被发现。

我使用这个 url 获得授权:

http://localhost:3000/auth?client_id=test&response_type=code&scope=openid

然后我将通过邮递员发送此请求:

POST http://localhost:3000/token

REQ HEADERS
Content-Type:application/x-www-form-urlencoded

REQ BODY
grant_type:authorization_code
code:rekLjZKPFPk0pPSBJKlgqFt0tPtCq1k03ktS8CUh_X6
client_id:test
client_secret:testsecret
redirect_uri:https://lvh:8080/cb

响应如下:

{
    "error": "invalid_grant",
    "error_description": "grant request is invalid"
}

深入研究库,我在第 38 行的 if 检查之前在文件 lib/actions/grants/authorization_code.js 中放置了一个 console.log。在那里我可以看到 ctx.oidc.params.code 变量已正确设置auth_code,但随后从 const code = await ctx.oidc.provider.AuthorizationCode.find 行检索到的代码未定义。

这是我的 oidc-config: oidc-config

这是 mongodb 适配器:mongodb-adapter

鉴于我在调试代码中输入的控制台日志,这是我从邮递员发送令牌请求时得到的输出:

Received code: rekLjZKPFPk0pPSBJKlgqFt0tPtCq1k03ktS8CUh_X6
MongoAdapter#find rekLjZKPFPk0pPSBJKlgqFt0tPtCq1k03ktS8CUh_X6
MongoAdapter#coll authorization_code oidc.authorization_code
MongoAdapter#find result {
  iat: 1570099703,
  exp: 1570105703,
  accountId: 5d937633b00ba1073edaa689,
  authTime: 1570099703,
  claims: { rejected: [] },
  grantId: 'JEMh2GsZaEjlgeGx9MXT0',
  redirectUri: 'https://lvh:8080/cb',
  scope: 'openid',
  sessionUid: 'QA3vhV_8-Jlgc8583_aGZ',
  kind: 'AuthorizationCode',
  jti: 'rekLjZKPFPk0pPSBJKlgqFt0tPtCq1k03ktS8CUh_X6',
  clientId: 'test',
  expiresWithSession: true
}
MongoAdapter#coll session oidc.session
Found code: undefined
InvalidGrant authorization code not found

我很确定错误是我的,而不是库中的错误。但我似乎无法理解我做错了什么。

更新

进入调试模式,我看到它实际上在 lib/models/mixins/is_session_bound.js 的第 26 行文件: >assert.equal(token.accountId, session.accountId(), 'token and session principal are now different');

但我还是不知道那是什么意思。

所以基本上问题是我传递了从 mongoose 查询中得出的帐户 ID。但是由于默认情况下文档的 id 是一个对象,断言检查将失败(即使内容相同)。然后我所做的是编辑 User 模型上的虚拟方法,在末尾添加一个 toString()。

schema.virtual('id').get(function (this: { _id: string }) {
    return this._id.toString();
});

问题就解决了。