ci: expand shared package validation and test coverage
- Add lint, formatting, vet, and publish workflow checks - Run Go database tests with PostgreSQL and add Java tests - Make consumer sync opt-in; update docs and Go dependency
This commit is contained in:
1 parent
937f51b4fb
commit
0ff94f9b10
107 files changed
+10124
-410
No files matched your search
@@ -1,21 +1,80 @@
|
||||
package auth
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// BcryptCost is kept in one place so all services use the same password
|
||||
// storage policy. Existing hashes remain valid when this value is raised.
|
||||
// storage policy. Hashes stored at a lower cost are rewritten on the next
|
||||
// successful login (see VerifyAndUpgradePassword).
|
||||
const BcryptCost = 12
|
||||
|
||||
// HashPassword returns a bcrypt password hash suitable for storage.
|
||||
// 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$"
|
||||
|
||||
// HashPassword returns a versioned password hash suitable for storage.
|
||||
func HashPassword(password string) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), BcryptCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(hash), nil
|
||||
return bcryptScheme + string(hash), nil
|
||||
}
|
||||
|
||||
// VerifyPassword checks a plaintext password against a stored bcrypt hash.
|
||||
// VerifyPassword checks a plaintext password against a stored hash. It accepts
|
||||
// hashes from HashPassword and unprefixed bcrypt hashes; any other format
|
||||
// fails closed.
|
||||
func VerifyPassword(hash, password string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
||||
bcryptHash, ok := bcryptHashOf(hash)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
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 {
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user