diff --git a/pages/api/saml/auth.ts b/pages/api/saml/auth.ts index 1b6d80b..2472b5e 100644 --- a/pages/api/saml/auth.ts +++ b/pages/api/saml/auth.ts @@ -1,6 +1,6 @@ import { createHash } from 'crypto'; -import type { NextApiRequest, NextApiResponse } from 'next'; import config from 'lib/env'; +import type { NextApiRequest, NextApiResponse } from 'next'; import type { User } from 'types'; import { createResponseForm, @@ -14,9 +14,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) if (req.method === 'POST') { const email = req.body.email; - if (!email.endsWith('@example.com')) { + if (!email.endsWith('@example.com') && !email.endsWith('@example.org')) { res.status(403).send(`${email} denied access`); } + const id = createHash('sha256').update(email).digest('hex'); const user: User = { diff --git a/pages/saml/login.tsx b/pages/saml/login.tsx index 9d54ae4..0f271a1 100644 --- a/pages/saml/login.tsx +++ b/pages/saml/login.tsx @@ -1,12 +1,16 @@ import Head from 'next/head'; -import { useEffect, useRef, useState } from 'react'; -import type { FormEvent } from 'react'; import { useRouter } from 'next/router'; +import type { FormEvent } from 'react'; +import { useEffect, useRef, useState } from 'react'; export default function Login() { const router = useRouter(); const { id, audience, acsUrl, providerName, relayState } = router.query; - const [email, setEmail] = useState('jackson'); + + const [state, setState] = useState({ + username: 'jackson', + domain: 'example.com', + }); // Set focus to email input on load const emailInp = useRef(null); @@ -17,22 +21,39 @@ export default function Login() { } }, []); - const handleChange = (e: FormEvent): void => { - setEmail(e.currentTarget.value); + const handleChange = (e: FormEvent): void => { + const { name, value } = e.currentTarget; + + setState({ + ...state, + [name]: value, + }); }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); + + const { username, domain } = state; + const response = await fetch(`/api/saml/auth`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify({ email: `${email}@example.com`, id, audience, acsUrl, providerName, relayState }), + body: JSON.stringify({ + email: `${username}@${domain}`, + id, + audience, + acsUrl, + providerName, + relayState, + }), }); + if (response.ok) { const newHtml = await response.text(); const newDoc = document.open('text/html', 'replace'); + newDoc.write(newHtml); newDoc.close(); } else { @@ -45,25 +66,36 @@ export default function Login() { Mock SAML IdP - Login -
+

Login

-
- - +
+ + +
+