【问题标题】:FeathersJS authentication using client certificate使用客户端证书进行 FeathersJS 身份验证
【发布时间】:2019-01-14 21:57:40
【问题描述】:

我正在尝试创建自己的身份验证策略,在 FeathersJS 后端读取客户端的 PKI 证书。这是在 before 钩子中处理的,并且基于文档钩子是

钩子是独立于传输的,这意味着它是否通过 HTTP(S) (REST)、Socket.io、Primus 或 Feathers 将来可能支持的任何其他传输调用都无关紧要。它们也与服务无关,这意味着它们可以与任何服务一起使用,无论它们是否有模型。

这不是一个坏主意,但是我需要挂钩中的 TLS 套接字结构来获取用户的证书。本质上调用:req.socket.getPeerCertificate()。我正在使用passport-client-certificate 模块,这是有问题的策略:

class ClientCertStrategy extends Strategy {
  constructor (options, verify) {
    if (typeof options === 'function') {
      verify = options
      options = {}
    }
    if (!verify) throw new Error('Client cert authentication strategy requires a verify function')

    super()

    this.name = 'client-cert'
    this._verify = verify
    this._passReqToCallback = options.passReqToCallback
  }

  _verified (err, user) {
    if (err) { return this.error(err) }
    if (!user) { return this.fail() }
    this.success(user)
  }

  authenticate (req, options) {
    // Requests must be authorized
    // (i.e. the certificate must be signed by at least one trusted CA)
    if (!req.socket.authorized) {
      this.fail()
      return
    }

    // This is where it fails! req.socket does not exist
    const clientCert = req.socket.getPeerCertificate()

    if (!clientCert) {
      this.fail()
      // TODO: Failure message
      // this.fail({message: options.badRequestMessage || 'Missing client     certificate'}, 400)
      return
    }

    try {
      if (this._passReqToCallback) {
        this._verify(req, clientCert, this._verified.bind(this))
      } else {
        this._verify(clientCert, this._verified.bind(this))
      }
    } catch (err) {
      return this.error(err)
    }
  }
}

基于 FeathersJS 代码,authenticate 函数基本上从hook 生成一个新的请求对象。有什么方法可以更早地获取用户的证书并在稍后执行钩子时使其可用?

【问题讨论】:

    标签: node.js feathersjs feathers-authentication


    【解决方案1】:

    我写了一个问题并被指向最终帮助我解决这个问题的常见问题解答:

    https://github.com/feathersjs/authentication/issues/693

    https://docs.feathersjs.com/faq/readme.html#how-do-i-access-the-request-object-in-hooks-or-services

    我最终编写了一个将证书插入请求params 的中间件。请求params 被复制到钩子中,然后传递到 Passport 策略中。

    【讨论】:

      猜你喜欢
      • 2012-01-10
      • 2015-10-29
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      相关资源
      最近更新 更多