Documentation
¶
Overview ¶
Package auth provides password hashing, verification, token generation, and role hierarchy utilities.
Index ¶
- func CheckPasswordStrength(password string) error
- func GenerateTOTPCode(secret string) string
- func GenerateTOTPSecret() (string, error)
- func GenerateTOTPURI(secret, issuer, accountName string) string
- func GenerateToken() (string, error)
- func HashPassword(password string) (string, error)
- func TokenHash(raw string) string
- func ValidateTOTP(secret, code string) bool
- func VerifyPassword(hash, password string) bool
- type RoleHierarchy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckPasswordStrength ¶ added in v0.13.0
CheckPasswordStrength validates that a password meets minimum strength rules: at least 8 characters, containing an uppercase letter, a lowercase letter, and a digit.
func GenerateTOTPCode ¶ added in v0.11.0
GenerateTOTPCode generates the current TOTP code for a given secret.
func GenerateTOTPSecret ¶ added in v0.10.0
GenerateTOTPSecret generates a random base32-encoded TOTP secret (160 bits).
func GenerateTOTPURI ¶ added in v0.10.0
GenerateTOTPURI builds an otpauth:// URI for use with authenticator apps.
func GenerateToken ¶ added in v0.13.0
GenerateToken returns a hex-encoded 32-byte random token using crypto/rand.
func HashPassword ¶
HashPassword hashes a plaintext password using bcrypt with a cost of 10.
func TokenHash ¶ added in v0.13.0
TokenHash returns the SHA-256 hex digest of the given string. Useful for storing a one-way hash of API keys, refresh tokens, etc.
func ValidateTOTP ¶ added in v0.10.0
ValidateTOTP checks a 6-digit code against a TOTP secret. Checks current, previous, and next time step to allow for clock drift.
func VerifyPassword ¶
VerifyPassword compares a plaintext password against a bcrypt hash. Returns true if the password matches the hash.
Types ¶
type RoleHierarchy ¶ added in v0.13.0
RoleHierarchy defines a role inheritance tree where each role maps to a list of roles it inherits from. Use Inherits to check whether a user's role satisfies a required role.
Example:
h := RoleHierarchy{
"viewer": {},
"editor": {"viewer"},
"admin": {"editor", "viewer"},
}
h.Inherits("admin", "viewer") // true
h.Inherits("viewer", "admin") // false
func (RoleHierarchy) Inherits ¶ added in v0.13.0
func (h RoleHierarchy) Inherits(userRole, requiredRole string) bool
Inherits reports whether userRole (or any role it transitively inherits from) equals requiredRole.