storage

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserAlreadyExistsError        = errors.New("a user with this id already exists")
	ErrUnknownUserError              = errors.New("unknown user")
	ErrIncorrectPasswordError        = errors.New("password does not match")
	ErrNoPasswordConfiguredError     = errors.New("no password configured")
	ErrSessionUnknownOrInactiveError = errors.New("session unknwon or inactive")

	ErrInvalidConfigurationError = errors.New("invalid configuration")
	ErrNoHostError               = fmt.Errorf("%w: no host given", ErrIncorrectPasswordError)
	ErrNoDBNameError             = fmt.Errorf("%w: no dbname given", ErrInvalidConfigurationError)
	ErrNoUserError               = fmt.Errorf("%w: no user given", ErrInvalidConfigurationError)
	ErrNoPasswordError           = fmt.Errorf("%w: no password given", ErrInvalidConfigurationError)
)

Functions

func DBOutputUserToPBUser

func DBOutputUserToPBUser(row DBOutputUser) *pbv1beta1.User

func PreparePostgresDB

func PreparePostgresDB(db *sql.DB) error

Types

type CreateSessionRequest

type CreateSessionRequest struct {
	Session     *Session
	ActiveUntil time.Time
}

type CreateUserRequest

type CreateUserRequest struct {
	User           *pbv1beta1.User
	HashedPassword *string
}

type DBOutputSession

type DBOutputSession struct {
	DBOutputUser
	ID        string    `db:"session_id"`
	ExpiresAt time.Time `db:"expires_at"`
}

type DBOutputUser

type DBOutputUser struct {
	DBUser
	Roles pq.StringArray `db:"roles"`
}

type DBOutputUserWithPassword

type DBOutputUserWithPassword struct {
	DBOutputUser
	Password sql.Null[string] `db:"password"`
}

type DBSession

type DBSession struct {
	ID        string    `db:"id"`
	UserID    string    `db:"user_id"`
	ExpiresAt time.Time `db:"expires_at"`
}

func CreateSessionRequestToDBSession

func CreateSessionRequestToDBSession(req *CreateSessionRequest) DBSession

type DBUser

type DBUser struct {
	ID       string           `db:"id"`
	Email    sql.Null[string] `db:"email"`
	FullName sql.Null[string] `db:"full_name"`
}

func PBUserToDBUser

func PBUserToDBUser(usr *pbv1beta1.User) DBUser

type DBUserRole

type DBUserRole struct {
	UserID string `db:"user_id"`
	Role   string `db:"role"`
}

func GrantUserRoleRequestToDBUserRole

func GrantUserRoleRequestToDBUserRole(req *pbv1beta1.GrantUserRoleRequest) DBUserRole

type LoginInfo

type LoginInfo struct {
	User           *pbv1beta1.User
	StoredPassword string
}

func DBOutputUserWithPasswordToLoginInfo

func DBOutputUserWithPasswordToLoginInfo(row DBOutputUserWithPassword) *LoginInfo

type Memory

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

func NewMemory

func NewMemory(conf MemoryConfig) *Memory

func (*Memory) CreateSession

func (m *Memory) CreateSession(ctx context.Context, req *CreateSessionRequest) error

func (*Memory) CreateUser

func (m *Memory) CreateUser(ctx context.Context, req *CreateUserRequest) error

func (*Memory) DeleteUser

func (m *Memory) DeleteUser(ctx context.Context, req *pbv1beta1.DeleteUserRequest) error

func (*Memory) GetActiveSession

func (m *Memory) GetActiveSession(ctx context.Context, sessionKey string) (*Session, error)

func (*Memory) GrantUserRoles

func (m *Memory) GrantUserRoles(ctx context.Context, req *pbv1beta1.GrantUserRoleRequest) error

func (*Memory) ListUserRoles

func (m *Memory) ListUserRoles(ctx context.Context, userId string) ([]string, error)

func (*Memory) ListUsers

func (m *Memory) ListUsers(ctx context.Context, req *pbv1beta1.ListUsersRequest) ([]*pbv1beta1.User, error)

func (*Memory) Purge

func (m *Memory) Purge(ctx context.Context) error

func (*Memory) ReadLoginInfo

func (m *Memory) ReadLoginInfo(ctx context.Context, userID *string, email *string) (*LoginInfo, error)

func (*Memory) ReadUser

func (m *Memory) ReadUser(ctx context.Context, userId string) (*pbv1beta1.User, error)

func (*Memory) RemoveSession

func (m *Memory) RemoveSession(ctx context.Context, sessionKey string) error

func (*Memory) RevokeUserRoles

func (m *Memory) RevokeUserRoles(ctx context.Context, req *pbv1beta1.RevokeUserRoleRequest) error

func (*Memory) SetUserPassword

func (m *Memory) SetUserPassword(ctx context.Context, req *SetUserPasswordRequest) error

func (*Memory) UpdateUser

func (m *Memory) UpdateUser(ctx context.Context, user *pbv1beta1.User) error

