certificate-parser.js 852 B

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