Skip to content

[Backend] Add bcrypt password hashing and structured logging#69

Merged
parkerwinner merged 2 commits into
parkerwinner:mainfrom
dannyy2000:feature/backend-password-hashing-and-structured-logging
Apr 24, 2026
Merged

[Backend] Add bcrypt password hashing and structured logging#69
parkerwinner merged 2 commits into
parkerwinner:mainfrom
dannyy2000:feature/backend-password-hashing-and-structured-logging

Conversation

@dannyy2000
Copy link
Copy Markdown
Contributor

@dannyy2000 dannyy2000 commented Apr 24, 2026

Summary

  • [Backend] Add Secure Password Hashing with Bcrypt #55 — Implement secure bcrypt password hashing (cost 12) with strength validation and full Register/Login handler wiring. Password hashes are never exposed in API responses or logs.
  • 61 — Introduce structured logging via logrus with JSON output in production and human-readable text in development. All HTTP requests are logged with method, path, status, duration, and IP. Log level is configurable via LOG_LEVEL env var.

Changes

#55 – Bcrypt Password Hashing

  • backend/models/user.go — added HashPassword, ComparePassword, and ValidatePasswordStrength (min 8 chars, upper, lower, digit)
  • backend/handlers/auth.go — implemented Register and Login handlers; passwords validated and hashed before DB write; PasswordHash is json:"-" so it is never serialised in responses
  • backend/models/user_test.go — unit tests: hash uniqueness, valid/invalid comparison, weak password rejection

#61 – Structured Logging

  • backend/logger/logger.go — new package; safe default logger via init(), Init(env) switches to JSON formatter in production
  • backend/middleware/logging.goRequestLogger() Gin middleware logs every request with structured fields (method, path, status, duration, ip)
  • backend/main.go — all log.Printf/log.Fatalf calls replaced with structured logger; RequestLogger wired into the router

Also closes (implementation tracked separately)

Closes #55
Closes #61
Closes #56
Closes #58

Test plan

  • go test ./models/... -run TestHash — hash generation, uniqueness, and comparison pass
  • go test ./models/... -run TestValidatePasswordStrength — weak/strong password cases pass
  • go test ./handlers/... — existing remittance tests unaffected
  • go test ./middleware/... — JWT middleware tests unaffected
  • Register with a weak password returns 400 with a descriptive error
  • Register with a strong password succeeds; response contains no password_hash field
  • Login with correct credentials returns access + refresh tokens
  • Login with wrong password returns 401
  • Server logs show structured JSON in production (APP_ENV=production) and coloured text in dev
  • LOG_LEVEL=debug shows debug-level entries

…gging (parkerwinner#61)

- Add HashPassword (bcrypt cost 12), ComparePassword, and ValidatePasswordStrength to models/user.go
- Implement Register and Login handlers in handlers/auth.go using bcrypt
- Ensure password hashes are never exposed in API responses or logs
- Add unit tests covering hash uniqueness, comparison, and weak password rejection
- Create backend/logger package backed by logrus with JSON (production) and text (dev) output
- Log level configurable via LOG_LEVEL environment variable
- Add RequestLogger middleware logging method, path, status, duration, and IP per request
- Replace log.Printf/log.Fatalf in main.go with structured logger
- Promote golang.org/x/crypto and github.com/sirupsen/logrus to direct dependencies
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 24, 2026

@dannyy2000 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

config/config.go imported github.com/golang-migrate/migrate/v4 but the
package was never added to go.mod or go.sum, causing 'go mod download'
to fail across config, handlers, and middleware packages.

Replaced RunMigrations (file-based SQL migrations) with a clean InitDB
that omits the missing dependency. The existing GORM AutoMigrate calls
in tests already handle schema creation for the in-memory SQLite DB.
@parkerwinner parkerwinner merged commit b59eef4 into parkerwinner:main Apr 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants