database

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package database provides types and functions for managing the persistence layer of the password manager.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidKey is the error given when attempting to decrypt an individual account database with an invalid
	// key.
	ErrInvalidKey = errors.New("invalid key")
	// ErrDatabaseExists is the error given when attempting to create an individual account database where one already
	// exists.
	ErrDatabaseExists = errors.New("database exists")
)
View Source
var (
	// ErrAccountExists is the error given when performing an operation for an account that conflicts with an existing
	// account record.
	ErrAccountExists = errors.New("account exists")
	// ErrAccountNotFound is the error given when querying an account that does not exist.
	ErrAccountNotFound = errors.New("account not found")
)
View Source
var (
	// ErrCardNotFound is the error given when performing an operation on a card record that does not exist.
	ErrCardNotFound = errors.New("card not found")
)
View Source
var (
	// ErrClosed is the error given when calling RepositoryProvider.For with a user identifier that has no open
	// database.
	ErrClosed = errors.New("closed")
)
View Source
var (
	// ErrLoginNotFound is the error given when performing an operation on a login record that does not exist.
	ErrLoginNotFound = errors.New("login not found")
)
View Source
var (
	// ErrNoteNotFound is the error given when performing an operation on a note record that does not exist.
	ErrNoteNotFound = errors.New("note not found")
)

Functions

func Open

func Open(path string, key []byte) (*badger.DB, error)

Open a new badger database at the specified path, encrypting it with the given key.

Types

type Account

type Account struct {
	// The user's unique identifier.
	ID uuid.UUID
	// The user's email address.
	Email string
	// The user's hashed password.
	PasswordHash []byte
	// The user's display name.
	DisplayName string
}

The Account type represents a user account as stored in the master database.

type AccountRepository

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

The AccountRepository type is responsible for managing the persistence of individual user accounts. This should be instantiated against the master database, as that is where metadata for accounts is stored. Actual account data, such as secrets etc should be stored within their respective, encrypted user databases.

func NewAccountRepository

func NewAccountRepository(db *badger.DB) *AccountRepository

NewAccountRepository returns a new instance of the AccountRepository type that will persist account records using the provided badger.DB database.

func (*AccountRepository) Create

func (r *AccountRepository) Create(account Account) error

Create a new account record. Returns ErrAccountExists if an account already exists with the same email address.

func (*AccountRepository) Delete

func (r *AccountRepository) Delete(id uuid.UUID) error

Delete the account associated with the given id. Returns ErrAccountNotFound if the specified account does not exist.

func (*AccountRepository) FindByEmail

func (r *AccountRepository) FindByEmail(email string) (Account, error)

FindByEmail attempts to return the account record associated with the given email address. Returns ErrAccountNotFound if the specified account does not exist.

func (*AccountRepository) FindByID

func (r *AccountRepository) FindByID(id uuid.UUID) (Account, error)

FindByID attempts to return the account record associated with the given identifier address. Returns ErrAccountNotFound if the specified account does not exist.

func (*AccountRepository) Update

func (r *AccountRepository) Update(account Account) error

Update an account record with the one provided. Returns ErrAccountNotFound if an account with the same identifier does not exist.

type Card

type Card struct {
	// The card's unique identifier.
	ID uuid.UUID
	// The cardholder's name.
	HolderName string
	// The card number.
	Number string
	// The month the card expires.
	ExpiryMonth time.Month
	// The year the card expires.
	ExpiryYear int
	// The card's CVV.
	CVV string
	// When the card was created.
	CreatedAt time.Time
	// A user-supplied name for the card.
	Name string
	// The card issuer.
	Issuer string
}

The Card type represents a single payment card as stored in a user's individual database.

type CardRepository

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

The CardRepository type is responsible for managing the persistence of user's payment cards. This should be instantiated against a user's individual database.

func NewCardRepository

func NewCardRepository(db *badger.DB) *CardRepository

NewCardRepository returns a new instance of the CardRepository type that will persist card data using the provided badger.DB database.

func (*CardRepository) Create

func (r *CardRepository) Create(card Card) error

Create a new card record.

func (*CardRepository) Delete

func (r *CardRepository) Delete(id uuid.UUID) error

Delete a card record, returns ErrCardNotFound if the card record does not exist.

func (*CardRepository) Get

func (r *CardRepository) Get(id uuid.UUID) (Card, error)

Get a card record by its id, returns ErrCardNotFound if the card record does not exist.

func (*CardRepository) List

func (r *CardRepository) List() ([]Card, error)

List all card records.

type Login

type Login struct {
	// The login's unique identifier.
	ID uuid.UUID
	// The username associated with the login.
	Username string
	// The login.
	Password string
	// The domains where this username and login combination can be used.
	Domains []string
	// When the login was created.
	CreatedAt time.Time
	// A user-supplied name for the login.
	Name string
}

The Login type represents a username & password combination as stored in a user's individual database.

type LoginRepository

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

The LoginRepository type is responsible for managing the persistence of user logins. This should be instantiated against a user's individual database.

