mocksaml/pages/api/apps/metadata.ts

61 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-01-13 19:20:01 +00:00
import type { NextApiRequest, NextApiResponse } from 'next';
2022-01-14 19:55:28 +00:00
import { createCertificate, createIdPMetadataXML } from '../../../utils';
2022-02-17 06:05:50 +00:00
import { IdPMetadata } from '../../../types';
const idpEntityId = 'http://saml.example.com';
const baseUrl = 'http://localhost:4000'; // TODO: Read from .env
// https://boxyhqdemo.onelogin.com/trust/saml2/http-post/sso/a810f17d-48a8-4ac2-ae0f-253c823b272c
// https://dev-8924093.okta.com/app/dev-8924093_jacksondemo_1/exk3u9pl6jx4P9AE15d7/sso/saml
// https://accounts.google.com/o/saml2/idp?idpid=C02frd9s1
2022-01-13 19:20:01 +00:00
export default async function handler(
req: NextApiRequest,
2022-02-17 06:05:50 +00:00
res: NextApiResponse<IdPMetadata>
2022-01-13 19:20:01 +00:00
) {
2022-02-17 06:05:50 +00:00
switch (req.method) {
case 'GET':
return await getMetadata();
case 'POST':
return await downloadMetadata();
default:
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
// Get metadata for an app
async function getMetadata() {
//const {id} = req.query;
const appId = '0480c44e-f200-4f72-8af0-a5a57611fd2d';
const metadata = {
certificate: await createCertificate(),
fingerprint: '',
sso_url: `${baseUrl}/saml2/app/${appId}`,
entity_id: idpEntityId,
}
return res.json(metadata);
2022-01-13 19:20:01 +00:00
}
2022-02-17 06:05:50 +00:00
// Download metadata for an app
async function downloadMetadata() {
const appId = '0480c44e-f200-4f72-8af0-a5a57611fd2d';
2022-01-13 19:20:01 +00:00
2022-01-14 19:55:28 +00:00
const certificate = await createCertificate();
const idpEntityId = 'http://localhost:4000/sso';
const idpSsoUrl = 'http://localhost:4000/sso';
2022-02-17 06:05:50 +00:00
2022-01-14 19:55:28 +00:00
const xml = await createIdPMetadataXML({
idpEntityId,
idpSsoUrl,
certificate,
});
2022-02-17 06:05:50 +00:00
2022-01-13 19:42:17 +00:00
res.setHeader('Content-type', 'text/xml');
res.setHeader('Content-Disposition', 'attachment; filename="metadata.xml"');
2022-02-17 06:05:50 +00:00
2022-01-13 19:42:17 +00:00
return res.send(xml);
2022-01-13 19:20:01 +00:00
}
}