utils

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

This is a tiny JSON database

Index

Constants

View Source
const Version = "1.0.4"

Version is the current version of the project

Variables

View Source
var (
	ErrMissingResource   = errors.New("missing resource - unable to save record")
	ErrMissingCollection = errors.New("missing collection - no place to save record")
)

Functions

func AuthCookieGet added in v0.30.0

func AuthCookieGet(r *http.Request) string

func AuthCookieRemove added in v0.30.0

func AuthCookieRemove(w http.ResponseWriter, r *http.Request)

func AuthCookieSet added in v0.30.0

func AuthCookieSet(w http.ResponseWriter, r *http.Request, token string)

func AuthTokenRetrieve added in v0.30.0

func AuthTokenRetrieve(r *http.Request, useCookies bool) string

authTokenRetrieve retrieves the auth token from the request Several attempts are made:

  1. From cookie
  2. Authorization header (aka Bearer token)
  3. Request param "api_key"
  4. Request param "token"

func BearerTokenFromHeader added in v0.29.0

func BearerTokenFromHeader(authHeader string) string

BearerTokenFromHeader extracts the bearer token from the passed authorization header value. If a bearer token is not found, an empty string is returned.

Parameters:

  • authHeader: a string representing the authorization header

Returns:

  • a string representing the extracted bearer token

Example:

	authHeader := r.Header.Get("Authorization")
	authTokenFromBearerToken := BearerTokenFromHeader(authHeader)

 or simplified

	authTokenFromBearerToken := BearerTokenFromHeader(r.Header.Get("Authorization"))

func GeneratePasswordResetToken added in v0.29.0

func GeneratePasswordResetToken() (string, error)

GeneratePasswordResetToken generates a random password reset token.

func GenerateVerificationCode added in v0.29.0

func GenerateVerificationCode(extraHardened bool) (string, error)

GenerateVerificationCode generates a random verification code using the configured length and gamma.

func LoginCodeGamma

func LoginCodeGamma(extraHardened bool) string

LoginCodeGamma returns the character set (gamma) used for verification/ login codes. extraHardened should be false in normal operation; it is only set to true when rate limiting is explicitly disabled (again, not recommended for production). In that hardened mode it returns a much larger alphabet to drastically increase entropy; combined with the hardened length, this makes brute-force attacks negligible even without rate limiting.

func LoginCodeLength

func LoginCodeLength(extraHardened bool) int

LoginCodeLength returns the length of verification/login codes. extraHardened should be false in normal operation; it is only set to true when rate limiting is explicitly disabled (which should never happen in production). In that hardened mode it returns a longer length to significantly increase the search space; together with the hardened gamma this yields an astronomically large space.

func ValidateEmailFormat added in v0.29.0

func ValidateEmailFormat(email string) string

func ValidatePasswordStrength added in v0.29.0

func ValidatePasswordStrength(password string, cfg *authtypes.PasswordStrengthConfig) error

ValidatePasswordStrength validates the provided password against the supplied PasswordStrengthConfig. If cfg is nil, no checks are applied.

Types

type Driver added in v0.30.0

type Driver struct {
	// contains filtered or unexported fields
}

Driver is what is used to interact with the scribble database. It runs transactions, and provides log output

func New added in v0.30.0

func New(dir string, options *Options) (*Driver, error)

New creates a new scribble database at the desired directory location, and returns a *Driver to then use for interacting with the database

func (*Driver) Delete added in v0.30.0

func (d *Driver) Delete(collection, resource string) error

Delete locks the database then attempts to remove the collection/resource specified by path

func (*Driver) Read added in v0.30.0

func (d *Driver) Read(collection, resource string, v interface{}) error

Read a record from the database

func (*Driver) ReadAll added in v0.30.0

func (d *Driver) ReadAll(collection string) ([][]byte, error)

ReadAll records from a collection; this is returned as a slice of strings because there is no way of knowing what type the record is.

func (*Driver) Write added in v0.30.0

func (d *Driver) Write(collection, resource string, v interface{}) error

Write locks the database and attempts to write the record to the database under the [collection] specified with the [resource] name given

type InMemoryRateLimiter added in v0.29.0

type InMemoryRateLimiter struct {
	// contains filtered or unexported fields
}

InMemoryRateLimiter provides thread-safe in-memory rate limiting

func NewInMemoryRateLimiter added in v0.29.0

func NewInMemoryRateLimiter(maxAttempts int, windowDuration time.Duration, lockoutDuration time.Duration) *InMemoryRateLimiter

NewInMemoryRateLimiter creates a new in-memory rate limiter with default settings

func (*InMemoryRateLimiter) Check added in v0.29.0

func (r *InMemoryRateLimiter) Check(ip string, endpoint string) RateLimitResult

Check verifies if a request from the given IP to the given endpoint should be allowed

func (*InMemoryRateLimiter) Stop added in v0.29.0

func (r *InMemoryRateLimiter) Stop()

Stop gracefully stops the rate limiter's background cleanup

type Options added in v0.30.0

type Options struct {
	Logger *slog.Logger // the logger scribble will use (configurable)
}

Options uses for specification of working golang-scribble

type RateLimitResult added in v0.29.0

type RateLimitResult struct {
	Allowed    bool
	RetryAfter time.Duration
}

RateLimitResult represents the result of a rate limit check

Jump to

Keyboard shortcuts

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