mocksaml/utils/request.ts
2022-02-23 19:18:20 +05:30

36 lines
979 B
TypeScript

import { promisify } from 'util';
import xml2js from 'xml2js';
import { inflateRaw } from 'zlib';
const inflateRawAsync = promisify(inflateRaw);
// Parse XML
const parseXML = (xml: string): Promise<Record<string, any>> => {
return new Promise((resolve, reject) => {
xml2js.parseString(xml, (err: Error, result: any) => {
if (err) {
reject(err);
}
resolve(result);
});
});
};
// Parse SAMLRequest attributes
const extractSAMLRequestAttributes = async (samlRequest: string) => {
const request = (await inflateRawAsync(Buffer.from(samlRequest, 'base64'))).toString();
const result = await parseXML(request);
const attributes = result['samlp:AuthnRequest']['$'];
const issuer = result['samlp:AuthnRequest']['saml:Issuer'];
return {
id: attributes.ID,
acsUrl: attributes.AssertionConsumerServiceURL,
providerName: attributes.ProviderName,
audience: issuer[0]['_'],
};
};
export { extractSAMLRequestAttributes };