auth

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package auth provides password hashing, verification, token generation, and role hierarchy utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckPasswordStrength added in v0.13.0

func CheckPasswordStrength(password string) error

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

func GenerateTOTPCode(secret string) string

GenerateTOTPCode generates the current TOTP code for a given secret.

func GenerateTOTPSecret added in v0.10.0

func GenerateTOTPSecret() (string, error)

GenerateTOTPSecret generates a random base32-encoded TOTP secret (160 bits).

func GenerateTOTPURI added in v0.10.0

func GenerateTOTPURI(secret, issuer, accountName string) string

GenerateTOTPURI builds an otpauth:// URI for use with authenticator apps.

func GenerateToken added in v0.13.0

func GenerateToken() (string, error)

GenerateToken returns a hex-encoded 32-byte random token using crypto/rand.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes a plaintext password using bcrypt with a cost of 10.

func TokenHash added in v0.13.0

func TokenHash(raw string) string

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

func ValidateTOTP(secret, code string) bool

ValidateTOTP checks a 6-digit code against a TOTP secret. Checks current, previous, and next time step to allow for clock drift.

func VerifyPassword

func VerifyPassword(hash, password string) bool

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

type RoleHierarchy map[string][]string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL