storage

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeySize is returned when the symmetric key for the database is the incorrect size.
	ErrKeySize = errors.New("symmetric key for database is incorrect size")
	// ErrNotFound is returned when a record is not found.
	ErrNotFound = errors.New("not found")
)
View Source
var (
	// ErrPostgresSetupCheck is the error returned when the Postgres setup check fails.
	ErrPostgresSetupCheck = errors.New("failed to perform Postgres setup check")
)
View Source
var (
	// ErrSemVer indicates a semver is invalid.
	ErrSemVer = errors.New("invalid semver")
)

Functions

func DecodeAES256Base64 added in v0.5.1

func DecodeAES256Base64(aes256KeyBase64 string) ([32]byte, error)

DecodeAES256Base64 decodes a Base64 encoded AES256 key.

Types

type Config added in v0.5.1

type Config struct {
	AES256KeyBase64 string                      `json:"aes256KeyBase64"`
	AutoMigrate     bool                        `json:"autoMigrate"`
	DSN             string                      `json:"dsn"`
	Health          *jt.JSONType[time.Duration] `json:"health"`
	InitialTimeout  *jt.JSONType[time.Duration] `json:"initialTimeout"`
	MaxIdle         *jt.JSONType[time.Duration] `json:"maxIdle"`
	MinConns        int32                       `json:"minConns"`
	PlaintextClaims bool                        `json:"plaintextClaims"`
	PlaintextJWK    bool                        `json:"plaintextJWK"`
}

Config is the configuration for Postgres storage.

func (Config) DefaultsAndValidate added in v0.5.1

func (c Config) DefaultsAndValidate() (Config, error)

DefaultsAndValidate implements the jsontype.Config interface.

type Migrator added in v0.5.1

type Migrator interface {
	// Migrate applies all migrations in order. It will automatically skip migrations that have .
	Migrate(ctx context.Context) error
}

Migrator is the interface for applying migrations.

Migrator only migrates in the forward direction. It does not support rolling back migrations. Ensure you have quick and robust database backup and restore procedure before running migrations.

No other program should interact with the database while the Migrator is running.

No Migrator implementation should depend on code elsewhere in this project.

func NewMigrator added in v0.5.1

func NewMigrator(pool *pgxpool.Pool, options MigratorOptions) (Migrator, error)

NewMigrator returns a new Migrator for a Postgres storage implementation.

type MigratorOptions added in v0.5.1

type MigratorOptions struct {
	EncryptionKey [32]byte
	Logger        *slog.Logger
	SetupCtx      context.Context
}

MigratorOptions are options for creating a Migrator.

type ReadSigningKeyOptions

type ReadSigningKeyOptions struct {
	JWTAlg string
}

ReadSigningKeyOptions are the options for the SigningKeyRead method.

type Setup added in v0.5.1

type Setup struct {
	PlaintextClaims bool   `json:"plaintextClaims,omitempty"`
	PlaintextJWK    bool   `json:"plaintextJWK,omitempty"`
	SemVer          string `json:"semver,omitempty"` // https://pkg.go.dev/golang.org/x/mod/semver
}

Setup is the JSON data that sits in the setup table.

func ReadSetup added in v0.5.1

func ReadSetup(ctx context.Context, tx pgx.Tx) (Setup, error)

ReadSetup reads the setup information in the database.

type Storage

type Storage interface {
	Begin(ctx context.Context) (Tx, error)
	Close(ctx context.Context) error
	TestingTruncate(ctx context.Context) error

	SAAdminCreate(ctx context.Context, args model.ValidAdminCreateParams) error
	SACreate(ctx context.Context, args model.ValidServiceAccountCreateParams) (model.ServiceAccount, error)
	SARead(ctx context.Context, u uuid.UUID) (model.ServiceAccount, error)
	SAReadFromAPIKey(ctx context.Context, apiKey uuid.UUID) (model.ServiceAccount, error)
	SigningKeyRead(ctx context.Context, options ReadSigningKeyOptions) (jwk jwkset.JWK, err error)
	SigningKeyDefaultRead(ctx context.Context) (jwk jwkset.JWK, err error)
	SigningKeyDefaultUpdate(ctx context.Context, keyID string) error

	jwkset.Storage
	magiclink.Storage
	otp.Storage
}

Storage is the interface for magiclinksdev storage.

func New added in v0.5.1

func New(ctx context.Context, config Config) (Storage, *pgxpool.Pool, error)

New creates a new Postgres storage and returns its connection pool.

func NewWithSetup added in v0.5.1

func NewWithSetup(ctx context.Context, config Config, setupLogger *slog.Logger) (Storage, *pgxpool.Pool, error)

NewWithSetup creates a new Postgres storage and returns its connection pool. It also performs a setup check.

type Transaction added in v0.5.1

type Transaction struct {
	Tx pgx.Tx
}

Transaction is a storage.Tx implementation for Postgres.

func (*Transaction) Commit added in v0.5.1

func (t *Transaction) Commit(ctx context.Context) error

Commit helps implement the storage.Tx interface.

func (*Transaction) Rollback added in v0.5.1

func (t *Transaction) Rollback(ctx context.Context) error

Rollback helps implement the storage.Tx interface.

type Tx

type Tx interface {
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

Tx is the interface for a transaction.

Jump to

Keyboard shortcuts

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