UNPKG

@pagopa/io-spid-commons

Version:

Common code for integrating SPID authentication

36 lines 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.middlewareCatchAsInternalError = exports.getAuthnContextFromResponse = void 0; const xmldom_1 = require("@xmldom/xmldom"); const responses_1 = require("@pagopa/ts-commons/lib/responses"); const function_1 = require("fp-ts/lib/function"); const O = require("fp-ts/lib/Option"); const saml_1 = require("./saml"); /** * Extract AuthnContextClassRef from SAML response. * * ie. for <saml2:AuthnContextClassRef>https://www.spid.gov.it/SpidL2</saml2:AuthnContextClassRef> * returns "https://www.spid.gov.it/SpidL2" */ const getAuthnContextFromResponse = (xml) => (0, function_1.pipe)(O.fromNullable(xml), O.chain((xmlStr) => O.tryCatch(() => new xmldom_1.DOMParser().parseFromString(xmlStr))), O.chain((xmlResponse) => xmlResponse ? O.some(xmlResponse.getElementsByTagNameNS(saml_1.SAML_NAMESPACE.ASSERTION, "AuthnContextClassRef")) : O.none), O.chain((responseAuthLevelEl) => { var _a; return ((_a = responseAuthLevelEl === null || responseAuthLevelEl === void 0 ? void 0 : responseAuthLevelEl[0]) === null || _a === void 0 ? void 0 : _a.textContent) ? O.some(responseAuthLevelEl[0].textContent.trim()) : O.none; })); exports.getAuthnContextFromResponse = getAuthnContextFromResponse; const middlewareCatchAsInternalError = (f, message = "Exception while calling express middleware") => (req, res, next) => { try { f(req, res, next); } catch (_) { // Send a ResponseErrorInternal only if a response was not already sent to the client if (!res.headersSent) { return (0, responses_1.ResponseErrorInternal)(`${message} [${_}]`).apply(res); } } }; exports.middlewareCatchAsInternalError = middlewareCatchAsInternalError; //# sourceMappingURL=response.js.map