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

32 lines
1007 B
TypeScript
Raw Normal View History

2022-02-17 06:57:50 +00:00
import type { NextApiRequest, NextApiResponse } from 'next';
2022-02-21 14:31:47 +00:00
import { fetchPublicKey, createIdPMetadataXML } from '../../../../utils';
2022-02-17 06:57:50 +00:00
import { IdPMetadata } from '../../../../types';
import stream from 'stream';
import { promisify } from 'util';
2022-02-22 05:36:06 +00:00
import config from '../../../../lib/env';
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,
2022-02-21 14:31:47 +00:00
certificate: await fetchPublicKey(),
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
}