2022-02-22 05:35:42 +00:00
|
|
|
import xml2js from "xml2js";
|
|
|
|
|
import { promisify } from "util";
|
|
|
|
|
import { inflateRaw } from "zlib";
|
2022-02-21 14:31:47 +00:00
|
|
|
|
2022-02-22 05:35:42 +00:00
|
|
|
const inflateRawAsync = promisify(inflateRaw);
|
2022-02-21 14:31:47 +00:00
|
|
|
|
|
|
|
|
// Parse XML
|
|
|
|
|
const parseXML = (xml: string): Promise<Record<string, any>> => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
xml2js.parseString(xml, (err: Error, result: any) => {
|
2022-02-22 05:36:06 +00:00
|
|
|
if (err) {
|
2022-02-21 14:31:47 +00:00
|
|
|
reject(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolve(result);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Parse SAMLRequest attributes
|
|
|
|
|
const extractSAMLRequestAttributes = async (samlRequest: string) => {
|
2022-02-22 05:35:42 +00:00
|
|
|
const request = (
|
|
|
|
|
await inflateRawAsync(Buffer.from(samlRequest, "base64"))
|
|
|
|
|
).toString();
|
|
|
|
|
const result = await parseXML(request);
|
2022-02-21 14:31:47 +00:00
|
|
|
|
2022-02-22 05:35:42 +00:00
|
|
|
const attributes = result["samlp:AuthnRequest"]["$"];
|
|
|
|
|
const issuer = result["samlp:AuthnRequest"]["saml:Issuer"];
|
2022-02-21 14:31:47 +00:00
|
|
|
return {
|
2022-02-22 05:35:42 +00:00
|
|
|
id: attributes.ID,
|
|
|
|
|
acsUrl: attributes.AssertionConsumerServiceURL,
|
|
|
|
|
providerName: attributes.ProviderName,
|
|
|
|
|
audience: issuer[0]["_"],
|
2022-02-21 14:31:47 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-22 05:36:06 +00:00
|
|
|
export { extractSAMLRequestAttributes };
|