* Use boxyhq/saml20 * use sign from saml20 * cleaned up GetKeyInfo * cleaned up getPublicKeyPemFromCertificate * cleaned up node-forge * use hasValidSignature from saml20 * cleanup and update saml20 to the beta version * throw an error if signature is not valid * updated saml20
33 lines
1015 B
TypeScript
33 lines
1015 B
TypeScript
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<IdPMetadata | string>) {
|
|
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);
|
|
}
|
|
}
|