Skip to content

Commit 91b6d72

Browse files
robcresswellmarkstos
authored andcommitted
fix: disable esmoduleInterop setting
This patch disables the `esmoduleInterop` setting that causes type issues when the library is used by applications that do not transform their code in this way. Note the workaround for the `strategy.ts` file (import = require()) is not ideal, but the export in that file is not a valid ES export, so using this TS workaround to get past it. fixes: #482
1 parent 7b71596 commit 91b6d72

File tree

7 files changed

+34
-35
lines changed

7 files changed

+34
-35
lines changed

src/passport-saml/algorithms.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import crypto from 'crypto';
1+
import * as crypto from 'crypto';
22

33
export function getSigningAlgorithm (shortName: string): string {
44
switch(shortName) {

src/passport-saml/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CacheItem, CacheProvider} from './inmemory-cache-provider';
22
import { SAML } from './saml';
3-
import Strategy from './strategy';
3+
import Strategy = require('./strategy');
44
import type { Profile, VerifiedCallback, VerifyWithRequest, VerifyWithoutRequest } from './types';
55

66
export { SAML, Strategy, CacheItem, CacheProvider, Profile, VerifiedCallback, VerifyWithRequest, VerifyWithoutRequest };

src/passport-saml/multiSamlStrategy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import util from 'util';
1+
import * as util from 'util';
22
import * as saml from './saml';
33
import {CacheProvider as InMemoryCacheProvider} from './inmemory-cache-provider';
4-
import SamlStrategy from './strategy';
4+
import SamlStrategy = require('./strategy');
55

66
function MultiSamlStrategy (options, verify) {
77
if (!options || typeof options.getSamlOptions != 'function') {

src/passport-saml/saml.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import Debug from 'debug';
22
const debug = Debug('passport-saml');
3-
import zlib from 'zlib';
4-
import xml2js from 'xml2js';
5-
import xmlCrypto, { xpath } from 'xml-crypto';
6-
import crypto, { KeyLike } from 'crypto';
7-
import xmldom from 'xmldom';
8-
import url from 'url';
9-
import querystring from 'querystring';
10-
import xmlbuilder from 'xmlbuilder';
11-
import xmlenc from 'xml-encryption';
12-
import util, { promisify } from 'util';
3+
import * as zlib from 'zlib';
4+
import * as xml2js from 'xml2js';
5+
import * as xmlCrypto from 'xml-crypto';
6+
import * as crypto from 'crypto';
7+
import * as xmldom from 'xmldom';
8+
import * as url from 'url';
9+
import * as querystring from 'querystring';
10+
import * as xmlbuilder from 'xmlbuilder';
11+
import * as xmlenc from 'xml-encryption';
12+
import * as util from 'util';
1313
import {CacheProvider as InMemoryCacheProvider} from './inmemory-cache-provider';
1414
import * as algorithms from './algorithms';
1515
import { signAuthnRequestPost } from './saml-post-signing';
@@ -69,7 +69,7 @@ function processValidlySignedSamlLogout(self: SAML, doc, dom, callback) {
6969
}
7070

7171
function callBackWithNameID(nameid, callback) {
72-
const format = xpath(nameid, "@Format") as Node[];
72+
const format = xmlCrypto.xpath(nameid, "@Format") as Node[];
7373
return callback(null, {
7474
value: nameid.textContent,
7575
format: format && format[0] && format[0].nodeValue
@@ -241,7 +241,7 @@ class SAML {
241241

242242
(async () => {
243243
if(this.options.validateInResponseTo) {
244-
return promisify(this.cacheProvider.save).bind(this.cacheProvider)(id, instant);
244+
return util.promisify(this.cacheProvider.save).bind(this.cacheProvider)(id, instant);
245245
} else {
246246
return;
247247
}
@@ -619,7 +619,7 @@ class SAML {
619619
"namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#' and " +
620620
"descendant::*[local-name(.)='Reference' and @URI='#"+currentNode.getAttribute('ID')+"']" +
621621
"]";
622-
const signatures = xpath(currentNode, xpathSigQuery);
622+
const signatures = xmlCrypto.xpath(currentNode, xpathSigQuery);
623623
// This function is expecting to validate exactly one signature, so if we find more or fewer
624624
// than that, reject.
625625
if (signatures.length != 1) {
@@ -653,7 +653,7 @@ class SAML {
653653
return false;
654654
// If we find any extra referenced nodes, reject. (xml-crypto only verifies one digest, so
655655
// multiple candidate references is bad news)
656-
const totalReferencedNodes = xpath(currentNode.ownerDocument,
656+
const totalReferencedNodes = xmlCrypto.xpath(currentNode.ownerDocument,
657657
"//*[@" + idAttribute + "='" + refId + "']");
658658

659659
if (totalReferencedNodes.length > 1) {
@@ -673,7 +673,7 @@ class SAML {
673673
if (!Object.prototype.hasOwnProperty.call(doc, 'documentElement'))
674674
throw new Error('SAMLResponse is not valid base64-encoded XML');
675675

676-
inResponseTo = xpath(doc, "/*[local-name()='Response']/@InResponseTo");
676+
inResponseTo = xmlCrypto.xpath(doc, "/*[local-name()='Response']/@InResponseTo");
677677

678678
if (inResponseTo) {
679679
inResponseTo = inResponseTo.length ? inResponseTo[0].nodeValue : null;
@@ -689,8 +689,8 @@ class SAML {
689689
validSignature = true;
690690
}
691691

692-
const assertions = xpath(doc, "/*[local-name()='Response']/*[local-name()='Assertion']");
693-
const encryptedAssertions = xpath(doc,
692+
const assertions = xmlCrypto.xpath(doc, "/*[local-name()='Response']/*[local-name()='Assertion']");
693+
const encryptedAssertions = xmlCrypto.xpath(doc,
694694
"/*[local-name()='Response']/*[local-name()='EncryptedAssertion']");
695695

696696
if (assertions.length + encryptedAssertions.length > 1) {
@@ -718,7 +718,7 @@ class SAML {
718718
return util.promisify(xmlenc.decrypt).bind(xmlenc)(encryptedAssertionXml, xmlencOptions)
719719
.then(decryptedXml => {
720720
const decryptedDoc = new xmldom.DOMParser().parseFromString(decryptedXml);
721-
const decryptedAssertions = xpath(decryptedDoc, "/*[local-name()='Assertion']");
721+
const decryptedAssertions = xmlCrypto.xpath(decryptedDoc, "/*[local-name()='Assertion']");
722722
if (decryptedAssertions.length != 1)
723723
throw new Error('Invalid EncryptedAssertion content');
724724

@@ -1194,8 +1194,8 @@ class SAML {
11941194
}
11951195

11961196
getNameID(self, doc, callback) {
1197-
const nameIds = xpath(doc, "/*[local-name()='LogoutRequest']/*[local-name()='NameID']");
1198-
const encryptedIds = xpath(doc,
1197+
const nameIds = xmlCrypto.xpath(doc, "/*[local-name()='LogoutRequest']/*[local-name()='NameID']");
1198+
const encryptedIds = xmlCrypto.xpath(doc,
11991199
"/*[local-name()='LogoutRequest']/*[local-name()='EncryptedID']") as Node[];
12001200

12011201
if (nameIds.length + encryptedIds.length > 1) {
@@ -1209,7 +1209,7 @@ class SAML {
12091209
return callback(new Error('No decryption key for encrypted SAML response'));
12101210
}
12111211

1212-
const encryptedDatas = xpath(encryptedIds[0], "./*[local-name()='EncryptedData']");
1212+
const encryptedDatas = xmlCrypto.xpath(encryptedIds[0], "./*[local-name()='EncryptedData']");
12131213

12141214
if (encryptedDatas.length !== 1) {
12151215
return callback(new Error('Invalid LogoutRequest'));
@@ -1220,7 +1220,7 @@ class SAML {
12201220
return util.promisify(xmlenc.decrypt).bind(xmlenc)(encryptedDataXml, xmlencOptions)
12211221
.then(function (decryptedXml) {
12221222
const decryptedDoc = new xmldom.DOMParser().parseFromString(decryptedXml);
1223-
const decryptedIds = xpath(decryptedDoc, "/*[local-name()='NameID']");
1223+
const decryptedIds = xmlCrypto.xpath(decryptedDoc, "/*[local-name()='NameID']");
12241224
if (decryptedIds.length !== 1) {
12251225
return callback(new Error('Invalid EncryptedAssertion content'));
12261226
}
@@ -1325,7 +1325,7 @@ class SAML {
13251325
return xmlbuilder.create(metadata).end({ pretty: true, indent: ' ', newline: '\n' });
13261326
}
13271327

1328-
keyToPEM(key: KeyLike) {
1328+
keyToPEM(key: crypto.KeyLike) {
13291329
if (!key || typeof key !== 'string') return key;
13301330

13311331
const lines = key.split('\n');

src/passport-saml/strategy.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import passport from 'passport-strategy';
2-
import util from 'util';
1+
import * as passport from 'passport-strategy';
2+
import * as util from 'util';
33
import * as saml from './saml';
4-
import url from 'url';
4+
import * as url from 'url';
55
import { AuthenticateOptions, AuthorizeOptions, SamlConfig, VerifyWithoutRequest, VerifyWithRequest } from './types';
66
import type { Request } from 'express';
77
import { Profile } from './types';

src/passport-saml/types.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type express from 'express';
2-
import passport from 'passport';
1+
import type * as express from 'express';
2+
import * as passport from 'passport';
33
import type { CacheProvider } from './inmemory-cache-provider';
44

55
export type CertCallback = (callback: (err: Error | null, cert?: string | string[]) => void) => void;
@@ -73,10 +73,9 @@ export type Profile = {
7373
} & {
7474
[attributeName: string]: unknown; // arbitrary `AttributeValue`s
7575
};
76-
76+
7777
export type VerifiedCallback = (err: Error | null, user?: Record<string, unknown>, info?: Record<string, unknown>) => void;
7878

7979
export type VerifyWithRequest = (req: express.Request, profile: Profile, done: VerifiedCallback) => void;
8080

8181
export type VerifyWithoutRequest = (profile: Profile, done: VerifiedCallback) => void;
82-

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
// "typeRoots": [], /* List of folders to include type definitions from. */
4949
// "types": [], /* Type declaration files to be included in compilation. */
5050
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
51-
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
51+
// "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5252
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
5353
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5454

0 commit comments

Comments
 (0)