Skip to content

Commit

Permalink
Move xmlbuilder to xml module
Browse files Browse the repository at this point in the history
  • Loading branch information
forty committed Mar 22, 2021
1 parent 7538c56 commit 4aa7e07
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/passport-saml/saml.ts
Expand Up @@ -4,7 +4,6 @@ import * as zlib from "zlib";
import * as crypto from "crypto";
import * as url from "url";
import * as querystring from "querystring";
import * as xmlbuilder from "xmlbuilder";
import * as util from "util";
import { CacheProvider as InMemoryCacheProvider } from "./inmemory-cache-provider";
import * as algorithms from "./algorithms";
Expand Down Expand Up @@ -33,6 +32,7 @@ import {
import { assertRequired } from "./utility";
import {
buildXml2JsObject,
buildXmlBuilderObject,
decryptXml,
parseDomFromString,
parseXml2JsFromString,
Expand Down Expand Up @@ -352,7 +352,7 @@ class SAML {
request["samlp:AuthnRequest"]["samlp:Scoping"] = scoping;
}

let stringRequest = xmlbuilder.create((request as unknown) as Record<string, any>).end();
let stringRequest = buildXmlBuilderObject(request, false);
if (isHttpPostBinding && this.options.privateKey != null) {
stringRequest = signAuthnRequestPost(stringRequest, this.options);
}
Expand Down Expand Up @@ -398,7 +398,7 @@ class SAML {
}

await this.cacheProvider.saveAsync(id, instant);
return xmlbuilder.create((request as unknown) as Record<string, any>).end();
return buildXmlBuilderObject(request, false);
}

generateLogoutResponse(req: Request, logoutRequest: Profile) {
Expand All @@ -425,7 +425,7 @@ class SAML {
},
};

return xmlbuilder.create(request).end();
return buildXmlBuilderObject(request, false);
}

async requestToUrlAsync(
Expand Down Expand Up @@ -1370,9 +1370,7 @@ class SAML {
"@Binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST",
"@Location": this.getCallbackUrl({}),
};
return xmlbuilder
.create((metadata as unknown) as Record<string, any>)
.end({ pretty: true, indent: " ", newline: "\n" });
return buildXmlBuilderObject(metadata, true);
}

keyToPEM(key: string | Buffer): typeof key extends string | Buffer ? string | Buffer : Error {
Expand Down
6 changes: 6 additions & 0 deletions src/passport-saml/xml.ts
Expand Up @@ -3,6 +3,7 @@ import * as xmlCrypto from "xml-crypto";
import * as xmlenc from "xml-encryption";
import * as xmldom from "xmldom";
import * as xml2js from "xml2js";
import * as xmlbuilder from "xmlbuilder";

type SelectedValue = string | number | boolean | Node;

Expand Down Expand Up @@ -113,3 +114,8 @@ export const buildXml2JsObject = (rootName: string, xml: any): string => {
};
return new xml2js.Builder(builderOpts).buildObject(xml);
};

export const buildXmlBuilderObject = (xml: Record<string, any>, pretty: boolean): string => {
const options = pretty ? { pretty: true, indent: " ", newline: "\n" } : {};
return xmlbuilder.create(xml).end(options);
};

0 comments on commit 4aa7e07

Please sign in to comment.