Documentation ¶
Index ¶
- Constants
- Variables
- func ClearAuth(c *fiber.Ctx)
- func CompareHash(password, encodedHash string) (match bool, err error)
- func ComputeTOTP(secret string, timestamp int64) (string, error)
- func CreateHash(password string) (encodedHash string, err error)
- func ErrorHandler(c *fiber.Ctx, err error) error
- func GenerateRandomBytes(n uint32) ([]byte, error)
- func GenerateSecureRandomBase32(n int) (string, error)
- func GenerateString(length int) string
- func GenerateTOTP(secret string, stepType StepDurationType) (string, error)
- func GetUserID(c *fiber.Ctx) string
- func ParseAndValidate(c *fiber.Ctx, body any) error
- func SetAuthCookie(c *fiber.Ctx, token string, expiration time.Time)
- func ValidateTOTP(secret, code string, stepType StepDurationType) bool
- type ArgonParams
- type Error
- type ErrorDetails
- type ErrorField
- type StepDurationType
Constants ¶
const AuthCookieName = "Authorization"
AuthCookieName is the name of the cookie used to store the session token.
const AuthDuration = time.Hour * 24 * 7 // 1 week
AuthDuration is the duration of the Authorization cookie it's also used for the token expiration.
Variables ¶
var ( ErrInternal = NewError(http.StatusInternalServerError, "An internal server error occurred while attempting to process the request.", nil) ErrForbidden = NewError(http.StatusForbidden, "You do not have permission to access the requested resource.", nil) ErrNotFound = NewError(http.StatusNotFound, "The requested resource does not exist.", nil) ErrNotImplemented = NewError(http.StatusNotImplemented, "A portion of this request has not been implemented.", nil) ErrInvalidCredentials = NewError(http.StatusUnauthorized, "Invalid credentials. Please try again.", &ErrorDetails{ Fields: []ErrorField{ { Name: "email", Errors: []string{"Invalid credentials. Please try again."}, }, { Name: "password", Errors: []string{"Invalid credentials. Please try again."}, }, }, }) )
Recurring Errors
var ( ErrInvalidHash = fmt.Errorf("the encoded hash is not in the correct format") ErrIncompatibleVersion = fmt.Errorf("incompatible version of argon2") )
Predefined errors for hash validation and processing.
var DefaultArgonParams = ArgonParams{ // contains filtered or unexported fields }
DefaultArgonParams provides default settings for Argon2 parameters.
Functions ¶
func ClearAuth ¶
func ClearAuth(c *fiber.Ctx)
ClearAuth clears the Authorization cookie by setting the MaxAge to 0 and replacing the value with an empty string.
func CompareHash ¶
CompareHash checks if a password matches the hash.
func ComputeTOTP ¶
ComputeTOTP computes the TOTP value for a given secret and time, and returns an error if any.
func CreateHash ¶
CreateHash generates a hash for a given password using Argon2.
func ErrorHandler ¶
func GenerateRandomBytes ¶
GenerateRandomBytes creates a slice of random bytes of specified length.
func GenerateSecureRandomBase32 ¶
GenerateSecureRandomBase32 generates a cryptographically secure random Base32 string of length n. It returns the generated string or an error if there was one.
func GenerateString ¶
GenerateString produces a random string of the specified length.
func GenerateTOTP ¶
func GenerateTOTP(secret string, stepType StepDurationType) (string, error)
GenerateTOTP provides a TOTP code for the current time and desired type (MFA or Email Verification).
func GetUserID ¶
func GetUserID(c *fiber.Ctx) string
GetUserID returns the session from the Authorization cookie.
func ParseAndValidate ¶
ParseAndValidate parses the request body into the provided struct and validates it. Returns a detailed error if validation fails.
func SetAuthCookie ¶
SetAuthCookie sets the Authorization cookie with the token and the duration.
func ValidateTOTP ¶
func ValidateTOTP(secret, code string, stepType StepDurationType) bool
ValidateTOTP verifies if the provided code matches the expected TOTP value for the given secret and duration type.
Types ¶
type ArgonParams ¶
type ArgonParams struct {
// contains filtered or unexported fields
}
ArgonParams defines the parameters for the Argon2 hashing algorithm.
func DecodeHash ¶
func DecodeHash(encodedHash string) (p *ArgonParams, salt, hash []byte, err error)
DecodeHash extracts the parameters, salt, and hash from an encoded hash string.
type Error ¶
type Error struct { Status int `json:"-"` Code string `json:"code"` Message string `json:"message"` Details *ErrorDetails `json:"details"` }
Error is the structure for an error responses.
type ErrorDetails ¶
type ErrorDetails struct {
Fields []ErrorField `json:"fields"`
}
ErrorDetails provides details about the error, such as fields but can be expanded to include more details.
type ErrorField ¶
ErrorField is a field that has an error, this is filled in by the validator.
type StepDurationType ¶
type StepDurationType int
StepDurationType defines the type of TOTP duration.
const ( MFACode StepDurationType = iota // For Multi-Factor Authentication. EmailVerification // For email code verification. )
These constants represent the two types of durations.