func NewLoginRepository

func NewLoginRepository(db *badger.DB) *LoginRepository

NewLoginRepository returns a new instance of the LoginRepository type that will persist login data using the provided badger.DB database.

func (*LoginRepository) Create

func (r *LoginRepository) Create(login Login) error

Create a new login record.

func (*LoginRepository) Delete

func (r *LoginRepository) Delete(id uuid.UUID) error

Delete a login record, returns ErrLoginNotFound if the login record does not exist.

func (*LoginRepository) Get

func (r *LoginRepository) Get(id uuid.UUID) (Login, error)

Get a login record by its id, returns ErrLoginNotFound if the login record does not exist.

func (*LoginRepository) List

func (r *LoginRepository) List() ([]Login, error)

List all login records.

type Manager

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

The Manager type is responsible for managing individual user's encrypted databases.

func NewManager

func NewManager(dir string, state State, expiration time.Duration) *Manager

NewManager returns a new instance of the Manager type that will manage individual user databases within the specified directory. Each database is wrapped within a lifetime causing it to be automatically closed after a specified expiration time.

func (*Manager) Close

func (m *Manager) Close() error

Close all open user databases.

func (*Manager) Create

func (m *Manager) Create(id uuid.UUID, key []byte) error

Create a database for a given account identifier. This database is closed upon return of this method and is not placed into the state.

func (*Manager) Delete

func (m *Manager) Delete(id uuid.UUID) error

Delete a user's encrypted database.

func (*Manager) Lock

func (m *Manager) Lock(id uuid.UUID) error

Lock immediately closes a user's encrypted database if it is open.

func (*Manager) RotateKey

func (m *Manager) RotateKey(id uuid.UUID, oldKey, newKey []byte) error

RotateKey updates the master encryption key used to encrypt individual user databases. Returns ErrInvalidKey if the old encryption key is invalid.

func (*Manager) Unlock

func (m *Manager) Unlock(id uuid.UUID, key []byte) error

Unlock opens a user's encrypted database using their encryption key. Each database remains open for the amount of time specified when calling New to create the Manager. If this method is called for a user whose database is currently open, the expiration is then reset to its original value.

type Note

type Note struct {
	// The note's unique identifier.
	ID uuid.UUID
	// The note's name.
	Name string
	// The note's contents
	Content string
	// When the note was created.
	CreatedAt time.Time
}

The Note type represents a secure note as stored in a user's individual database.

type NoteRepository

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

The NoteRepository type is responsible for managing the persistence of user notes. This should be instantiated against a user's individual database.

func NewNoteRepository

func NewNoteRepository(db *badger.DB) *NoteRepository

NewNoteRepository returns a new instance of the NoteRepository type that will persist note data using the provided badger.DB database.

func (*NoteRepository) Create

func (r *NoteRepository) Create(note Note) error

Create a new note record.

func (*NoteRepository) Delete

func (r *NoteRepository) Delete(id uuid.UUID) error

Delete a note record, returns ErrNoteNotFound if the note record does not exist.

func (*NoteRepository) Get

func (r *NoteRepository) Get(id uuid.UUID) (Note, error)

Get a note record by its id, returns ErrNoteNotFound if the note record does not exist.

func (*NoteRepository) List

func (r *NoteRepository) List() ([]Note, error)

List all note records.

type RepositoryFunc

type RepositoryFunc[T any] func(db *badger.DB) T

The RepositoryFunc type is a function that turns a badger.DB instance to an instance of the parameterized type T.

type RepositoryProvider

type RepositoryProvider[T any] struct {
	// contains filtered or unexported fields
}

The RepositoryProvider type is used to conveniently instantiate repositories for individual user databases. Each instance of the RepositoryProvider type is used to instantiate a single repository type via a RepositoryFunc implementation.

func NewRepositoryProvider

func NewRepositoryProvider[T any](state State, fn RepositoryFunc[T]) *RepositoryProvider[T]

NewRepositoryProvider returns a new instance of the RepositoryProvider type that will provide instances of the parameterized type T for individual users. Databases are obtained from the provided State implementation.

func (*RepositoryProvider[T]) For

func (m *RepositoryProvider[T]) For(id uuid.UUID) (T, error)

For returns an instance of the parameterized type T using the RepositoryFunc specified when calling New to create the RepositoryProvider. The state is checked for an existing, open database associated with the provided user identifier. If no database exists, or it has expired, this method returns ErrClosed. Callers must check for the ErrClosed error and correctly inform upstream that reauthentication is required.

type State

type State interface {
	Get(id uuid.UUID) (*lifetime.Lifetime[*badger.DB], bool)
	Put(id uuid.UUID, db *lifetime.Lifetime[*badger.DB])
	Range() iter.Seq2[uuid.UUID, *lifetime.Lifetime[*badger.DB]]
	Remove(id uuid.UUID)
}

The State interface describes types that store references to individual badgerdb instances wrapped within a lifetime.

Jump to

Keyboard shortcuts

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