2026-08-06 16:53:48 +02:00
|
|
|
package auth
|
|
|
|
|
|
2026-10-19 21:46:52 +00:00
|
|
|
import (
|
2026-10-20 15:27:53 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
|
"crypto/subtle"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"errors"
|
|
|
|
|
"strconv"
|
2026-10-19 21:46:52 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2026-10-20 15:27:53 +00:00
|
|
|
"golang.org/x/crypto/pbkdf2"
|
2026-10-19 21:46:52 +00:00
|
|
|
)
|
2026-08-06 16:53:48 +02:00
|
|
|
|
|
|
|
|
// BcryptCost is kept in one place so all services use the same password
|
2026-10-19 21:46:52 +00:00
|
|
|
// storage policy. Hashes stored at a lower cost are rewritten on the next
|
|
|
|
|
// successful login (see VerifyAndUpgradePassword).
|
2026-08-06 16:53:48 +02:00
|
|
|
const BcryptCost = 12
|
|
|
|
|
|
2026-10-19 21:46:52 +00:00
|
|
|
// bcryptScheme prefixes every hash HashPassword writes, so verification picks
|
|
|
|
|
// the algorithm from the stored value. The bcrypt string follows unchanged, so
|
|
|
|
|
// a stored value reads "bcrypt$$2a$12$..." (the bcrypt string itself starts
|
|
|
|
|
// with "$"). Unprefixed bcrypt hashes from before the prefix still verify and
|
|
|
|
|
// are upgraded on login.
|
|
|
|
|
const bcryptScheme = "bcrypt$"
|
2026-10-20 15:27:53 +00:00
|
|
|
const bcryptSHA256Scheme = "bcrypt-sha256$"
|
|
|
|
|
|
|
|
|
|
const (
|
2026-10-27 09:54:10 +00:00
|
|
|
maxPasswordBytes = 72
|
2026-10-20 15:27:53 +00:00
|
|
|
legacyPBKDF2Iterations = 100000
|
|
|
|
|
maxPBKDF2Iterations = 10000000
|
|
|
|
|
)
|
2026-10-19 21:46:52 +00:00
|
|
|
|
|
|
|
|
// HashPassword returns a versioned password hash suitable for storage.
|
2026-08-06 16:53:48 +02:00
|
|
|
func HashPassword(password string) (string, error) {
|
2026-10-20 15:27:53 +00:00
|
|
|
if len([]byte(password)) > maxPasswordBytes {
|
|
|
|
|
return "", errors.New("password must be at most 72 bytes")
|
|
|
|
|
}
|
2026-08-06 16:53:48 +02:00
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), BcryptCost)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2026-10-19 21:46:52 +00:00
|
|
|
return bcryptScheme + string(hash), nil
|
2026-08-06 16:53:48 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 15:27:53 +00:00
|
|
|
// VerifyPassword checks a plaintext password against bcrypt and legacy Node
|
|
|
|
|
// PBKDF2 hashes. A valid legacy hash is rewritten by VerifyAndUpgradePassword.
|
2026-08-06 16:53:48 +02:00
|
|
|
func VerifyPassword(hash, password string) bool {
|
2026-10-20 15:27:53 +00:00
|
|
|
if bcryptHash, ok := strings.CutPrefix(hash, bcryptSHA256Scheme); ok {
|
|
|
|
|
digest := sha256.Sum256([]byte(password))
|
|
|
|
|
return bcrypt.CompareHashAndPassword([]byte(bcryptHash), []byte(hex.EncodeToString(digest[:]))) == nil
|
|
|
|
|
}
|
|
|
|
|
if iterations, salt, expected, ok := parsePBKDF2Hash(hash); ok {
|
|
|
|
|
actual := pbkdf2.Key([]byte(password), []byte(salt), iterations, 32, sha256.New)
|
|
|
|
|
expectedBytes, err := hex.DecodeString(expected)
|
|
|
|
|
return err == nil && subtle.ConstantTimeCompare(expectedBytes, actual) == 1
|
|
|
|
|
}
|
2026-10-19 21:46:52 +00:00
|
|
|
bcryptHash, ok := bcryptHashOf(hash)
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2026-10-20 15:27:53 +00:00
|
|
|
if len([]byte(password)) > maxPasswordBytes {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2026-10-19 21:46:52 +00:00
|
|
|
return bcrypt.CompareHashAndPassword([]byte(bcryptHash), []byte(password)) == nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PasswordHashNeedsUpgrade reports whether a stored hash is not in the current
|
|
|
|
|
// scheme: an unprefixed legacy hash, or a bcrypt cost below BcryptCost.
|
|
|
|
|
func PasswordHashNeedsUpgrade(hash string) bool {
|
2026-10-20 15:27:53 +00:00
|
|
|
if bcryptHash, prefixed := strings.CutPrefix(hash, bcryptSHA256Scheme); prefixed {
|
|
|
|
|
cost, err := bcrypt.Cost([]byte(bcryptHash))
|
|
|
|
|
return err != nil || cost < BcryptCost
|
|
|
|
|
}
|
|
|
|
|
if _, _, _, ok := parsePBKDF2Hash(hash); ok {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2026-10-19 21:46:52 +00:00
|
|
|
bcryptHash, prefixed := strings.CutPrefix(hash, bcryptScheme)
|
|
|
|
|
if !prefixed {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
cost, err := bcrypt.Cost([]byte(bcryptHash))
|
|
|
|
|
return err != nil || cost < BcryptCost
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VerifyAndUpgradePassword checks password against a stored hash. When it
|
|
|
|
|
// matches a hash that is not in the current scheme, it returns a replacement
|
|
|
|
|
// from HashPassword for the caller to store; upgraded is empty when no rewrite
|
|
|
|
|
// is needed or the password does not match. A non-nil error with valid set
|
|
|
|
|
// means the login succeeded but the replacement could not be made.
|
|
|
|
|
func VerifyAndUpgradePassword(hash, password string) (valid bool, upgraded string, err error) {
|
|
|
|
|
if !VerifyPassword(hash, password) {
|
|
|
|
|
return false, "", nil
|
|
|
|
|
}
|
|
|
|
|
if !PasswordHashNeedsUpgrade(hash) {
|
|
|
|
|
return true, "", nil
|
|
|
|
|
}
|
2026-10-20 15:27:53 +00:00
|
|
|
if len([]byte(password)) > maxPasswordBytes {
|
|
|
|
|
digest := sha256.Sum256([]byte(password))
|
|
|
|
|
var hashBytes []byte
|
|
|
|
|
hashBytes, err = bcrypt.GenerateFromPassword([]byte(hex.EncodeToString(digest[:])), BcryptCost)
|
|
|
|
|
if err == nil {
|
|
|
|
|
upgraded = bcryptSHA256Scheme + string(hashBytes)
|
|
|
|
|
}
|
|
|
|
|
return true, upgraded, err
|
|
|
|
|
}
|
2026-10-19 21:46:52 +00:00
|
|
|
upgraded, err = HashPassword(password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return true, "", err
|
|
|
|
|
}
|
|
|
|
|
return true, upgraded, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// bcryptHashOf returns the bcrypt string inside a stored hash.
|
|
|
|
|
func bcryptHashOf(stored string) (string, bool) {
|
|
|
|
|
if rest, prefixed := strings.CutPrefix(stored, bcryptScheme); prefixed {
|
|
|
|
|
return rest, true
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(stored, "$2a$") || strings.HasPrefix(stored, "$2b$") || strings.HasPrefix(stored, "$2y$") {
|
|
|
|
|
return stored, true
|
|
|
|
|
}
|
|
|
|
|
return "", false
|
2026-08-06 16:53:48 +02:00
|
|
|
}
|
2026-10-20 15:27:53 +00:00
|
|
|
|
|
|
|
|
func parsePBKDF2Hash(stored string) (iterations int, salt, expected string, ok bool) {
|
|
|
|
|
if strings.HasPrefix(stored, "pbkdf2-sha256$") {
|
|
|
|
|
parts := strings.Split(stored, "$")
|
|
|
|
|
if len(parts) != 4 {
|
|
|
|
|
return 0, "", "", false
|
|
|
|
|
}
|
|
|
|
|
parsedIterations, err := strconv.Atoi(parts[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, "", "", false
|
|
|
|
|
}
|
|
|
|
|
iterations, salt, expected = parsedIterations, parts[2], parts[3]
|
|
|
|
|
} else if strings.Count(stored, ":") == 1 {
|
|
|
|
|
parts := strings.SplitN(stored, ":", 2)
|
|
|
|
|
iterations, salt, expected = legacyPBKDF2Iterations, parts[0], parts[1]
|
|
|
|
|
} else {
|
|
|
|
|
return 0, "", "", false
|
|
|
|
|
}
|
|
|
|
|
if iterations < 1 || iterations > maxPBKDF2Iterations || len(salt) != 32 || len(expected) != 64 {
|
|
|
|
|
return 0, "", "", false
|
|
|
|
|
}
|
|
|
|
|
if _, err := hex.DecodeString(salt); err != nil {
|
|
|
|
|
return 0, "", "", false
|
|
|
|
|
}
|
|
|
|
|
if _, err := hex.DecodeString(expected); err != nil {
|
|
|
|
|
return 0, "", "", false
|
|
|
|
|
}
|
|
|
|
|
return iterations, salt, expected, true
|
|
|
|
|
}
|