Files
egutierrez eff5771b03 feat: jwt_generate, jwt_validate, password_hash, password_verify
Fase 2 del issue 0010 — auth core:
- jwt_generate/validate: HS256 manual con crypto/hmac + crypto/sha256
- password_hash/verify: wrappers de golang.org/x/crypto/bcrypt (cost 12 default)
- JWT rechaza alg != HS256 para mitigar ataque 'alg=none'
- hmac.Equal para comparacion constant-time de firmas
2026-04-18 17:39:00 +02:00

1.8 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
password_verify function go infra 1.0.0 impure func PasswordVerify(password string, hash string) error Verifica un password en texto plano contra un hash bcrypt. Retorna nil si hacen match, error si no coinciden o si el hash es invalido.
password
verify
bcrypt
auth
infra
false error_go_core
golang.org/x/crypto/bcrypt
name desc
password password en texto plano a verificar
name desc
hash hash bcrypt obtenido previamente de PasswordHash (guardado en BD)
nil si el password coincide con el hash; error si no coincide o hash invalido true
verifica password correcto
rechaza password incorrecto
rechaza hash malformado
functions/infra/password_verify_test.go functions/infra/password_verify.go

Ejemplo

row := db.QueryRow("SELECT password_hash FROM users WHERE email = ?", email)
var stored string
if err := row.Scan(&stored); err != nil {
    HTTPErrorResponse(w, HTTPError{Status: 401, Code: "invalid_credentials", Message: "email o password incorrectos"})
    return
}
if err := PasswordVerify(input, stored); err != nil {
    HTTPErrorResponse(w, HTTPError{Status: 401, Code: "invalid_credentials", Message: "email o password incorrectos"})
    return
}
// OK, emitir token

Notas

Impura — bcrypt.CompareHashAndPassword es constant-time internamente (mitiga timing attacks). En respuestas HTTP al usuario NO distinguir entre "email no existe" y "password incorrecto": ambos casos deben retornar el mismo mensaje generico para no filtrar existencia de cuentas. El error real se puede loguear internamente con log_info/log_warn sin problema.