service

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 38 Imported by: 0 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 service implements signing requests, the ceremony state machine, events, webhooks and artifacts on any storage.Store.

Index

Constants

View Source
const (
	// LinkTTL is how long a signing link can be opened after it is created.
	LinkTTL = 10 * time.Minute
	// CeremonyTTL is how long an opened ceremony session stays valid.
	CeremonyTTL = 30 * time.Minute
	// CodeTTL is how long an emailed code can be entered.
	CodeTTL = 10 * time.Minute
	// CodeAttempts is the number of guesses allowed per code.
	CodeAttempts = 5
	// CodeSends is the number of codes that can be sent per session.
	CodeSends = 3
	// MaxSigners is the largest number of signers on one request.
	MaxSigners = 10
	// WebhookWindow is how long a webhook delivery is retried before it
	// is marked failed.
	WebhookWindow = 72 * time.Hour
	// AssuranceCode is the assurance level that verifies the signer's email
	// address with a one-time code.
	AssuranceCode = "email_code"
)
View Source
const CurrentConsent = "v1"

CurrentConsent is the default consent text version.

Variables

This section is empty.

Functions

func ConsentText

func ConsentText(version, locale string) string

ConsentText returns the published consent text.

func RequestID

func RequestID(ctx context.Context) string

RequestID returns the request id in ctx, or "".

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

WithRequestID returns ctx carrying a request id for log correlation.

Types

type ApplicationSettings

type ApplicationSettings struct {
	Name           string
	BrandColor     string
	WebhookURL     string
	AllowedOrigins []string
}

ApplicationSettings are the editable settings of an application.

type Ceremony

type Ceremony struct {
	Nonce       string
	AppName     string
	BrandColor  string
	Locale      string
	SignerName  string
	MaskedEmail string
	RequestID   string
	SignerState string
	// Outcome is "signed" or "declined" once the session is finished.
	Outcome string
	// ReturnURL is where to send the signer after the outcome.
	ReturnURL string
	// Challenge is none, pending, passed, locked or expired.
	Challenge      string
	CodeSendsLeft  int
	AttemptsLeft   int
	DocumentSHA256 string
	Pages          int
	ConsentVersion string
	ConsentText    string
	// Open is true when the signer may act now.
	Open bool
	// Closed explains why the signer cannot act, if Open is false.
	Closed string
}

Ceremony is what the signing pages render.

type Config

type Config struct {
	// BaseURL is the public origin used in signing links.
	BaseURL string
	// Secret keys the HMAC that protects emailed codes.
	Secret []byte
	// Keys are the seal and evidence signing keys.
	Keys *signer.Keys
	// TSAURL is the RFC 3161 timestamp authority.
	TSAURL string
	// Blobs holds document and artifact bytes.
	Blobs blob.Store
	// Mailer sends signing codes.
	Mailer mail.Mailer
	// PDFLimits bounds uploaded documents; zero uses pdf.DefaultLimits.
	PDFLimits pdf.Limits
	// HTTP is the client for timestamp requests; zero uses a client with a
	// 15 second timeout.
	HTTP *http.Client
	// Logger receives service logs; zero uses slog.Default.
	Logger *slog.Logger
	// Development lets webhooks reach loopback and link-local addresses.
	// Production refuses them at dial time.
	Development bool
}

Config holds the service's dependencies and settings.

type Credentials

type Credentials struct {
	ApplicationID string `json:"application_id"`
	APIKey        string `json:"api_key,omitempty"`
	WebhookSecret string `json:"webhook_secret,omitempty"`
}

Credentials are the secrets of an application. They are returned once and stored only as hashes (API key) or for signing (webhook secret).

type Error

type Error struct {
	Status  int
	Code    string
	Message string
}

Error is an API error with a stable code.

func AsError

func AsError(err error) *Error

AsError maps any error to an API error, hiding internal details.

func (*Error) Error

func (e *Error) Error() string

type Service

type Service struct {

	// Now is the clock.
	Now func() time.Time
	// contains filtered or unexported fields
}

Service implements the signing workflow on a storage.Store.

func New

func New(db storage.Store, cfg Config) *Service

New returns a Service on db.

func (*Service) Applications

func (s *Service) Applications(ctx context.Context) ([]storage.Application, error)

Applications lists all applications.

func (*Service) Artifact

func (s *Service) Artifact(ctx context.Context, app, id, name string) ([]byte, error)

Artifact returns final.pdf or evidence.zip of a completed request.

func (*Service) Authenticate

func (s *Service) Authenticate(ctx context.Context, apiKey string) (string, error)

Authenticate resolves a bearer API key to its application.

func (*Service) Ceremony

func (s *Service) Ceremony(ctx context.Context, secret string) (*Ceremony, error)

Ceremony loads what the signing pages render for the browser secret.

func (*Service) CreateApplication

func (s *Service) CreateApplication(ctx context.Context, a ApplicationSettings) (*Credentials, error)

CreateApplication registers an API client and returns its secrets once.

func (*Service) CreateDocument

func (s *Service) CreateDocument(ctx context.Context, app string, b []byte, expectedSHA256 string) (*client.Document, error)

CreateDocument stores an immutable PDF after checking its digest and limits.

func (*Service) CreateRequest

func (s *Service) CreateRequest(ctx context.Context, app string, in *client.CreateSigningRequest) (*client.SigningRequest, error)

CreateRequest stores a draft signing request. It fills defaults into in.

func (*Service) CreateSession

func (s *Service) CreateSession(ctx context.Context, app, id, ref string, assertion json.RawMessage, returnURL string) (*client.Session, error)

CreateSession returns a one-use link for an authenticated signer.

func (*Service) DeliverWebhooks

