Handle POST binding
This commit is contained in:
parent
0f06186989
commit
cc68eca2f8
@ -4,24 +4,36 @@ import { extractSAMLRequestAttributes } from 'utils';
|
|||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<string>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<string>) {
|
||||||
switch (req.method) {
|
switch (req.method) {
|
||||||
case 'GET':
|
case 'GET':
|
||||||
return await processSAMLRequest();
|
return await processSAMLRequest(req, res, false);
|
||||||
|
case 'POST':
|
||||||
|
return await processSAMLRequest(req, res, true);
|
||||||
default:
|
default:
|
||||||
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function processSAMLRequest() {
|
async function processSAMLRequest(req: NextApiRequest, res: NextApiResponse, isPost: boolean) {
|
||||||
const relayState = <string>req.query.RelayState;
|
let samlRequest, relayState, isDeflated;
|
||||||
const samlRequest = <string>req.query.SAMLRequest;
|
if (isPost) {
|
||||||
|
relayState = req.body.RelayState;
|
||||||
|
samlRequest = req.body.SAMLRequest;
|
||||||
|
isDeflated = false;
|
||||||
|
} else {
|
||||||
|
relayState = req.query.RelayState;
|
||||||
|
samlRequest = req.query.SAMLRequest;
|
||||||
|
isDeflated = true;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const { id, audience, acsUrl, providerName } = await extractSAMLRequestAttributes(
|
||||||
|
samlRequest,
|
||||||
|
isDeflated
|
||||||
|
);
|
||||||
|
const params = new URLSearchParams({ id, audience, acsUrl, providerName, relayState });
|
||||||
|
|
||||||
try {
|
res.redirect(302, `/saml/login?${params.toString()}`);
|
||||||
const { id, audience, acsUrl, providerName } = await extractSAMLRequestAttributes(samlRequest);
|
} catch (err) {
|
||||||
const params = new URLSearchParams({ id, audience, acsUrl, providerName, relayState });
|
console.error(err);
|
||||||
|
|
||||||
res.redirect(302, `/saml/login?${params.toString()}`);
|
res.status(500).send(`Error parsing SAML request`);
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
|
|
||||||
res.status(500).send(`Error parsing SAML request`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,8 +18,10 @@ const parseXML = (xml: string): Promise<Record<string, any>> => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Parse SAMLRequest attributes
|
// Parse SAMLRequest attributes
|
||||||
const extractSAMLRequestAttributes = async (samlRequest: string) => {
|
const extractSAMLRequestAttributes = async (samlRequest: string, isDeflated: boolean) => {
|
||||||
const request = (await inflateRawAsync(Buffer.from(samlRequest, 'base64'))).toString();
|
const request = isDeflated
|
||||||
|
? (await inflateRawAsync(Buffer.from(samlRequest, 'base64'))).toString()
|
||||||
|
: Buffer.from(samlRequest, 'base64').toString();
|
||||||
const result = await parseXML(request);
|
const result = await parseXML(request);
|
||||||
|
|
||||||
const attributes = result['samlp:AuthnRequest']['$'];
|
const attributes = result['samlp:AuthnRequest']['$'];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user