Skip to content

Conversation

@PavanCodes05
Copy link
Contributor

@PavanCodes05 PavanCodes05 commented Oct 30, 2025

Enhanced Login Flow with Secure Bcrypt Handling

Closes #21

Summary

This PR enhances the authentication logic by introducing bcrypt-based password hashing while maintaining backward compatibility for legacy users who still have plain-text passwords.


Key Changes

  • Added a helper function isBcryptHash to detect bcrypt-hashed passwords.
  • Implemented secure password verification using bcrypt.CompareHashAndPassword.
  • Automatically rehashes and updates passwords for legacy users upon successful login.

Why This Change

  • Strengthens password security using bcrypt hashing.
  • Ensures seamless migration for existing users without breaking authentication.
  • Improves maintainability and reliability of the login module.

Testing

  • Verified login functionality for:
    • Bcrypt-hashed users ✅
    • Legacy (plain-text) users ✅
  • Tested:
    • Invalid credentials ❌
    • Missing input fields ❌
    • Automatic DB update on successful login for legacy users ✅

Impact

  • Improves security while preserving backward compatibility.
  • No frontend or API changes required.

Migration Note

It’s recommended to back up the users table before deployment.
Legacy user passwords will be upgraded to bcrypt hashes automatically after successful logins.

- Import bcrpyt lib
- Hash password before inserting to db
- Insert hashed password to db
- Import bcrypt lib
- Get user and password hash
- Verify password with hash
- Added bcrypt hash detection via  helper
- Implemented secure password comparison and rehashing for legacy users
Comment on lines +87 to +100
} else {
// Compare plain text for legacy users
if storedPasswordHash != l.Password {
logger.Info("Login: invalid password attempt (legacy)")
return nil, fmt.Errorf("invalid username/email or password")
}

// Rehash and update DB for this user
newHash, err := bcrypt.GenerateFromPassword([]byte(l.Password), bcrypt.DefaultCost)
if err == nil {
_, _ = db.Exec(ctx, "UPDATE users SET password = $1 WHERE id = $2", string(newHash), id)
logger.Info(fmt.Sprintf("Upgraded password hash for user %v", id))
}
}
Copy link
Member

@Ashrockzzz2003 Ashrockzzz2003 Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's great that you thought about this. But a better way is where you write a migration script and add it as part of the initialization of the service.

In that migration script you migrate all passwords to the hashed form. Your approach fails in cases where users have set actual passwords starting with $2b ... etc

Maintain a new column that says legacy/v2, maybe version numbers for each record in the user table. default value is v1, new passwords gets v2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Security: User passwords are unhashed

2 participants