core

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserAlreadyExists = errors.New("a user with this name already exists")
	ErrUserNotFound      = errors.New("user not found")
)
View Source
var Config = func() AppConfig {
	config := AppConfig{
		DbPath:             resolvePath(env("GENESIS_DB_PATH")),
		BaseUrl:            env("GENESIS_BASE_URL"),
		JWTSecret:          []byte(env("GENESIS_JWT_SECRET")),
		JWTExpiration:      time.Duration(parseInt(env("GENESIS_JWT_TOKEN_EXPIRATION"))) * time.Minute,
		JWTCookieAllowHTTP: env("GENESIS_JWT_COOKIE_ALLOW_HTTP") == "true",
		AppBuildVersion:    env("GENESIS_BUILD_VERSION"),
		AppBuildDate:       env("GENESIS_BUILD_DATE"),
		AppBuildCommit:     env("GENESIS_BUILD_COMMIT"),
		AppGinMode:         env("GENESIS_GIN_MODE"),
		AppPort:            env("GENESIS_PORT"),
		AppUsersToCreate:   parseInitialUserList(env("GENESIS_CREATE_USERS")),
		AppUserPattern:     regexp.MustCompile(env("GENESIS_USERNAME_PATTERN")),
		AppKeyPattern:      regexp.MustCompile(env("GENESIS_KEY_PATTERN")),
		AppDataMaxSize:     parseInt(env("GENESIS_DATA_MAX_SIZE")) * 1000,
		AppKeysPerUser:     parseInt(env("GENESIS_KEYS_PER_USER")),
		LoginMaxAttempts:   parseInt(env("GENESIS_LOGIN_MAX_ATTEMPTS")),
		LoginLockDurations: parseDurations(env("GENESIS_LOGIN_LOCKOUT_DURATIONS")),
	}

	Logger.Debug("build info",
		zap.String("version", config.AppBuildVersion),
		zap.String("date", config.AppBuildDate),
		zap.String("commit", config.AppBuildCommit),
	)

	return config
}()
View Source
var Logger = func() *zap.Logger {
	_, filename, _, _ := runtime.Caller(0)
	var root = path.Join(path.Dir(filename), "..")

	envFile := path.Join(root, ".env")
	if testing.Testing() {
		envFile = path.Join(root, ".env.test")
	}

	envSkipped := godotenv.Load(envFile)

	var cfg zap.Config
	if os.Getenv("GENESIS_LOG_MODE") == "production" {
		cfg = zap.NewProductionConfig()
	} else {
		cfg = zap.NewDevelopmentConfig()
	}

	logger, err := cfg.Build(zap.AddCallerSkip(1))
	if err != nil {
		log.Fatal(err)
	}

	if envSkipped != nil {
		logger.Debug(".env file skipped")
	}

	return logger
}()

Functions

func ApplyFailedAttempt added in v1.5.0

func ApplyFailedAttempt(id string)

func CreateAuthToken

func CreateAuthToken(user *User) (string, error)

func CreateUser

func CreateUser(user User) error

func DeleteDataFromUser

func DeleteDataFromUser(name string, key string) error

func DeleteUser

func DeleteUser(name string) error

func GetAllDataFromUser

func GetAllDataFromUser(name string) ([]byte, error)

func GetDataCountForUser

func GetDataCountForUser(name, includedKey string) int64

func GetDataFromUser

func GetDataFromUser(name string, key string) ([]byte, error)

func InitializeUsers added in v1.4.0

func InitializeUsers()

func IsLockedOut added in v1.5.0

func IsLockedOut(id string) (bool, time.Duration)

func IsTokenBlacklisted

func IsTokenBlacklisted(jti string) (bool, error)

func ResetAllFailedLoginAttempts added in v1.5.0

func ResetAllFailedLoginAttempts()

func ResetDatabase

func ResetDatabase()

func ResetFailedLoginAttempts added in v1.5.0

func ResetFailedLoginAttempts(id string)

func SetDataForUser

func SetDataForUser(name string, key string, data []byte) error

func StoreInvalidatedToken

func StoreInvalidatedToken(jti string, expiration time.Duration) error

func UpdateUser

func UpdateUser(name string, user PartialUser) error

Types

type AppConfig

type AppConfig struct {
	DbPath             string
	BaseUrl            string
	JWTSecret          []byte
	JWTExpiration      time.Duration
	JWTCookieAllowHTTP bool
	AppBuildVersion    string
	AppBuildDate       string
	AppBuildCommit     string
	AppGinMode         string
	AppPort            string
	AppUsersToCreate   []User
	AppUserPattern     *regexp.Regexp
	AppKeyPattern      *regexp.Regexp
	AppDataMaxSize     int64
	AppKeysPerUser     int64
	LoginMaxAttempts   int64
	LoginLockDurations []time.Duration
}

type JWTClaim

type JWTClaim struct {
	User string `json:"user"`
	jwt.RegisteredClaims
}

func ParseAuthToken

func ParseAuthToken(token string) (*JWTClaim, error)

type PartialUser

type PartialUser struct {
	Admin    *bool   `json:"admin,omitempty"`
	Password *string `json:"password,omitempty" validate:"omitempty,gte=8,lte=64"`
}

type PublicUser

type PublicUser struct {
	Name  string `json:"name"`
	Admin bool   `json:"admin"`
}

func GetAllUsers added in v1.3.0

func GetAllUsers() ([]*PublicUser, error)

func GetUsers

func GetUsers(skip string) ([]*PublicUser, error)

type User

type User struct {
	Name     string `json:"name" validate:"required,gte=3,lte=32"`
	Admin    bool   `json:"admin"`
	Password string `json:"password" validate:"required,gte=8,lte=64"`
}

func AuthenticateUser

func AuthenticateUser(name string, password string) (*User, error)

func GetUser

func GetUser(name string) (*User, error)

Jump to

Keyboard shortcuts

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