sqlite

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package sqlite implements gouncer's user and session persistence in SQLite tables of its own.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, db *sql.DB, opts MigrateOptions) error

Migrate applies the auth table migrations to db under the migration lock of its file.

Types

type MigrateOptions

type MigrateOptions struct {
	// LockWait is how long a run waits for the migration lock, above zero.
	LockWait time.Duration
	// LockPoll is how often a waiting run tries the migration lock, above zero.
	LockPoll time.Duration
}

MigrateOptions sets how a run of Migrate waits for the migration lock of the database file.

type UserStore

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

UserStore persists users, their sessions and their tokens in the auth tables of an application's SQLite file.

func NewUserStore

func NewUserStore(handle *sql.DB) *UserStore

NewUserStore returns a UserStore on the handle dbkit/sqlite opened for the application.

func (*UserStore) ActivateByToken

func (s *UserStore) ActivateByToken(
	ctx context.Context,
	tokenHash []byte,
	now time.Time,
	passwordHash string,
) (uuid.UUID, error)

ActivateByToken spends the invite token, stores the password hash and confirms the account it names.

func (*UserStore) CreateSession

func (s *UserStore) CreateSession(ctx context.Context, session gouncer.Session) error

CreateSession stores a login session.

func (*UserStore) CreateToken

func (s *UserStore) CreateToken(ctx context.Context, t gouncer.Token, live int) error

CreateToken stores t for an enabled account while fewer than live unexpired tokens stand for its purpose.

func (*UserStore) CreateUser

func (s *UserStore) CreateUser(ctx context.Context, u gouncer.User) error

CreateUser stores a new user, or answers gouncer.ErrEmailTaken when the email is already in use.

func (*UserStore) DeleteExpiredSessions

func (s *UserStore) DeleteExpiredSessions(ctx context.Context, now time.Time) (int64, error)

DeleteExpiredSessions removes the sessions that expired at or before now and returns how many it removed.

func (*UserStore) DeleteExpiredTokens

func (s *UserStore) DeleteExpiredTokens(ctx context.Context, now time.Time) (int64, error)

DeleteExpiredTokens removes the expired tokens and the unconfirmed accounts an expired invite leaves behind.

func (*UserStore) DeleteSession

func (s *UserStore) DeleteSession(ctx context.Context, tokenHash []byte) error

DeleteSession removes the session with the token hash, if any.

func (*UserStore) GrantRoleToRoleless

func (s *UserStore) GrantRoleToRoleless(ctx context.Context, role string) (int64, error)

GrantRoleToRoleless gives a role to every account holding none, reporting how many took it.

func (*UserStore) ListUsers

func (s *UserStore) ListUsers(ctx context.Context) ([]gouncer.User, error)

ListUsers returns every user ordered by name regardless of ASCII case, then by id, without password hashes.

func (*UserStore) ReplaceToken

func (s *UserStore) ReplaceToken(ctx context.Context, t gouncer.Token) error

ReplaceToken stores t for an enabled account in place of every token it holds for the same purpose.

func (*UserStore) ResetByToken

func (s *UserStore) ResetByToken(
	ctx context.Context,
	tokenHash []byte,
	now time.Time,
	passwordHash string,
) (uuid.UUID, error)

ResetByToken spends the reset token, stores the password hash and ends every session of the account it names.

func (*UserStore) SetUserDisabled

func (s *UserStore) SetUserDisabled(ctx context.Context, id uuid.UUID, disabled bool) error

SetUserDisabled writes whether the user may log in, ending its sessions and tokens on a disable.

func (*UserStore) SetUserDisabledFrom

func (s *UserStore) SetUserDisabledFrom(
	ctx context.Context,
	id uuid.UUID,
	held string,
	disabled bool,
	privileged gouncer.Roles,
) error

SetUserDisabledFrom disables an account still holding held, refusing to leave no enabled account privileged.

func (*UserStore) SetUserDisabledUnderCover

func (s *UserStore) SetUserDisabledUnderCover(
	ctx context.Context,
	id uuid.UUID,
	disabled bool,
	privileged gouncer.Roles,
) error

SetUserDisabledUnderCover disables an account, refusing to leave no enabled account privileged.

func (*UserStore) SetUserRole

func (s *UserStore) SetUserRole(ctx context.Context, id uuid.UUID, role string, privileged gouncer.Roles) error

SetUserRole writes an account's role, refusing to leave no enabled account under a privileged one.

func (*UserStore) SetUserRoleFrom

func (s *UserStore) SetUserRoleFrom(
	ctx context.Context,
	id uuid.UUID,
	held, role string,
	privileged gouncer.Roles,
) error

SetUserRoleFrom writes the role of an account still holding held, refusing to leave no enabled account privileged.

func (*UserStore) UserByEmail

func (s *UserStore) UserByEmail(ctx context.Context, email string) (gouncer.User, error)

UserByEmail returns the user owning the email, or gouncer.ErrUserNotFound when none does.

func (*UserStore) UserByID

func (s *UserStore) UserByID(ctx context.Context, id uuid.UUID) (gouncer.User, error)

UserByID returns the user with the id, disabled or not, or gouncer.ErrUserNotFound.

func (*UserStore) UserBySession

func (s *UserStore) UserBySession(ctx context.Context, tokenHash []byte, now time.Time) (gouncer.User, error)

UserBySession returns the enabled user owning an unexpired session with the hash, or gouncer.ErrSessionNotFound.

func (*UserStore) UsersByIDs added in v0.2.0

func (s *UserStore) UsersByIDs(ctx context.Context, ids []uuid.UUID) ([]gouncer.User, error)

UsersByIDs returns each user an id names once, disabled or unconfirmed too, without password hashes, in no set order.

Directories

Path Synopsis
internal
db

Jump to

Keyboard shortcuts

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