mocksaml/pages/api/saml/metadata.ts
Deepak Prabhakara 6fae8857b1
Add a namespace page to get unique entity ids for multi tenant use (#472)
* allow a unique entity id per org

* updated metadata url to support org

* org specific login

* org -> namespace

* updated node and alpine

* spacing tweak
2024-01-20 23:20:37 +00:00

45 lines
1.3 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from 'next';
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';
import { getEntityId } from 'lib/entity-id';
const pipeline = promisify(stream.pipeline);
export default async function handler(req: NextApiRequest, res: NextApiResponse<IdPMetadata | string>) {
switch (req.method) {
case 'GET':
return await MetadataUrl();
default:
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
// Metadata URL
async function MetadataUrl() {
const { download } = req.query as { download: any };
const filename = 'mock-saml-metadata' + (req.query.namespace ? `-${req.query.namespace}` : '') + '.xml';
const xml = await createIdPMetadataXML({
idpEntityId: getEntityId(config.entityId, req.query.namespace as any),
idpSsoUrl: config.ssoUrl,
certificate: saml.stripCertHeaderAndFooter(config.publicKey),
});
res.setHeader('Content-type', 'text/xml');
if (download || download === '') {
res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
await pipeline(xml, res);
return;
}
res.send(xml);
}
}