sessionshare

package
v0.340.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: MIT Imports: 13 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 sessionshare is a mirrored session's SHARE LINKS (migration 0211; docs/auth.md "Sharing one session"): a link its owner makes to ONE mirrored window, which lets whoever holds it watch that window's screen and transcript — and, for a full-access link, the one account that redeemed it type into it — until the link expires, is revoked, or its window closes.

🔴 THE TOKEN IS 256 RANDOM BITS AND ONLY ITS SHA-256 IS STORED. It is shown once, to the owner who made it (Create's answer), and looked up by its hash (ByToken), compared again in constant time — the shape of a page's share link (internal/pages), whose token helpers this package uses.

🔴 A LINK THAT IS NOT LIVE IS NOT FOUND. Unknown, malformed, expired, revoked or ended: ByToken answers ErrNotFound for each, so a caller cannot tell them apart and neither can whoever holds the link.

Index

Constants

View Source
const (
	ActionCreate = "session.share.create"
	ActionRevoke = "session.share.revoke"
	ActionRedeem = "session.share.redeem"
	ActionEnd    = "session.share.end"
	// ActionGuestSend and ActionGuestAnswer: a guest typed into the session,
	// or answered one of its asks (AuditGuest).
	ActionGuestSend   = "session.share.guest.send"
	ActionGuestAnswer = "session.share.guest.answer"
	// ActionGuestPreviewHandoff: muster handed a link's holder a preview
	// hand-off on the owner's viewer plugin (internal/api session_preview.go),
	// written by muster itself when it mints one — never from a plugin's
	// report.
	ActionGuestPreviewHandoff = "session.share.guest.preview.handoff"
)

The audit actions.

View Source
const ActionGuestRemove = "session.share.guest.remove"

ActionGuestRemove is the owner's audit row for a guest muster removed.

View Source
const DefaultLifetime = 24 * time.Hour

DefaultLifetime is the choice a new link's form starts on.

View Source
const MaxLivePerWindow = 20

MaxLivePerWindow bounds the live links one window may have at once, so a form held down cannot fill the table.

View Source
const Prefix = "/w/"

Prefix begins every path a link's holder opens. The access log writes it as Prefix + "[redacted]".

Variables

View Source
var (
	// ErrNotFound: no such live link (ByToken: whatever the reason), or no such
	// link of the caller's (Revoke).
	ErrNotFound = errors.New("sessionshare: no such link")
	// ErrBadLifetime: a lifetime not in Lifetimes.
	ErrBadLifetime = errors.New("sessionshare: a link lasts 1 hour, 8 hours, 24 hours or 7 days")
	// ErrTooMany: the window already has MaxLivePerWindow live links.
	ErrTooMany = errors.New("sessionshare: this session has too many live links; revoke one first")
	// ErrNoTmuxServer: the target names no tmux server (an older host).
	ErrNoTmuxServer = errors.New("sessionshare: the host does not report its tmux server")
	// ErrAlreadyRedeemed: a full link another account already redeemed.
	ErrAlreadyRedeemed = errors.New("sessionshare: this link is already in use by someone else")
	// ErrGuestNotDue: RemoveGuest's account is not a guest, is already
	// removed, or holds a link that is live or ended within the grace.
	ErrGuestNotDue = errors.New("sessionshare: this account is not a guest due for removal")
)

The errors.

View Source
var Lifetimes = []time.Duration{time.Hour, 8 * time.Hour, 24 * time.Hour, 7 * 24 * time.Hour}

The lifetimes an owner chooses from when making a link (operator decision: 1 hour, 8 hours, 24 hours — the default — or 7 days). The set is closed: Create refuses any other.

Functions

func NewToken

func NewToken() (string, error)

NewToken is a fresh link token (256 random bits, base64url): a page link's.

func Object

func Object(id int64) string

Object is a link's audit object: "session-share:<id>".

func Path

func Path(token string) string

Path is the holder's page of token.

func TokenHash

func TokenHash(token string) string

TokenHash is a token's lookup key.

func TokenShapeOK

func TokenShapeOK(s string) bool

TokenShapeOK reports whether s is shaped like a token.

func ValidLifetime

func ValidLifetime(d time.Duration) bool

ValidLifetime reports whether d is one of Lifetimes.

Types

type Kind

type Kind string

Kind is what a link lets its holder do.

const (
	// KindRead: watch, anonymously, for whoever holds the link.
	KindRead Kind = "read"
	// KindFull: watch and type, for the ONE account that redeemed it first.
	KindFull Kind = "full"
)

func (Kind) Valid

func (k Kind) Valid() bool

Valid reports whether k is one of the two kinds.

type PGStore

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

PGStore is Store on Postgres.

func NewPG

func NewPG(pool *pgxpool.Pool) *PGStore

NewPG is the store on pool.

func (*PGStore) AuditGuest

func (st *PGStore) AuditGuest(ctx context.Context, id, guest int64, action string, detail map[string]any) error

AuditGuest writes a guest's write's audit row as the link's owner's (see Store).

func (*PGStore) BindTx

func (st *PGStore) BindTx(ctx context.Context, tx pgx.Tx, token string, person int64) (Share, error)

BindTx binds a live full link to person in tx (see Store). ctx must act under the system principal (Redeem's, users.CreateGuest's).

func (*PGStore) ByToken

func (st *PGStore) ByToken(ctx context.Context, token string) (Share, error)

ByToken reads one LIVE link by its token's hash (see Store). The token's shape is checked first, so a malformed one never reaches the database.

func (*PGStore) Create

func (st *PGStore) Create(ctx context.Context, t Target, kind Kind, ttl time.Duration) (Share, error)

Create makes a link (see Store).

func (*PGStore) End

func (st *PGStore) End(ctx context.Context, id int64) error

End marks link id over (see Store), under the system principal: muster saw its window close, which is no one's write — the link's holder (a grant's redeemer) may observe it as well as its owner. The audit row is the owner's.

func (*PGStore) GuestsDue added in v0.339.0

func (st *PGStore) GuestsDue(ctx context.Context, grace time.Duration, after int64, limit int) ([]int64, error)

GuestsDue lists guests due for removal (see Store).

func (*PGStore) ListForWindow

func (st *PGStore) ListForWindow(ctx context.Context, host, session, window string) ([]Share, error)

ListForWindow lists the caller's live links to one window (see Store).

func (*PGStore) Live

func (st *PGStore) Live(ctx context.Context) ([]Share, error)

Live is every live link of the caller's (see Store).

func (*PGStore) LiveOwnedAmong

func (st *PGStore) LiveOwnedAmong(ctx context.Context, ids []int64) (map[int64]Share, error)

LiveOwnedAmong is which of ids are the caller's live links (see Store).

func (*PGStore) MarkOpened

func (st *PGStore) MarkOpened(ctx context.Context, id int64) error

MarkOpened records a read link's first open (see Store).

func (*PGStore) MyGrants

func (st *PGStore) MyGrants(ctx context.Context) ([]Share, error)

MyGrants is the caller's live full grants (see Store).

func (*PGStore) MyGrantsOn

func (st *PGStore) MyGrantsOn(ctx context.Context, owner int64) ([]Share, error)

MyGrantsOn is MyGrants on owner's sessions (see Store).

func (*PGStore) Redeem

func (st *PGStore) Redeem(ctx context.Context, token string, person int64) (Share, error)

Redeem binds a live full link to person (see Store).

func (*PGStore) RemoveGuest added in v0.339.0

func (st *PGStore) RemoveGuest(ctx context.Context, id int64, grace time.Duration, deleteLogin func(ctx context.Context, sub string) error) (RemovedGuest, error)

RemoveGuest removes guest id (see Store).

🔴 THE ROW IS LOCKED BEFORE IT IS RE-CHECKED, IN A SEPARATE STATEMENT. A redemption of a new link by this guest (BindTx) takes the row FOR KEY SHARE first, so it either committed before the lock — and the re-check, a new statement with a new snapshot, sees its live grant — or it waits for this transaction and then finds the row removed.

func (*PGStore) Revoke

func (st *PGStore) Revoke(ctx context.Context, id int64) (Share, error)

Revoke revokes the caller's link (see Store).

type RemovedGuest added in v0.339.0

type RemovedGuest struct {
	ID int64
	// Owners are the owners of the links the guest held, each of whom got one
	// audit row.
	Owners []int64
}

RemovedGuest is what RemoveGuest removed.

type Share

type Share struct {
	ID      int64
	OwnerID int64
	Target
	Kind      Kind
	CreatedBy int64
	CreatedAt time.Time
	ExpiresAt time.Time
	// RevokedAt, EndedAt and RedeemedAt are zero when unset.
	RevokedAt  time.Time
	EndedAt    time.Time
	RedeemedAt time.Time
	// RedeemedBy is the account a full link bound to; 0 for none.
	RedeemedBy int64
	// Token is the link's secret: set only on Create's and ByToken's answers.
	Token string
}

Share is one link.

func (Share) LiveAt

func (s Share) LiveAt(now time.Time) bool

LiveAt reports whether the link works at now: not revoked, not ended, and not yet expired. The one predicate every reader of a stored row uses.

type Store

type Store interface {
	// Create makes a link of kind to t, lasting ttl, owned by the caller,
	// and writes its audit row in the same transaction. Its answer carries
	// the token.
	Create(ctx context.Context, t Target, kind Kind, ttl time.Duration) (Share, error)
	// Live is every live link the caller owns (at most 1000): what muster
	// checks against the owner's hosts when one of them changes.
	Live(ctx context.Context) ([]Share, error)
	// ListForWindow is the caller's live links to one window, newest first.
	ListForWindow(ctx context.Context, host, session, window string) ([]Share, error)
	// Revoke revokes the caller's link id (audited). A link already not live
	// is ErrNotFound.
	Revoke(ctx context.Context, id int64) (Share, error)
	// End marks link id over because its window closed (audited as the
	// owner's), under the system principal. A link already not live is left
	// as it is.
	End(ctx context.Context, id int64) error
	// MarkOpened records a read link's first open (audited once); later
	// opens change nothing.
	MarkOpened(ctx context.Context, id int64) error
	// ByToken is the LIVE link token names, under the system principal.
	ByToken(ctx context.Context, token string) (Share, error)

	// Redeem binds the live FULL link token names to person (its first
	// redemption, audited), or finds it already theirs. ErrAlreadyRedeemed:
	// another account holds it; ErrNotFound: no such live full link. Under the
	// system principal: the redeemer does not own the row.
	Redeem(ctx context.Context, token string, person int64) (Share, error)
	// BindTx is Redeem inside the caller's transaction (the guest account's
	// creation, users.CreateGuest), for a person who did not exist before it.
	BindTx(ctx context.Context, tx pgx.Tx, token string, person int64) (Share, error)
	// MyGrants is the caller's own LIVE full grants (session_shares'
	// guest_read: redeemed_by = the caller), newest first.
	MyGrants(ctx context.Context) ([]Share, error)
	// MyGrantsOn is MyGrants on one owner's sessions.
	MyGrantsOn(ctx context.Context, owner int64) ([]Share, error)
	// LiveOwnedAmong is which of ids are live links of the CALLER's (row-level
	// security: a plugin token acts as its owner) — a preview gateway's pull.
	LiveOwnedAmong(ctx context.Context, ids []int64) (map[int64]Share, error)
	// AuditGuest writes one audit row of what link id's holder did through
	// it — guest, the account a full link bound to, or 0 for a read link's
	// anonymous holder — as the link's OWNER's (so the owner reads it), under
	// the system principal.
	AuditGuest(ctx context.Context, id, guest int64, action string, detail map[string]any) error

	// GuestsDue is up to limit GUEST accounts with an id above after, in id
	// order, not yet removed, every one of whose full links ended at least
	// grace ago (guestDue), under the system principal. after pages through
	// them (keyset), so guests whose removal keeps failing never hide the rest.
	GuestsDue(ctx context.Context, grace time.Duration, after int64, limit int) ([]int64, error)
	// RemoveGuest removes guest id if it is still due (the row locked and
	// re-checked first): deleteLogin is handed its GoTrue subject ("" for none)
	// and must succeed before anything is written; then the row becomes a
	// tombstone and each owner of a link it held gets one audit row
	// (ActionGuestRemove), in one transaction. ErrGuestNotDue: not a guest, already
	// removed, or a link of its is live or ended within grace.
	RemoveGuest(ctx context.Context, id int64, grace time.Duration, deleteLogin func(ctx context.Context, sub string) error) (RemovedGuest, error)
}

Store is the session_shares table.

🔴 EVERY METHOD BUT ByToken RUNS UNDER THE CALLER'S PRINCIPAL: row-level security shows the owner only their own links. ByToken reads under the system principal, the token's hash being the only key.

type Target

type Target struct {
	Host, Session, Window string
	// TmuxServer is the host's tmux server (mirror.HostView.TmuxServer): with
	// Window, what names the window for good. Required.
	TmuxServer string
}

Target is the mirrored window a link names. A link follows the window, not the conversation in it (a /clear or a Claude Code restart keeps it).

Jump to

Keyboard shortcuts

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