2022-02-23 13:48:20 +00:00
|
|
|
import config from 'lib/env';
|
2022-02-22 08:17:41 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
|
import type { User } from 'types';
|
|
|
|
|
import {
|
|
|
|
|
createResponseForm,
|
|
|
|
|
createResponseXML,
|
|
|
|
|
fetchPrivateKey,
|
|
|
|
|
fetchPublicKey,
|
|
|
|
|
signResponseXML,
|
|
|
|
|
} from 'utils';
|
2022-02-22 05:36:06 +00:00
|
|
|
|
2022-02-22 08:17:41 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
if (req.method === 'POST') {
|
|
|
|
|
const email = req.body.email;
|
2022-02-23 13:48:20 +00:00
|
|
|
|
2022-02-22 08:17:41 +00:00
|
|
|
if (!email.endsWith('@example.com')) {
|
|
|
|
|
res.status(403).send(`${email} denied access`);
|
|
|
|
|
}
|
2022-02-23 13:48:20 +00:00
|
|
|
|
2022-02-22 08:17:41 +00:00
|
|
|
const id = email.replace('@example.com', '');
|
2022-02-23 13:48:20 +00:00
|
|
|
|
2022-02-22 08:17:41 +00:00
|
|
|
const user: User = {
|
|
|
|
|
id,
|
|
|
|
|
email,
|
|
|
|
|
firstName: id,
|
|
|
|
|
lastName: id,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const xml = await createResponseXML({
|
2022-02-22 10:20:01 +00:00
|
|
|
idpIdentityId: config.entityId,
|
2022-02-22 08:17:41 +00:00
|
|
|
audience: req.body.audience,
|
|
|
|
|
acsUrl: req.body.acsUrl,
|
2022-02-23 12:35:58 +00:00
|
|
|
samlReqId: req.body.id,
|
2022-02-22 08:17:41 +00:00
|
|
|
user: user,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const signingKey = await fetchPrivateKey();
|
|
|
|
|
const publicKey = await fetchPublicKey();
|
|
|
|
|
const xmlSigned = await signResponseXML(xml, signingKey, publicKey);
|
2022-02-22 10:20:01 +00:00
|
|
|
|
2022-02-22 08:17:41 +00:00
|
|
|
const encodedSamlResponse = Buffer.from(xmlSigned).toString('base64');
|
|
|
|
|
|
|
|
|
|
const html = createResponseForm(req.body.relayState, encodedSamlResponse, req.body.acsUrl);
|
|
|
|
|
|
|
|
|
|
res.send(html);
|
2022-02-22 05:36:06 +00:00
|
|
|
} else {
|
|
|
|
|
res.status(405).send(`Method ${req.method} Not Allowed`);
|
|
|
|
|
}
|
|
|
|
|
}
|