Harden backend auth, validation and error handling
- Rate limit login (5 attempts/min per IP) with a JSON 429 response - Constant-time password comparison; refuse login if no password is configured - Explicit SameSite=Strict auth cookie; log failed logins with client IP - Stop logging door codes (update log lines and the SMS body) - Replace per-action try/catch -> 400 with a global exception handler (JSON 500) - Return proper statuses: 404 for missing person, 502 for failed lock writes - Validate codes as ASCII digits only (int.TryParse accepted +/-/whitespace) - Validate person name and E.164 phone number; reject sending an unset code - Rename TwiloOptions -> TwilioOptions and fix message typos - Expose Program for integration tests
This commit is contained in:
1 parent
768bcc90e4
commit
d0425c8cb2
38 files changed
+189
-487
No files matched your search
@@ -0,0 +1,26 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue';
|
||||
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
const healthResult = ref<string>("Fetching health check...");
|
||||
|
||||
onMounted(async () => {
|
||||
// Send a request to the API Health Check endpoint
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Health`);
|
||||
|
||||
// Extract the response payload (string result)
|
||||
const result = await response.text();
|
||||
|
||||
// Set the result
|
||||
healthResult.value = result;
|
||||
})
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="text-slate-300">
|
||||
<h1>Health Check</h1>
|
||||
<p>{{ healthResult }}</p>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,67 @@
|
||||
<script setup lang='ts'>
|
||||
import { ref } from 'vue';
|
||||
import { type ApiResponse } from '~/types/api-response';
|
||||
|
||||
const password = ref('');
|
||||
const passwordError = ref('');
|
||||
const authenticated = useCookie<boolean>('authenticated');
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
|
||||
const handleLogin = async (event: Event) => {
|
||||
// Prevent default form submission
|
||||
event.preventDefault();
|
||||
|
||||
// Reset the error
|
||||
passwordError.value = '';
|
||||
|
||||
// Send a request to the api to login
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Authentication/login`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: `"${password.value}"`
|
||||
});
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<boolean>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
// If the response was successful, login and redirect to the home page
|
||||
authenticated.value = true;
|
||||
navigateTo('/');
|
||||
} else {
|
||||
// Otherwise, set the error
|
||||
passwordError.value = apiResponse.error ?? 'Unknown error';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="bg-zinc-800 text-slate-300 text-center w-full md:w-96 rounded-md">
|
||||
<h1 class="text-7xl mt-5">Yale</h1>
|
||||
<div>
|
||||
<h4 class="hr text-2xl mt-3">User Access</h4>
|
||||
</div>
|
||||
|
||||
<form @submit="handleLogin">
|
||||
<YaleFormInput type="password" v-model="password" placeholder="Password" class="block mt-10 w-5/6 mx-auto" />
|
||||
<p class="mt-3 w-5/6 mx-auto text-red-600" v-if="passwordError">{{ passwordError }}</p>
|
||||
<YaleButton type="submit" class="mb-8 mt-3 w-5/6 mx-auto ">Login</YaleButton>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style>
|
||||
.hr {
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.hr::after {
|
||||
content: '';
|
||||
display: block;
|
||||
border-top: 2px solid theme('colors.slate.300');
|
||||
margin-top: .1rem;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,132 @@
|
||||
<script setup lang="ts">
|
||||
import { type Person } from '@/types/yale';
|
||||
import { onMounted, ref } from 'vue';
|
||||
import type { ApiResponse } from '~/types/api-response';
|
||||
|
||||
const people = ref([] as Person[]);
|
||||
const person = ref({ id: 0, name: '', phoneNumber: '' } as Person);
|
||||
const loading = ref(true);
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
|
||||
const addModal = ref(false);
|
||||
|
||||
// When the component is mounted, load the people
|
||||
onMounted(async () => {
|
||||
await loadPeople();
|
||||
loading.value = false;
|
||||
});
|
||||
|
||||
const loadPeople = async () => {
|
||||
// Send a request to the api to get the people
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/People`, { credentials: 'include' });
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<Person[]>;
|
||||
|
||||
// Set the people
|
||||
people.value = apiResponse.data || [];
|
||||
}
|
||||
|
||||
const handleAddPerson = () => {
|
||||
addModal.value = true;
|
||||
}
|
||||
|
||||
const handleSavePerson = async () => {
|
||||
// Validate the person fields
|
||||
if (!person.value.name || !person.value.phoneNumber) {
|
||||
alert('Name and phone number are required');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the loading status
|
||||
loading.value = true;
|
||||
|
||||
// Send a request to the api to save the person
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/People`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(person.value)
|
||||
});
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<Person>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
// Add the new person to the list
|
||||
people.value.push(apiResponse.data!);
|
||||
addModal.value = false;
|
||||
} else {
|
||||
alert(apiResponse.error ?? 'An error occurred');
|
||||
}
|
||||
|
||||
// Close the modal and reset the person
|
||||
loading.value = false;
|
||||
person.value = { id: 0, name: '', phoneNumber: '' };
|
||||
|
||||
// Set the loading status
|
||||
loading.value = false;
|
||||
}
|
||||
|
||||
const handleDeletePerson = async (id: number) => {
|
||||
// Set the loading status
|
||||
loading.value = true;
|
||||
|
||||
// Send a request to the api to delete the person
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/People/${id}`, {
|
||||
method: 'DELETE',
|
||||
credentials: 'include'
|
||||
});
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<Person>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
// Remove the person from the list
|
||||
const index = people.value.findIndex(x => x.id === id);
|
||||
people.value.splice(index, 1);
|
||||
} else {
|
||||
alert(apiResponse.error ?? 'An error occurred');
|
||||
}
|
||||
|
||||
// Set the loading status
|
||||
loading.value = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<!-- Handle loading state -->
|
||||
<div v-if="loading">Loading...</div>
|
||||
<template v-else>
|
||||
<div class="text-end my-2">
|
||||
<YaleButton type="button" @click="handleAddPerson">Add Person</YaleButton>
|
||||
</div>
|
||||
<table class="w-full text-left">
|
||||
<tr>
|
||||
<th scope="col">Person ID</th>
|
||||
<th scope="col">Name</th>
|
||||
<th scope="col">Phone Number</th>
|
||||
<th scope="col">Actions</th>
|
||||
</tr>
|
||||
<PeopleListRow v-for="person in people" :key="person.id" :person="person" @delete-person="handleDeletePerson" />
|
||||
</table>
|
||||
</template>
|
||||
|
||||
<!-- Add modal -->
|
||||
<Modal v-if="addModal" @close="addModal = false">
|
||||
<template #title>
|
||||
Add Person
|
||||
</template>
|
||||
<template #default>
|
||||
<div class="flex flex-col space-y-2">
|
||||
<YaleFormInput v-model="person.name" type="text" placeholder="Name" class="block w-full" />
|
||||
<YaleFormInput v-model="person.phoneNumber" type="text" placeholder="Phone" class="block w-full" />
|
||||
<div class="flex justify-end">
|
||||
<YaleButton type="button" @click="handleSavePerson">Save</YaleButton>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</Modal>
|
||||
</template>
|
||||
@@ -0,0 +1,300 @@
|
||||
<script setup lang='ts'>
|
||||
import type { ApiResponse } from '@/types/api-response';
|
||||
import UserCodeListRow from './UserCodeListRow.vue';
|
||||
import { onMounted, ref } from 'vue';
|
||||
import { UserCodeStatus, type Person, type YaleUserCode } from '@/types/yale';
|
||||
|
||||
const userCodes = ref([] as YaleUserCode[]);
|
||||
const userCode = ref({} as YaleUserCode);
|
||||
const people = ref([] as { label: string, value: string }[]);
|
||||
const personNumber = ref('');
|
||||
const loading = ref(true);
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
|
||||
// Modals
|
||||
const showModal = ref(false);
|
||||
const editModal = ref(false);
|
||||
const clearModal = ref(false);
|
||||
const sendModal = ref(false);
|
||||
|
||||
// Form Fields
|
||||
const newCode = ref('');
|
||||
const newCodeError = ref('');
|
||||
|
||||
// When the component is mounted, load the user codes
|
||||
onMounted(async () => {
|
||||
// Load the user codes and set the loading status when complete
|
||||
await loadUserCodes();
|
||||
await loadPeople();
|
||||
loading.value = false;
|
||||
});
|
||||
|
||||
const loadUserCodes = async () => {
|
||||
// Send a request to the api to get the user codes
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Yale/codes`, { credentials: 'include' });
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<YaleUserCode[]>;
|
||||
|
||||
// Set the user codes by id
|
||||
userCodes.value = apiResponse.data?.sort((a, b) => a.id - b.id) || [];
|
||||
}
|
||||
|
||||
const loadPeople = async () => {
|
||||
// Send a request to the api to get the people
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/People`, { credentials: 'include' });
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<Person[]>;
|
||||
|
||||
// Set the people for drop down
|
||||
people.value = apiResponse.data?.map(x => ({ label: x.name, value: x.phoneNumber })) || [];
|
||||
}
|
||||
|
||||
const confirmUpdateCode = async (id: number, code: string) => {
|
||||
// Reset the error
|
||||
newCodeError.value = '';
|
||||
|
||||
// Validate the new code first
|
||||
if (!/^\d{4,6}$/.test(code)) {
|
||||
newCodeError.value = 'Code must be 4, 5 or 6 digits long';
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the loading status
|
||||
loading.value = true;
|
||||
|
||||
// Send a request to the api, the body will just be the new code
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Yale/code/${id}`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: `"${code}"`
|
||||
});
|
||||
|
||||
// Parse the result
|
||||
const apiResponse = await response.json() as ApiResponse<boolean>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
// If the response was successful update the code in the list
|
||||
const index = userCodes.value.findIndex(x => x.id === id);
|
||||
userCodes.value[index].code = code;
|
||||
userCodes.value[index].status = UserCodeStatus.ENABLED;
|
||||
} else {
|
||||
// Otherwise display an error to the user.
|
||||
alert(apiResponse.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
// Reset the new code
|
||||
newCode.value = '';
|
||||
|
||||
// Close the edit modal
|
||||
editModal.value = false;
|
||||
|
||||
// Set the loading status
|
||||
loading.value = false;
|
||||
}
|
||||
|
||||
const confirmClearCode = async (id: number) => {
|
||||
// Set the loading status
|
||||
loading.value = true;
|
||||
|
||||
// Send a request to the api
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Yale/code/${id}/status`, {
|
||||
method: 'POST',
|
||||
credentials: 'include'
|
||||
});
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<boolean>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
// If the response was successful set the code to available in the list
|
||||
const index = userCodes.value.findIndex(x => x.id === id);
|
||||
userCodes.value[index].code = '';
|
||||
userCodes.value[index].status = UserCodeStatus.AVAILABLE;
|
||||
} else {
|
||||
// Otherwise display an error to the user.
|
||||
alert(apiResponse.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
// Close the clear modal
|
||||
clearModal.value = false;
|
||||
|
||||
// Set the loading status
|
||||
loading.value = false;
|
||||
}
|
||||
|
||||
const handleShowCode = (id: number) => {
|
||||
// Get the user code to show
|
||||
const code = userCodes.value.find(x => x.id === id);
|
||||
|
||||
// If the code is not found then show an error to the user
|
||||
if (!code) {
|
||||
alert('Code not found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the user code to show
|
||||
userCode.value = code;
|
||||
|
||||
// Show the show-modal
|
||||
showModal.value = true;
|
||||
}
|
||||
|
||||
const handleUpdateCode = (id: number) => {
|
||||
// Get the user code to update
|
||||
const code = userCodes.value.find(x => x.id === id);
|
||||
|
||||
// If the code is not found then show an error to the user
|
||||
if (!code) {
|
||||
alert('Code not found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the user code to update
|
||||
userCode.value = code;
|
||||
|
||||
// Show the edit-modal
|
||||
editModal.value = true;
|
||||
}
|
||||
|
||||
const handleClearCode = (id: number) => {
|
||||
// Get the user code to clear
|
||||
const code = userCodes.value.find(x => x.id === id);
|
||||
|
||||
// If the code is not found then show an error to the user
|
||||
if (!code) {
|
||||
alert('Code not found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the user code to clear
|
||||
userCode.value = code;
|
||||
|
||||
// Show the clear-modal
|
||||
clearModal.value = true;
|
||||
}
|
||||
|
||||
const handleSendCode = (id: number) => {
|
||||
// Get the user code to send
|
||||
const code = userCodes.value.find(x => x.id === id);
|
||||
|
||||
// If the code is not found then show an error to the user
|
||||
if (!code) {
|
||||
alert('Code not found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the user code
|
||||
userCode.value = code;
|
||||
|
||||
// Show the send modal
|
||||
sendModal.value = true;
|
||||
}
|
||||
|
||||
const confirmSendCode = async (id: number) => {
|
||||
// Set the loading status
|
||||
loading.value = true;
|
||||
|
||||
if (!personNumber.value) {
|
||||
alert('Please select a person to send the code to');
|
||||
loading.value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Send a request to the api to send the code
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Yale/code/${id}/send`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: `"${personNumber.value}"`
|
||||
});
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<boolean>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
alert('Code sent successfully');
|
||||
} else {
|
||||
alert(apiResponse.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
// Close the clear modal
|
||||
sendModal.value = false;
|
||||
|
||||
// Clear the person number and user code
|
||||
personNumber.value = '';
|
||||
userCode.value = {} as YaleUserCode;
|
||||
|
||||
// Set the loading status
|
||||
loading.value = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<!-- Handle loading state -->
|
||||
<div v-if="loading">Loading...</div>
|
||||
<table v-else class="w-full text-left">
|
||||
<tr>
|
||||
<th scope="col">Code ID</th>
|
||||
<th scope="col">Status</th>
|
||||
<th scope="col">Actions</th>
|
||||
</tr>
|
||||
<UserCodeListRow v-for="userCode in userCodes" :key="userCode.id" :user-code="userCode"
|
||||
@update-code="handleUpdateCode" @clear-code="handleClearCode" @show-code="handleShowCode"
|
||||
@send-code="handleSendCode" />
|
||||
</table>
|
||||
|
||||
<!-- Show modal -->
|
||||
<Modal v-if="showModal" @close="showModal = false">
|
||||
<template #title>
|
||||
User Code # {{ userCode.id }}
|
||||
</template>
|
||||
<template #default>
|
||||
Code: {{ userCode.code }}
|
||||
</template>
|
||||
</Modal>
|
||||
|
||||
<!-- Edit modal -->
|
||||
<Modal v-if="editModal" @close="editModal = false">
|
||||
<template #title>
|
||||
Edit User Code # {{ userCode.id }}
|
||||
</template>
|
||||
<template #default>
|
||||
<YaleFormInput v-model="newCode" type="text" placeholder="New Code" class="block w-full" />
|
||||
<p class="text-red-600 mt-3" v-if="newCodeError">{{ newCodeError }}</p>
|
||||
<YaleButton @click="confirmUpdateCode(userCode.id, newCode)" class="w-full mt-3">Update Code</YaleButton>
|
||||
</template>
|
||||
<template #footer v-if="userCode.isHome">
|
||||
<p class="text-red-600 mt-3">You are about to edit the home code, are you sure?</p>
|
||||
</template>
|
||||
</Modal>
|
||||
|
||||
<!-- Clear modal -->
|
||||
<Modal v-if="clearModal" @close="clearModal = false">
|
||||
<template #title>
|
||||
Delete User Code # {{ userCode.id }}
|
||||
</template>
|
||||
<template #default>
|
||||
<p>Are you sure you want to delete this code?</p>
|
||||
<YaleButton @click="confirmClearCode(userCode.id)" class="w-full mt-3">Delete Code</YaleButton>
|
||||
</template>
|
||||
</Modal>
|
||||
|
||||
<!-- Send modal -->
|
||||
<Modal v-if="sendModal" @close="sendModal = false">
|
||||
<template #title>
|
||||
Send User Code # {{ userCode.id }}
|
||||
</template>
|
||||
<template #default>
|
||||
<YaleFormSelect v-model="personNumber" :options="people" class="block w-full"
|
||||
placeholder="Select a Person" />
|
||||
<YaleButton @click="confirmSendCode(userCode.id)" class="w-full mt-3">Send Code</YaleButton>
|
||||
</template>
|
||||
</Modal>
|
||||
</template>
|
||||
Reference in new issue
Block a user