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
- Variables
- func NewToken() (string, error)
- func Object(id int64) string
- func Path(token string) string
- func TokenHash(token string) string
- func TokenShapeOK(s string) bool
- func ValidLifetime(d time.Duration) bool
- type Kind
- type PGStore
- func (st *PGStore) AuditGuest(ctx context.Context, id, guest int64, action string, detail map[string]any) error
- func (st *PGStore) BindTx(ctx context.Context, tx pgx.Tx, token string, person int64) (Share, error)
- func (st *PGStore) ByToken(ctx context.Context, token string) (Share, error)
- func (st *PGStore) Create(ctx context.Context, t Target, kind Kind, ttl time.Duration) (Share, error)
- func (st *PGStore) End(ctx context.Context, id int64) error
- func (st *PGStore) GuestsDue(ctx context.Context, grace time.Duration, after int64, limit int) ([]int64, error)
- func (st *PGStore) ListForWindow(ctx context.Context, host, session, window string) ([]Share, error)
- func (st *PGStore) Live(ctx context.Context) ([]Share, error)
- func (st *PGStore) LiveOwnedAmong(ctx context.Context, ids []int64) (map[int64]Share, error)
- func (st *PGStore) MarkOpened(ctx context.Context, id int64) error
- func (st *PGStore) MyGrants(ctx context.Context) ([]Share, error)
- func (st *PGStore) MyGrantsOn(ctx context.Context, owner int64) ([]Share, error)
- func (st *PGStore) Redeem(ctx context.Context, token string, person int64) (Share, error)
- func (st *PGStore) RemoveGuest(ctx context.Context, id int64, grace time.Duration, ...) (RemovedGuest, error)
- func (st *PGStore) Revoke(ctx context.Context, id int64) (Share, error)
- type RemovedGuest
- type Share
- type Store
- type Target
Constants ¶
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.
const ActionGuestRemove = "session.share.guest.remove"
ActionGuestRemove is the owner's audit row for a guest muster removed.
const DefaultLifetime = 24 * time.Hour
DefaultLifetime is the choice a new link's form starts on.
const MaxLivePerWindow = 20
MaxLivePerWindow bounds the live links one window may have at once, so a form held down cannot fill the table.
const Prefix = "/w/"
Prefix begins every path a link's holder opens. The access log writes it as Prefix + "[redacted]".
Variables ¶
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.
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 TokenShapeOK ¶
TokenShapeOK reports whether s is shaped like a token.
func ValidLifetime ¶
ValidLifetime reports whether d is one of Lifetimes.
Types ¶
type PGStore ¶
type PGStore struct {
// contains filtered or unexported fields
}
PGStore is Store on Postgres.
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 ¶
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 ¶
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) LiveOwnedAmong ¶
LiveOwnedAmong is which of ids are the caller's live links (see Store).
func (*PGStore) MarkOpened ¶
MarkOpened records a read link's first open (see Store).
func (*PGStore) MyGrantsOn ¶
MyGrantsOn is MyGrants on owner's sessions (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.
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 {
// RevokedAt, EndedAt and RedeemedAt are zero when unset.
RedeemedBy int64
Token string
}
Share is one link.
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).