certificate-parser.js 1014 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const {
  3. calculatePartialFingerprintOfEscapedPemCertificate,
  4. } = require('../../modules/certificate');
  5. module.exports = async (req, res, next) => {
  6. // this header from nginx contains the CN from the client certificate
  7. if (
  8. req.headers['ssl_client_s_dn_cn'] &&
  9. req.headers['ssl_client_s_dn_cn'] !== 'no_client_cert'
  10. ) {
  11. req.clientCertificateCn = req.headers['ssl_client_s_dn_cn'];
  12. } else {
  13. const err = new Error('Unauthorized');
  14. err.status = 403;
  15. return next(err);
  16. }
  17. // this header from nginx contains the URI-encoded PEM-encoded X.509 client certificate
  18. if (req.headers['ssl_client_certificate']) {
  19. const pemCertificateEscaped = req.headers['ssl_client_certificate'];
  20. const fingerprint = calculatePartialFingerprintOfEscapedPemCertificate(
  21. pemCertificateEscaped,
  22. );
  23. req.clientCertificateFingerprint = fingerprint;
  24. } else {
  25. const err = new Error('Unauthorized');
  26. err.status = 403;
  27. return next(err);
  28. }
  29. return next();
  30. };