type MemoryConfig

type MemoryConfig struct {
	Now func() time.Time
}

type MemoryStoreOpts

type MemoryStoreOpts struct {
	// NowFunc optionally overrides "now" for the storage layer. Exposed only for unit tests
	NowFunc func() time.Time
}

type Postgres

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

func NewPostgres

func NewPostgres(conf PostgresConfig) *Postgres

func (*Postgres) CreateSession

func (p *Postgres) CreateSession(ctx context.Context, req *CreateSessionRequest) error

func (*Postgres) CreateUser

func (p *Postgres) CreateUser(ctx context.Context, req *CreateUserRequest) error

func (*Postgres) DeleteUser

func (p *Postgres) DeleteUser(ctx context.Context, req *pbv1beta1.DeleteUserRequest) error

func (*Postgres) GetActiveSession

func (p *Postgres) GetActiveSession(ctx context.Context, sessionKey string) (*Session, error)

func (*Postgres) GrantUserRoles

func (p *Postgres) GrantUserRoles(ctx context.Context, req *pbv1beta1.GrantUserRoleRequest) error

func (*Postgres) ListUserRoles

func (p *Postgres) ListUserRoles(ctx context.Context, userId string) ([]string, error)

func (*Postgres) ListUsers

func (p *Postgres) ListUsers(ctx context.Context, req *pbv1beta1.ListUsersRequest) ([]*pbv1beta1.User, error)

func (*Postgres) Purge

func (p *Postgres) Purge(ctx context.Context) error

func (*Postgres) ReadLoginInfo

func (p *Postgres) ReadLoginInfo(ctx context.Context, userID *string, email *string) (*LoginInfo, error)

func (*Postgres) ReadUser

func (p *Postgres) ReadUser(ctx context.Context, userId string) (*pbv1beta1.User, error)

func (*Postgres) RemoveSession

func (p *Postgres) RemoveSession(ctx context.Context, sessionKey string) error

func (*Postgres) RevokeUserRoles

func (p *Postgres) RevokeUserRoles(ctx context.Context, req *pbv1beta1.RevokeUserRoleRequest) error

func (*Postgres) SetUserPassword

func (p *Postgres) SetUserPassword(ctx context.Context, req *SetUserPasswordRequest) error

func (*Postgres) UpdateUser

func (p *Postgres) UpdateUser(ctx context.Context, user *pbv1beta1.User) error

type PostgresConfig

type PostgresConfig struct {
	DB      *sql.DB
	NowFunc func() time.Time
}

type PostgresStoreConnectionOpts

type PostgresStoreConnectionOpts struct {
	Host     string
	Port     int
	DBName   string
	User     string
	Password string //nolint: gosec // its env config, relax
	SSLMode  string
}

type PostgresStoreOpts

type PostgresStoreOpts struct {
	Logger *logr.Logger
	// DB is an existing database handle. Either DB or ConnectionOpts is required
	DB *sql.DB
	// ConnectionOpts is a set of params to connect to a postgres database. Either DB or ConnectionOpts is required
	ConnectionOpts *PostgresStoreConnectionOpts
	// NowFunc optionally overrides "now" for the storage layer. Exposed only for unit tests
	NowFunc func() time.Time
}

type Session

type Session struct {
	ID   string
	User *pbv1beta1.User
}

func DBOutputSessionToSession

func DBOutputSessionToSession(row DBOutputSession) *Session

type SetUserPasswordRequest

type SetUserPasswordRequest struct {
	UserId         string
	HashedPassword string
}

type Storer

type Storer interface {
	Purge(ctx context.Context) error

	CreateUser(ctx context.Context, req *CreateUserRequest) error
	ReadUser(ctx context.Context, userId string) (*pbv1beta1.User, error)
	ReadLoginInfo(ctx context.Context, userID *string, email *string) (*LoginInfo, error)
	ListUsers(ctx context.Context, req *pbv1beta1.ListUsersRequest) ([]*pbv1beta1.User, error)
	SetUserPassword(ctx context.Context, req *SetUserPasswordRequest) error
	UpdateUser(ctx context.Context, user *pbv1beta1.User) error
	DeleteUser(ctx context.Context, req *pbv1beta1.DeleteUserRequest) error
	ListUserRoles(ctx context.Context, userId string) ([]string, error)
	GrantUserRoles(ctx context.Context, req *pbv1beta1.GrantUserRoleRequest) error
	RevokeUserRoles(ctx context.Context, req *pbv1beta1.RevokeUserRoleRequest) error

	GetActiveSession(ctx context.Context, sessionKey string) (*Session, error)
	CreateSession(ctx context.Context, req *CreateSessionRequest) error
	RemoveSession(ctx context.Context, sessionKey string) error
}

func NewMemoryStore

func NewMemoryStore(opts MemoryStoreOpts) (Storer, func(), error)

func NewPostgresStore

func NewPostgresStore(opts PostgresStoreOpts) (Storer, func(), error)

Jump to

Keyboard shortcuts

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