diff --git a/pages/api/saml/metadata/download.ts b/pages/api/saml/metadata/download.ts deleted file mode 100644 index 3e575dd..0000000 --- a/pages/api/saml/metadata/download.ts +++ /dev/null @@ -1,32 +0,0 @@ -import config from 'lib/env'; -import type { NextApiRequest, NextApiResponse } from 'next'; -import stream from 'stream'; -import { IdPMetadata } from 'types'; -import { promisify } from 'util'; -import { createIdPMetadataXML } from 'utils'; -import saml from '@boxyhq/saml20'; - -const pipeline = promisify(stream.pipeline); - -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - switch (req.method) { - case 'GET': - return await downloadMetadata(); - default: - return res.status(405).end(`Method ${req.method} Not Allowed`); - } - - // Download metadata - async function downloadMetadata() { - const xml = await createIdPMetadataXML({ - idpEntityId: config.entityId, - idpSsoUrl: config.ssoUrl, - certificate: saml.stripCertHeaderAndFooter(config.publicKey), - }); - - res.setHeader('Content-type', 'text/xml'); - res.setHeader('Content-Disposition', 'attachment; filename=mock-saml-metadata.xml'); - - await pipeline(xml, res); - } -} diff --git a/pages/api/saml/metadata/index.ts b/pages/api/saml/metadata/index.ts index a17cc04..045b1c0 100644 --- a/pages/api/saml/metadata/index.ts +++ b/pages/api/saml/metadata/index.ts @@ -4,6 +4,10 @@ import saml from '@boxyhq/saml20'; import config from 'lib/env'; import type { IdPMetadata } from 'types'; import { createIdPMetadataXML } from 'utils'; +import stream from 'stream'; +import { promisify } from 'util'; + +const pipeline = promisify(stream.pipeline); export default async function handler(req: NextApiRequest, res: NextApiResponse) { switch (req.method) { @@ -15,6 +19,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< // Metadata URL async function MetadataUrl() { + const { download } = req.query as { download: any }; + const xml = await createIdPMetadataXML({ idpEntityId: config.entityId, idpSsoUrl: config.ssoUrl, @@ -22,6 +28,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< }); res.setHeader('Content-type', 'text/xml'); + + if (download || download === '') { + res.setHeader('Content-Disposition', 'attachment; filename=mock-saml-metadata.xml'); + + await pipeline(xml, res); + return; + } + res.send(xml); } } diff --git a/pages/index.tsx b/pages/index.tsx index 22a32c8..710c125 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -15,7 +15,7 @@ const Home: React.FC<{ metadata: IdPMetadata }> = ({ metadata }) => {
- +