【问题标题】:NodeJS - Can't attach Client Certificate for Testing with Mocha/Chai-HttpNodeJS - 无法附加客户端证书以使用 Mocha/Chai-Http 进行测试
【发布时间】:2019-06-19 15:07:30
【问题描述】:

我正在检查我的登录端点上的客户端证书。使用 Postman 等进行测试一切正常,但我无法使用我的 mocha/chai-http 单元测试运行它。

const ca = fs.readFileSync(path.join(__dirname, '..', process.env.SERVER_CERT_FILE));
const cert = fs.readFileSync(path.join(__dirname, '..', process.env.CLIENT_CERT_FILE));
const key = fs.readFileSync(path.join(__dirname, '..', process.env.CLIENT_KEY_FILE));

describe('POST /login', () => {
        it('validates the certificate', (done) => {
            chai.request(app)
                .post('/api/login')
                .ca(ca)
                .key(key)
                .cert(cert)
                .send(userData)
                .end((err: Error, res: any) => {
                    /* checks */
                    done();
                });
        });

我什至无法以这种方式获得req.socket.getPeerCertificate()。有人知道这会如何工作吗?

我的登录控制器方法:

login = (req: any, res: any, next: any) => {
    if (!req.client.authorized) {
        sendJSONResponse(res, 'Missing or invalid client certificate', 401);
        return;
    }
    /* .... */
 }

我的服务器选项

const options = {
        key: fs.readFileSync(process.env.SERVER_KEY_FILE),
        cert: fs.readFileSync(process.env.SERVER_CERT_FILE),
        requestCert: true,
        rejectUnauthorized: false,
        ca: [
            fs.readFileSync(process.env.SERVER_CERT_FILE)
        ]
    };
    https.createServer(options, this.app).listen(port, () => {
        /*...*/;
    });

【问题讨论】:

    标签: node.js authentication certificate mocha.js chai-http


    【解决方案1】:

    您需要在 https 侦听器对象上使用 module.exports。我有同样的确切问题,并通过进行以下更改来解决。希望这会有所帮助。

    module.exports = https.createServer(options, app)
      .listen(443, function () {
        console.log('HTTPS App listening on port 443! Go to https://localhost:443/')
      })

    【讨论】:

    • 嗨@user11477381,你能解释一下你的解决方案是做什么的吗?
    猜你喜欢
    • 2019-02-15
    • 2017-02-18
    • 2017-03-19
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多