Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

using randomly generated password salts instead of a static one

Blue Mousea61b79a5a9b7
diff --git a/.env.example b/.env.example
index e5230081a..3408116de 100644
--- a/.env.example
+++ b/.env.example
@@ -1,9 +1,6 @@
# Auth
JWT_SECRET=SOME_SECRET_TOKEN
-# Bcrypt Salt
-PASSWORD_SALT=BCRYPT_SECRET_SALT
-
# Database
MYSQL_HOST=localhost
MYSQL_USER=host_user
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index d4c73834c..11b82dac5 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -59,7 +59,7 @@ export class AuthController {
// await this.authService.checkCaptcha(recaptcha)
this.userService.validatePassword(userDTO.password)
- userDTO.password = this.authService.hashPassword(userDTO.password)
+ userDTO.password = await this.authService.hashPassword(userDTO.password)
try {
const user = await this.userService.create(userDTO)
@@ -144,7 +144,7 @@ export class AuthController {
}
if (actionToken.action === ActionTokenType.PASSWORD_RESET) {
- const password = this.authService.hashPassword(body.password)
+ const password = await this.authService.hashPassword(body.password)
await this.userService.update(actionToken.user.id, { ...actionToken.user, password })
await this.actionTokensService.delete(actionToken.id)
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index c18586fda..0714101c2 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -13,20 +13,19 @@ export class AuthService {
private userService: UserService,
) {}
- hashPassword(pass: string): string {
- return bcrypt.hashSync(pass, process.env.PASSWORD_SALT)
+ async hashPassword(pass: string): Promise<string> {
+ const salt = await bcrypt.genSalt(10)
+ return await bcrypt.hash(pass, salt)
}
- checkPassword(passToCheck: string, hashedPass: string): boolean {
- const hashedPassToCheck = this.hashPassword(passToCheck)
-
- return hashedPassToCheck === hashedPass
+ async checkPassword(passToCheck: string, hashedPass: string): Promise<boolean> {
+ return await bcrypt.compare(passToCheck, hashedPass)
}
async validateUser(email: string, pass: string): Promise<User> {
const user = await this.userService.findOneWhere({ email })
- if (user && this.checkPassword(pass, user.password)) {
+ if (user && await this.checkPassword(pass, user.password)) {
return user
}
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index 940ec7d37..c5bc612f5 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -74,7 +74,7 @@ export class UserController {
async create(@Body() userDTO: UserProfileDTO): Promise<User> {
this.logger.log({ userDTO }, 'POST /user')
this.userService.validatePassword(userDTO.password)
- userDTO.password = this.authService.hashPassword(userDTO.password)
+ userDTO.password = await this.authService.hashPassword(userDTO.password)
try {
const user = await this.userService.create({ ...userDTO, isActive: true })
@@ -176,7 +176,7 @@ export class UserController {
if (userDTO.password) {
this.userService.validatePassword(userDTO.password)
- userDTO.password = this.authService.hashPassword(userDTO.password)
+ userDTO.password = await this.authService.hashPassword(userDTO.password)
}
const user = await this.userService.findOneWhere({ id })
@@ -207,7 +207,7 @@ export class UserController {
if (userDTO.password?.length > 0) {
this.userService.validatePassword(userDTO.password)
- userDTO.password = this.authService.hashPassword(userDTO.password)
+ userDTO.password = await this.authService.hashPassword(userDTO.password)
await this.mailerService.sendEmail(userDTO.email, LetterTemplate.PasswordChanged)
}