storage

package
v0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package storage defines the persistence interface the Inkless service uses and a registry of drivers selected by URL scheme. Drivers register themselves from init, as database/sql drivers do; import them for their side effect (for example storage/sqlite).

Drivers must implement optimistic concurrency: updates of requests, sessions, challenges and webhook deliveries succeed only when the stored version (or the stated previous value) is unchanged, and fail with ErrConflict otherwise. Uniqueness of ids, event sequence numbers and idempotency keys comes from unique constraints.

Index

Constants

View Source
const DefaultURL = "sqlite:inkless.db"

DefaultURL is used when no database URL is configured.

Variables

View Source
var (
	// ErrNotFound is returned when a looked-up record does not exist.
	ErrNotFound = errors.New("storage: not found")
	// ErrConflict means a concurrent change won; the caller retries the
	// whole transaction.
	ErrConflict = errors.New("storage: conflict")
)

Functions

func Drivers

func Drivers() []string

Drivers returns the registered schemes.

func Register

func Register(scheme string, open Opener)

Register makes a driver available for URLs with the given scheme. It panics if the scheme is already registered.

Types

type APIKey

type APIKey struct {
	Hash          []byte
	ApplicationID string
	CreatedAt     time.Time
}

type Application

type Application struct {
	ID             string
	Name           string
	BrandColor     string
	WebhookURL     string
	AllowedOrigins []string
	CreatedAt      time.Time
}

type Artifacts

type Artifacts struct {
	RequestID string
	FinalPDF  []byte
	Evidence  []byte
	CreatedAt time.Time
}

type Challenge

type Challenge struct {
	ID        string
	SessionID string
	CodeHash  []byte
	CreatedAt time.Time
	ExpiresAt time.Time
	Attempts  int
	PassedAt  *time.Time
	UsedAt    *time.Time
	Version   int64
}

type Delivery

type Delivery struct {
	EventID       string
	ApplicationID string
	RequestID     string
	Type          string
	Seq           int64
	Payload       []byte
	Attempts      int
	NextAttemptAt time.Time
	CreatedAt     time.Time
	DeliveredAt   *time.Time
	FailedAt      *time.Time
	LastError     string
}

type Document

type Document struct {
	ID            string
	ApplicationID string
	SHA256        string
	Size          int64
	Pages         int
	CreatedAt     time.Time
}

type Event

type Event struct {
	ID        string
	RequestID string
	Seq       int64
	Type      string
	At        time.Time
	Raw       []byte
}

type Idempotency

type Idempotency struct {
	ApplicationID string
	Key           string
	RequestHash   []byte
	// Status is 0 while the first request is in progress.
	Status    int
	Response  []byte
	CreatedAt time.Time
}

type Opener

type Opener func(ctx context.Context, url string) (Store, error)

Opener opens a store from a URL whose scheme selected the driver. It creates or migrates the schema as needed.

type Request

type Request struct {
	ID             string
	ApplicationID  string
	DocumentID     string
	State          string
	Routing        string
	Assurance      string
	ConsentVersion string
	Locale         string
	ExpiresAt      time.Time
	ExternalRef    string
	Metadata       []byte
	Sequence       int64
	LastHash       string
	RevokeReason   string
	CreatedAt      time.Time
	UpdatedAt      time.Time
	IssuedAt       *time.Time
	EndedAt        *time.Time
	// Signers are ordered by (Order, Ref).
	Signers []Signer
	Version int64
}

type Session

type Session struct {
	ID                string
	TokenHash         []byte
	RequestID         string
	SignerRef         string
	Assertion         []byte
	ReturnURL         string
	Nonce             string
	CreatedAt         time.Time
	LinkExpiresAt     time.Time
	OpenedAt          *time.Time
	BrowserHash       []byte
	CeremonyExpiresAt *time.Time
	IP                string
	UserAgent         string
	CodeSends         int
	Outcome           string
	OutcomeAt         *time.Time
	Version           int64
}

type Signer

type Signer struct {
	Ref           string
	Name          string
	Email         string
	Order         int
	State         string
	SignedAt      *time.Time
	DeclinedAt    *time.Time
	DeclineReason string
}

type Store

type Store interface {
	// Tx runs fn in one transaction and commits when fn returns nil.
	// Every read and write happens through a Tx. Calls must not nest.
	Tx(ctx context.Context, fn func(Tx) error) error
	Close() error
}

Store is a database holding Inkless state.

func Open

func Open(ctx context.Context, url string) (Store, error)

Open opens the store for url (DefaultURL if empty) with the driver registered for its scheme.

type Tx

