2022-02-22 08:18:07 +00:00
|
|
|
import Head from 'next/head';
|
|
|
|
|
import { useRouter } from 'next/router';
|
2022-03-02 21:02:13 +00:00
|
|
|
import type { FormEvent } from 'react';
|
|
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2022-02-18 18:12:45 +00:00
|
|
|
|
|
|
|
|
export default function Login() {
|
2022-02-22 08:18:07 +00:00
|
|
|
const router = useRouter();
|
|
|
|
|
const { id, audience, acsUrl, providerName, relayState } = router.query;
|
2022-03-02 21:02:13 +00:00
|
|
|
|
|
|
|
|
const [state, setState] = useState({
|
|
|
|
|
username: 'jackson',
|
|
|
|
|
domain: 'example.com',
|
|
|
|
|
});
|
2022-02-24 06:20:00 +00:00
|
|
|
|
|
|
|
|
// Set focus to email input on load
|
|
|
|
|
const emailInp = useRef<HTMLInputElement>(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (emailInp.current) {
|
|
|
|
|
emailInp.current.focus();
|
|
|
|
|
emailInp.current.select();
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
2022-02-22 08:18:07 +00:00
|
|
|
|
2022-03-02 21:02:13 +00:00
|
|
|
const handleChange = (e: FormEvent<HTMLInputElement | HTMLSelectElement>): void => {
|
|
|
|
|
const { name, value } = e.currentTarget;
|
|
|
|
|
|
|
|
|
|
setState({
|
|
|
|
|
...state,
|
|
|
|
|
[name]: value,
|
|
|
|
|
});
|
2022-02-22 08:18:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
|
|
|
|
|
e.preventDefault();
|
2022-03-02 21:02:13 +00:00
|
|
|
|
|
|
|
|
const { username, domain } = state;
|
|
|
|
|
|
2022-02-22 08:18:07 +00:00
|
|
|
const response = await fetch(`/api/saml/auth`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2022-03-02 21:02:13 +00:00
|
|
|
body: JSON.stringify({
|
|
|
|
|
email: `${username}@${domain}`,
|
|
|
|
|
id,
|
|
|
|
|
audience,
|
|
|
|
|
acsUrl,
|
|
|
|
|
providerName,
|
|
|
|
|
relayState,
|
|
|
|
|
}),
|
2022-02-22 08:18:07 +00:00
|
|
|
});
|
2022-03-02 21:02:13 +00:00
|
|
|
|
2022-02-22 08:18:07 +00:00
|
|
|
if (response.ok) {
|
2022-02-22 08:33:22 +00:00
|
|
|
const newHtml = await response.text();
|
|
|
|
|
const newDoc = document.open('text/html', 'replace');
|
2022-03-02 21:02:13 +00:00
|
|
|
|
2022-02-22 08:33:22 +00:00
|
|
|
newDoc.write(newHtml);
|
|
|
|
|
newDoc.close();
|
2022-02-22 08:18:07 +00:00
|
|
|
} else {
|
|
|
|
|
document.write('Error in getting SAML response');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-18 18:12:45 +00:00
|
|
|
return (
|
2022-02-22 08:18:07 +00:00
|
|
|
<div className='h-full'>
|
2022-02-18 18:12:45 +00:00
|
|
|
<Head>
|
2022-03-02 21:16:28 +00:00
|
|
|
<title>Mock SAML Identity Provider - Login</title>
|
2022-02-18 18:12:45 +00:00
|
|
|
</Head>
|
2022-03-02 21:02:13 +00:00
|
|
|
<div className='relative top-20 mx-auto w-[465px] max-w-[90%] rounded-md border p-10 text-[#145698]'>
|
2022-03-01 12:35:42 +00:00
|
|
|
<h2 className='mb-3 text-center text-3xl font-bold'>Login</h2>
|
2022-02-22 08:18:07 +00:00
|
|
|
<form onSubmit={handleSubmit}>
|
2022-03-02 21:02:13 +00:00
|
|
|
<div className='flex items-end gap-x-1'>
|
|
|
|
|
<div>
|
|
|
|
|
<label htmlFor='username' className='mb-2 block'>
|
|
|
|
|
Email
|
|
|
|
|
</label>
|
|
|
|
|
<input
|
|
|
|
|
name='username'
|
|
|
|
|
id='username'
|
|
|
|
|
ref={emailInp}
|
|
|
|
|
autoComplete='off'
|
|
|
|
|
type='text'
|
|
|
|
|
placeholder='jackson'
|
|
|
|
|
value={state.username}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
className='input'
|
|
|
|
|
title='please provide a mock example.com email address'
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<select
|
|
|
|
|
name='domain'
|
|
|
|
|
id='domain'
|
|
|
|
|
className='select w-full'
|
2022-02-22 08:18:07 +00:00
|
|
|
onChange={handleChange}
|
2022-03-02 21:02:13 +00:00
|
|
|
value={state.domain}>
|
|
|
|
|
<option value='example.com'>@example.com</option>
|
|
|
|
|
<option value='example.org'>@example.org</option>
|
|
|
|
|
</select>
|
2022-02-18 18:12:45 +00:00
|
|
|
</div>
|
2022-02-22 08:18:07 +00:00
|
|
|
<div className='mt-5'>
|
2022-03-01 12:35:42 +00:00
|
|
|
<label htmlFor='password' className='mb-2 block'>
|
2022-02-18 18:12:45 +00:00
|
|
|
Password <sup>(Prefilled for you)</sup>
|
|
|
|
|
</label>
|
|
|
|
|
<input
|
2022-02-22 08:18:07 +00:00
|
|
|
id='password'
|
2022-02-18 18:12:45 +00:00
|
|
|
readOnly={true}
|
2022-02-22 08:18:07 +00:00
|
|
|
autoComplete='off'
|
|
|
|
|
type='password'
|
|
|
|
|
defaultValue='samlstrongpassword'
|
2022-03-01 12:35:42 +00:00
|
|
|
className='input w-full'
|
2022-02-18 18:12:45 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
2022-03-01 12:35:42 +00:00
|
|
|
<button type='submit' className='button mt-8 w-full'>
|
2022-02-18 18:12:45 +00:00
|
|
|
Sign In
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
2022-03-02 21:16:28 +00:00
|
|
|
<div className='relative top-20 mx-auto w-[800px] max-w-[90%] rounded-md p-10 text-[#145698]'>
|
|
|
|
|
This is a simulated login screen, feel free to pick any username but you are restricted to two domains
|
|
|
|
|
example.com and example.org. But this should allow you to test all combinations of your authentication
|
|
|
|
|
and user modelling.
|
|
|
|
|
</div>
|
2022-02-18 18:12:45 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|