mocksaml/pages/api/saml/metadata/download.ts

32 lines
1001 B
TypeScript
Raw Normal View History

import config from 'lib/env';
2022-02-17 06:57:50 +00:00
import type { NextApiRequest, NextApiResponse } from 'next';
import stream from 'stream';
import { IdPMetadata } from 'types';
2022-02-17 06:57:50 +00:00
import { promisify } from 'util';
import { createIdPMetadataXML, stripCertHeaderAndFooter } from 'utils';
2022-02-17 06:57:50 +00:00
const pipeline = promisify(stream.pipeline);
2022-02-22 05:36:06 +00:00
export default async function handler(req: NextApiRequest, res: NextApiResponse<IdPMetadata | string>) {
2022-02-17 06:57:50 +00:00
switch (req.method) {
case 'GET':
return await downloadMetadata();
default:
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
2022-02-18 04:07:27 +00:00
// Download metadata
2022-02-17 06:57:50 +00:00
async function downloadMetadata() {
const xml = await createIdPMetadataXML({
idpEntityId: config.entityId,
2022-02-18 04:07:27 +00:00
idpSsoUrl: config.ssoUrl,
certificate: stripCertHeaderAndFooter(config.publicKey),
2022-02-17 06:57:50 +00:00
});
res.setHeader('Content-type', 'text/xml');
2022-02-18 04:07:27 +00:00
res.setHeader('Content-Disposition', 'attachment; filename=mock-saml-metadata.xml');
2022-02-17 06:57:50 +00:00
await pipeline(xml, res);
}
2022-02-22 05:36:06 +00:00
}