mocksaml/pages/api/saml/metadata/download.ts
Kiran K 9bc55ea7f0
Validate AuthnRequest signature (#11)
* Validate AuthnRequest signature skelton

* Code refactor: Move the base64decode to common method

* wip

* Add signature validation

* Read the keys from config

* Lock dep version

Co-authored-by: Deepak Prabhakara <deepak@boxyhq.com>
2022-03-02 21:06:04 +00:00

32 lines
1001 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, stripCertHeaderAndFooter } from 'utils';
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: stripCertHeaderAndFooter(config.publicKey),
});
res.setHeader('Content-type', 'text/xml');
res.setHeader('Content-Disposition', 'attachment; filename=mock-saml-metadata.xml');
await pipeline(xml, res);
}
}