mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-10-12 07:18:49 +00:00
66403e88fb
- Remove registration tokens feature in favor of simpler API token auth - Add password authentication with change password functionality - Centralize API client logic with proper auth handling - Add development scripts for better DX (hot-reload, proxy setup) - Refactor auth middleware and handlers for cleaner separation - Update frontend to use new centralized API client
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"strings"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const (
|
|
// BcryptCost is the cost factor for bcrypt hashing
|
|
// Higher values are more secure but slower
|
|
BcryptCost = 12
|
|
)
|
|
|
|
// HashPassword generates a bcrypt hash from a plain text password
|
|
func HashPassword(password string) (string, error) {
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), BcryptCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(bytes), nil
|
|
}
|
|
|
|
// CheckPasswordHash compares a plain text password with a hash
|
|
func CheckPasswordHash(password, hash string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
return err == nil
|
|
}
|
|
|
|
// IsPasswordHashed checks if a string looks like a bcrypt hash
|
|
func IsPasswordHashed(password string) bool {
|
|
// Bcrypt hashes start with $2a$, $2b$, or $2y$ and are 60 characters long
|
|
return strings.HasPrefix(password, "$2") && len(password) == 60
|
|
}
|
|
|
|
// MigratePassword takes a password that might be plain text or hashed
|
|
// and returns a properly hashed version
|
|
func MigratePassword(password string) (string, error) {
|
|
if IsPasswordHashed(password) {
|
|
// Already hashed, return as-is
|
|
return password, nil
|
|
}
|
|
// Plain text password, hash it
|
|
return HashPassword(password)
|
|
} |