mirror of https://github.com/wg-easy/wg-easy
Browse Source
* separate route for onboarding * zse zod for validation * use argon2id * add build toolspull/1648/head
committed by
Bernd Storath
17 changed files with 104 additions and 126 deletions
@ -0,0 +1,8 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const { username, password } = await readValidatedBody( |
|||
event, |
|||
validateZod(passwordType) |
|||
); |
|||
await Database.createUser(username, password); |
|||
return { success: true }; |
|||
}); |
@ -1,24 +0,0 @@ |
|||
import { DatabaseError } from '~~/services/database/repositories/database'; |
|||
|
|||
export default defineEventHandler(async (event) => { |
|||
setHeader(event, 'Content-Type', 'application/json'); |
|||
try { |
|||
const { username, password } = await readValidatedBody( |
|||
event, |
|||
validateZod(passwordType) |
|||
); |
|||
await Database.newUserWithPassword(username, password); |
|||
return { success: true }; |
|||
} catch (error) { |
|||
if (error instanceof DatabaseError) { |
|||
const t = await useTranslation(event); |
|||
throw createError({ |
|||
statusCode: 400, |
|||
statusMessage: t(error.message), |
|||
message: error.message, |
|||
}); |
|||
} else { |
|||
throw createError('Something happened !'); |
|||
} |
|||
} |
|||
}); |
@ -0,0 +1,15 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const { username, password } = await readValidatedBody( |
|||
event, |
|||
validateZod(passwordType) |
|||
); |
|||
const users = await Database.getUsers(); |
|||
if (users.length !== 0) { |
|||
throw createError({ |
|||
statusCode: 400, |
|||
statusMessage: 'Invalid state', |
|||
}); |
|||
} |
|||
await Database.createUser(username, password); |
|||
return { success: true }; |
|||
}); |
@ -1,47 +1,18 @@ |
|||
import bcrypt from 'bcryptjs'; |
|||
import argon2 from 'argon2'; |
|||
|
|||
/** |
|||
* Checks if `password` matches the user password. |
|||
* |
|||
* @param {string} password string to test |
|||
* @returns {boolean} `true` if matching user password, otherwise `false` |
|||
* Checks if `password` matches the hash. |
|||
*/ |
|||
export function isPasswordValid(password: string, hash: string): boolean { |
|||
return bcrypt.compareSync(password, hash); |
|||
} |
|||
|
|||
/** |
|||
* Checks if a password is strong based on following criteria : |
|||
* |
|||
* - minimum length of 12 characters |
|||
* - contains at least one uppercase letter |
|||
* - contains at least one lowercase letter |
|||
* - contains at least one number |
|||
* - contains at least one special character (e.g., !@#$%^&*(),.?":{}|<>). |
|||
* |
|||
* @param {string} password - The password to validate |
|||
* @returns {boolean} `true` if the password is strong, otherwise `false` |
|||
*/ |
|||
|
|||
export function isPasswordStrong(password: string): boolean { |
|||
if (password.length < 12) { |
|||
return false; |
|||
} |
|||
|
|||
const hasUpperCase = /[A-Z]/.test(password); |
|||
const hasLowerCase = /[a-z]/.test(password); |
|||
const hasNumber = /\d/.test(password); |
|||
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password); |
|||
|
|||
return hasUpperCase && hasLowerCase && hasNumber && hasSpecialChar; |
|||
export function isPasswordValid( |
|||
password: string, |
|||
hash: string |
|||
): Promise<boolean> { |
|||
return argon2.verify(hash, password); |
|||
} |
|||
|
|||
/** |
|||
* Hashes a password. |
|||
* |
|||
* @param {string} password - The plaintext password to hash |
|||
* @returns {string} The hash of the password |
|||
*/ |
|||
export function hashPassword(password: string): string { |
|||
return bcrypt.hashSync(password, 12); |
|||
export async function hashPassword(password: string): Promise<string> { |
|||
return argon2.hash(password); |
|||
} |
|||
|
Loading…
Reference in new issue