func (s *Service) DeliverWebhooks(ctx context.Context) error

DeliverWebhooks sends due deliveries, up to eight at a time. Deliveries of one request go out in sequence order because only the first pending delivery of a request is due. request.completed waits until the artifacts exist. Each delivery is claimed with a lease before sending, so instances never send it twice at once.

func (*Service) Document

func (s *Service) Document(ctx context.Context, secret, ip, ua string) ([]byte, error)

Document returns the exact stored PDF for review after the code passed. Serving it records signer.viewed the first time, which signing requires.

func (*Service) ExpireDue

func (s *Service) ExpireDue(ctx context.Context) error

ExpireDue moves open requests past expires_at to expired. A request that fails is logged and retried later with a growing delay; the others are still processed.

func (*Service) FailedDeliveries

func (s *Service) FailedDeliveries(ctx context.Context, app string) ([]client.WebhookDelivery, error)

FailedDeliveries lists webhook deliveries that exhausted their retries.

func (*Service) Finalize

func (s *Service) Finalize(ctx context.Context, id string) error

Finalize produces final.pdf (summary page and seal in one incremental update) and evidence.zip for a completed request. It is safe to call repeatedly and concurrently: the first stored artifacts win.

func (*Service) GetRequest

func (s *Service) GetRequest(ctx context.Context, app, id string) (*client.SigningRequest, error)

GetRequest returns the application's signing request id.

func (*Service) IdempotencyBegin

func (s *Service) IdempotencyBegin(ctx context.Context, app, key string, reqHash []byte) (*Stored, error)

IdempotencyBegin claims key for this request. It returns the stored response when the same request already completed.

func (*Service) IdempotencyEnd

func (s *Service) IdempotencyEnd(ctx context.Context, app, key string, status int, body []byte) error

IdempotencyEnd records the response, or releases the key after a server error or a 409 conflict so the client can retry. It completes even if the client has already gone away, so a key is never left in progress.

func (*Service) Issue

func (s *Service) Issue(ctx context.Context, app, id string) (*client.SigningRequest, error)

Issue freezes a draft and makes the first signers ready.

func (*Service) Keys

func (s *Service) Keys() *signer.Keys

Keys returns the configured signing keys.

func (*Service) LinkNonce

func (s *Service) LinkNonce(tok string) string

LinkNonce returns the form nonce that protects consuming the link token tok.

func (*Service) ListRequests

func (s *Service) ListRequests(ctx context.Context, app string, since time.Time, cursor string) (*client.SigningRequestList, error)

ListRequests returns requests updated at or after since, ordered by (updated_at, id), with an opaque cursor.

func (*Service) Logger

func (s *Service) Logger() *slog.Logger

Logger returns the service's logger.

func (*Service) OpenSession

func (s *Service) OpenSession(ctx context.Context, tok, ip, ua string) (string, error)

OpenSession consumes a session link and returns the browser secret that carries the ceremony from here on.

func (s *Service) PeekLink(ctx context.Context, tok string) error

PeekLink reports whether the link token tok can still be opened, without consuming it.

func (*Service) Report

func (s *Service) Report(ctx context.Context, route string, err error) *Error

Report maps err like AsError and logs it server-side, with the request id in ctx and the route, when it is not a deliberate service error.

func (*Service) Revoke

func (s *Service) Revoke(ctx context.Context, app, id, reason string) (*client.SigningRequest, error)

Revoke ends an open request.

func (*Service) RotateAPIKey

func (s *Service) RotateAPIKey(ctx context.Context, id string) (*Credentials, error)

RotateAPIKey revokes the application's API keys and returns a new one.

func (*Service) RotateWebhookSecret

func (s *Service) RotateWebhookSecret(ctx context.Context, id string, overlap time.Duration) (*Credentials, error)

RotateWebhookSecret adds a new webhook secret. Current secrets keep signing deliveries for overlap, so receivers can switch without gaps.

func (*Service) Run

func (s *Service) Run(ctx context.Context, every time.Duration)

Run executes the background jobs every interval until ctx ends. Expiry, sealing and webhook delivery run independently, so a slow job does not delay the others.

func (*Service) SendCode

func (s *Service) SendCode(ctx context.Context, secret string) error

SendCode emails a new 6-digit code to the signer's own address.

func (*Service) Submit

func (s *Service) Submit(ctx context.Context, secret string, sub *Submission) error

Submit signs or declines in one transaction that rechecks every precondition and writes the session, the challenge and the request only if none of them changed since they were read. A retry with the same session returns the first outcome.

func (*Service) Tick

func (s *Service) Tick(ctx context.Context)

Tick runs every job once.

func (*Service) UpdateApplication

func (s *Service) UpdateApplication(ctx context.Context, id string, edit func(*ApplicationSettings)) error

UpdateApplication changes an application's settings; edit receives the current settings.

func (*Service) VerifyCode

func (s *Service) VerifyCode(ctx context.Context, secret, code string) error

VerifyCode checks a code. Each attempt counts, even a wrong one, and a signer who fails too often across all their sessions is locked.

type Stored

type Stored struct {
	Status int
	Body   []byte
}

Stored is a recorded response for an idempotency key.

type Submission

type Submission struct {
	// Nonce is the form nonce of the session.
	Nonce string
	// Decline declines instead of signing.
	Decline bool
	// Consent is whether the signer ticked the consent box.
	Consent bool
	// DocumentSHA256 is the digest of the document the signer reviewed.
	DocumentSHA256 string
	// Reason is the optional decline reason.
	Reason string
	// IP and UserAgent are recorded in the signing event.
	IP, UserAgent string
}

Submission is a signer's form submission with the request metadata that is recorded as evidence.

Jump to

Keyboard shortcuts

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