Help us shape the future of Go! Take the 2026 Go Developer Survey

postgres

package module
v0.14.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: 20 Imported by: 3

Documentation

Overview

Package postgres implements gouncer's user and session persistence in a PostgreSQL schema of its own.

Index

Constants

This section is empty.

Variables

View Source
var Migrations embed.FS

Migrations holds the auth schema migration files applied by goose.

Functions

func Migrate

func Migrate(ctx context.Context, databaseURL string) error

Migrate applies the auth schema migrations to the database at databaseURL.

func RunCreateAdmin added in v0.2.0

func RunCreateAdmin(ctx context.Context, databaseURL string, args []string, stdin io.Reader, stdout io.Writer) error

RunCreateAdmin provisions a user account from command-line arguments, reading the password as one line from stdin.

func RunGrantRole added in v0.7.0

func RunGrantRole(ctx context.Context, databaseURL string, args []string, stdout io.Writer) error

RunGrantRole gives a role to every account holding none, from command-line arguments.

Types

type UserStore

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

UserStore persists users and their sessions in the auth schema.

func NewUserStore

func NewUserStore(pool *pgxpool.Pool) *UserStore

NewUserStore returns a UserStore backed by pool.

func (*UserStore) ActivateByToken added in v0.9.0

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

ActivateByToken spends the invite token, storing the password hash and confirming the account it names, answering the account's id, gouncer.ErrTokenNotFound for a spent or expired token, and gouncer.ErrUserNotFound for a disabled or confirmed account.

func (*UserStore) CreateSession

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

CreateSession stores a login session.

func (*UserStore) CreateToken added in v0.9.0

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 the same user and purpose, or returns gouncer.ErrTokenExists at the cap and gouncer.ErrUserNotFound for a disabled one.

func (*UserStore) CreateUser

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

CreateUser stores a new user, or 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 sessions that expired at or before now and returns how many it removed.

func (*UserStore) DeleteExpiredTokens added in v0.9.0

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

DeleteExpiredTokens removes expired tokens and the unconfirmed accounts an expired invite leaves behind, reporting how many tokens went.

func (*UserStore) DeleteSession

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

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

func (*UserStore) GrantRoleToRoleless added in v0.7.0

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 added in v0.9.0

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, or returns gouncer.ErrUserNotFound for a disabled one and for an activated one under gouncer.PurposeInvite, replacing nothing when it refuses.

func (*UserStore) ResetByToken added in v0.9.0

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

ResetByToken spends the reset token, storing the password hash and ending every session the account it names holds, answering the account's id, gouncer.ErrTokenNotFound for a spent or expired token, and gouncer.ErrUserNotFound for a disabled account.

func (*UserStore) SetUserDisabled

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

SetUserDisabled updates whether the user may log in, revoking every session the user holds when disabling so a later re-enable cannot resurrect them, or returns gouncer.ErrUserNotFound when no such user exists.

func (*UserStore) SetUserDisabledFrom added in v0.13.0

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 added in v0.5.0

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 added in v0.7.0

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 added in v0.13.0

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 if none exists.

func (*UserStore) UserByID added in v0.3.0

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

UserByID returns the user with the given 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 given token hash, or gouncer.ErrSessionNotFound.

func (*UserStore) UsersByIDs added in v0.14.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.

func (*UserStore) Within added in v0.11.0

func (s *UserStore) Within(tx pgx.Tx) *UserStore

Within returns a UserStore running every call inside tx.

Directories

Path Synopsis
internal
db
Package testdb provides pgtestdb wiring for tests against the module's auth schema.
Package testdb provides pgtestdb wiring for tests against the module's auth schema.

Jump to

Keyboard shortcuts

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