From 5daaa3a73c4eca2817d67e226679d125928a3645 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 26 Mar 2021 20:18:08 +0100 Subject: [PATCH] Now checking password rules on user creation ref #99 --- src/models/actions/create/CreateUser.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/models/actions/create/CreateUser.ts b/src/models/actions/create/CreateUser.ts index 06c2507..86bcd31 100644 --- a/src/models/actions/create/CreateUser.ts +++ b/src/models/actions/create/CreateUser.ts @@ -1,9 +1,10 @@ import * as argon2 from "argon2"; +import { passwordStrength } from "check-password-strength"; import { IsBoolean, IsEmail, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUrl } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import * as uuid from 'uuid'; import { config } from '../../../config'; -import { UserEmailNeededError, UsernameContainsIllegalCharacterError } from '../../../errors/UserErrors'; +import { PasswordMustContainLowercaseLetterError, PasswordMustContainNumberError, PasswordMustContainUppercaseLetterError, PasswordTooShortError, UserEmailNeededError, UsernameContainsIllegalCharacterError } from '../../../errors/UserErrors'; import { UserGroupNotFoundError } from '../../../errors/UserGroupErrors'; import { User } from '../../entities/User'; import { UserGroup } from '../../entities/UserGroup'; @@ -96,6 +97,13 @@ export class CreateUser { } if (this.username.includes("@")) { throw new UsernameContainsIllegalCharacterError(); } + + let password_strength = passwordStrength(this.password); + if (!password_strength.contains.includes("uppercase")) { throw new PasswordMustContainUppercaseLetterError(); } + if (!password_strength.contains.includes("lowercase")) { throw new PasswordMustContainLowercaseLetterError(); } + if (!password_strength.contains.includes("number")) { throw new PasswordMustContainNumberError(); } + if (!(password_strength.length > 9)) { throw new PasswordTooShortError(); } + newUser.email = this.email newUser.username = this.username newUser.firstname = this.firstname