type Tx interface {
	InsertApplication(ctx context.Context, a Application) error
	Application(ctx context.Context, id string) (*Application, error)
	Applications(ctx context.Context) ([]Application, error)
	UpdateApplication(ctx context.Context, a Application) error
	InsertAPIKey(ctx context.Context, k APIKey) error
	// APIKeyApplication returns the application of an unrevoked key hash.
	APIKeyApplication(ctx context.Context, hash []byte) (string, error)
	RevokeAPIKeys(ctx context.Context, appID string, at time.Time) error
	InsertWebhookSecret(ctx context.Context, s WebhookSecret) error
	// WebhookSecrets returns the application's secrets that have not expired
	// at now, oldest first.
	WebhookSecrets(ctx context.Context, appID string, now time.Time) ([]WebhookSecret, error)
	ExpireWebhookSecrets(ctx context.Context, appID string, at time.Time) error

	InsertDocument(ctx context.Context, d Document, content []byte) error
	Document(ctx context.Context, id string) (*Document, error)
	DocumentContent(ctx context.Context, id string) ([]byte, error)

	// InsertRequest stores a request and its signers with Version 1.
	InsertRequest(ctx context.Context, r *Request) error
	Request(ctx context.Context, id string) (*Request, error)
	// UpdateRequest writes r and its signers' mutable fields if the stored
	// version equals r.Version, then increments r.Version.
	UpdateRequest(ctx context.Context, r *Request) error
	// ListRequests returns ids of the application's requests ordered by
	// (UpdatedAt, ID) strictly after (after, afterID).
	ListRequests(ctx context.Context, appID string, after time.Time, afterID string, limit int) ([]string, error)
	// OverdueRequests returns ids of issued or partially signed requests
	// whose ExpiresAt is not after now.
	OverdueRequests(ctx context.Context, now time.Time, limit int) ([]string, error)
	// UnsealedRequests returns ids of completed requests without artifacts.
	UnsealedRequests(ctx context.Context, limit int) ([]string, error)

	InsertEvents(ctx context.Context, events []Event) error
	// Events returns a request's events in sequence order.
	Events(ctx context.Context, requestID string) ([]Event, error)

	InsertDeliveries(ctx context.Context, ds []Delivery) error
	// DueDeliveries returns pending deliveries with NextAttemptAt not after
	// now that are first in sequence among their request's pending
	// deliveries; request.completed only once the request has artifacts.
	DueDeliveries(ctx context.Context, now time.Time, limit int) ([]Delivery, error)
	// UpdateDelivery writes d if it is still pending and its stored
	// NextAttemptAt equals prevNextAttempt.
	UpdateDelivery(ctx context.Context, d *Delivery, prevNextAttempt time.Time) error
	FailedDeliveries(ctx context.Context, appID string, limit int) ([]Delivery, error)

	InsertSession(ctx context.Context, s *Session) error
	SessionByToken(ctx context.Context, tokenHash []byte) (*Session, error)
	SessionByBrowser(ctx context.Context, browserHash []byte) (*Session, error)
	// UpdateSession writes s if the stored version equals s.Version, then
	// increments s.Version.
	UpdateSession(ctx context.Context, s *Session) error
	InsertChallenge(ctx context.Context, c *Challenge) error
	// LatestChallenge returns the session's most recent challenge.
	LatestChallenge(ctx context.Context, sessionID string) (*Challenge, error)
	// UpdateChallenge writes c if the stored version equals c.Version, then
	// increments c.Version.
	UpdateChallenge(ctx context.Context, c *Challenge) error

	// InsertIdempotency stores k unless the (AppID, Key) pair exists, and
	// reports whether it was inserted.
	InsertIdempotency(ctx context.Context, k Idempotency) (bool, error)
	Idempotency(ctx context.Context, appID, key string) (*Idempotency, error)
	CompleteIdempotency(ctx context.Context, appID, key string, status int, response []byte) error
	DeleteIdempotency(ctx context.Context, appID, key string) error
	PruneIdempotency(ctx context.Context, before time.Time) error

	// InsertArtifacts stores a's files unless the request already has
	// artifacts, which are kept.
	InsertArtifacts(ctx context.Context, a Artifacts) error
	Artifacts(ctx context.Context, requestID string) (*Artifacts, error)
	HasArtifacts(ctx context.Context, requestID string) (bool, error)

	// Setting returns a named service setting, such as the server secret
	// or published key material.
	Setting(ctx context.Context, name string) ([]byte, error)
	// InsertSetting stores value unless name exists, and reports whether
	// it was inserted.
	InsertSetting(ctx context.Context, name string, value []byte) (bool, error)
	// PutSetting stores value, replacing any existing one.
	PutSetting(ctx context.Context, name string, value []byte) error
}

Tx is the set of operations available inside a transaction. Methods returning a single record return ErrNotFound when it does not exist.

type WebhookSecret

type WebhookSecret struct {
	ApplicationID string
	Secret        string
	CreatedAt     time.Time
	ExpiresAt     *time.Time
}

Directories

Path Synopsis
Package mysql registers the storage driver for mysql://user:password@host:port/database URLs, using go-sql-driver/mysql.
Package mysql registers the storage driver for mysql://user:password@host:port/database URLs, using go-sql-driver/mysql.
Package postgres registers the storage driver for postgres:// and postgresql:// URLs, using pgx.
Package postgres registers the storage driver for postgres:// and postgresql:// URLs, using pgx.
Package sqlite registers the default storage driver for URLs of the form sqlite:PATH (sqlite:inkless.db, sqlite:///var/lib/inkless.db).
Package sqlite registers the default storage driver for URLs of the form sqlite:PATH (sqlite:inkless.db, sqlite:///var/lib/inkless.db).
Package sqlstore implements storage.Store on database/sql.
Package sqlstore implements storage.Store on database/sql.
Package storagetest is the conformance suite for storage drivers.
Package storagetest is the conformance suite for storage drivers.

Jump to

Keyboard shortcuts

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