access

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 12 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 access issues and validates capability grants: invitation tokens that let a guest edit or view one terminal, and the access sessions they are redeemed for.

Index

Constants

View Source
const (

	// MinGrantTTL is the shortest configurable grant lifetime.
	MinGrantTTL = time.Minute
	// MaxLabelBytes bounds a grant's label.
	MaxLabelBytes = 64
)
View Source
const (
	StatusActive    = "active"
	StatusExpired   = "expired"
	StatusExhausted = "exhausted"
	StatusRevoked   = "revoked"
	StatusReplaced  = "replaced"
)

Grant statuses reported by Status.

View Source
const (
	ReasonRevoked  = "revoked"
	ReasonReplaced = "replaced"
	ReasonLogout   = "logout"
	// ReasonSuperseded ends a session whose browser redeemed another
	// invitation.
	ReasonSuperseded = "superseded"
)

Revocation reasons passed to a Listener.

Variables

View Source
var (
	ErrInvalidToken    = errors.New("access: invalid or expired invitation")
	ErrUnauthenticated = errors.New("access: unauthenticated")
	ErrNotFound        = errors.New("access: not found")
	ErrInvalidArgument = errors.New("access: invalid argument")
)

Functions

func CSRFToken

func CSRFToken(sessionToken string) string

CSRFToken derives the CSRF token bound to an access-session token.

func HashToken

func HashToken(token string) []byte

HashToken is the SHA-256 digest under which tokens are stored.

func Status

func Status(grant store.AccessGrant, now time.Time) string

Status summarizes whether grant can still be redeemed at now.

func VerifyCSRFToken

func VerifyCSRFToken(sessionToken, csrf string) bool

VerifyCSRFToken reports whether csrf is bound to sessionToken.

Types

type Config

type Config struct {
	Store    *store.Store
	Listener Listener
	Now      func() time.Time
	// SessionTTL bounds an access session; it never outlives its grant.
	SessionTTL time.Duration
	// DefaultGrantTTL applies when a request gives no TTL; MaxGrantTTL is
	// the longest TTL a request may ask for.
	DefaultGrantTTL time.Duration
	MaxGrantTTL     time.Duration
	// MaxRedemptions is both the default and the upper bound of a grant's
	// redemption limit, and so of the sessions it can create.
	MaxRedemptions int
}

Config wires a Service. Zero durations and limits receive defaults.

type GrantRequest

type GrantRequest struct {
	TerminalID     string
	Role           store.AccessRole
	Label          string
	TTL            time.Duration
	SingleUse      bool
	MaxRedemptions int
	RemoteAddr     string
}

GrantRequest describes a new grant. A zero TTL or MaxRedemptions uses the configured default; SingleUse limits the grant to one redemption.

type Invitation

type Invitation struct {
	Grant    store.AccessGrant
	Token    string
	Replaced []store.AccessGrant
}

Invitation is a newly created grant. Token is the raw invitation token and is available only here. Replaced lists editor grants the new grant revoked.

type Listener

type Listener interface {
	GrantsRevoked(terminalID string, grantIDs []string, reason string)
	SessionRevoked(sessionID int64, reason string)
}

Listener is told about revocations after they are committed, so live connections can lose access without waiting to re-validate.

type Service

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

Service manages grants and access sessions.

func NewService

func NewService(config Config) (*Service, error)

NewService validates config and returns a Service.

func (*Service) Audit

func (s *Service) Audit(ctx context.Context, eventType, remoteAddr string, details map[string]string) error

Audit records a collaboration audit event that changes no capability state. details must never contain capability tokens, their hashes, or terminal content.

func (*Service) Authenticate

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

Authenticate resolves an access-session token to a live session.

func (*Service) CreateGrant

func (s *Service) CreateGrant(ctx context.Context, req GrantRequest) (Invitation, error)

CreateGrant issues a grant for a terminal. Creating an editor grant atomically revokes the terminal's previous editor grant and its sessions.

func (*Service) Grants

func (s *Service) Grants(ctx context.Context, terminalID string) ([]store.AccessGrant, error)

Grants lists a terminal's grants in creation order.

func (*Service) Logout

func (s *Service) Logout(ctx context.Context, token, remoteAddr string) error

Logout revokes the access session identified by token.

func (*Service) Now

func (s *Service) Now() time.Time

Now returns the service clock's current time.

func (*Service) Redeem

func (s *Service) Redeem(ctx context.Context, token, remoteAddr string, check func(store.AccessGrant) error) (Session, error)

Redeem exchanges an invitation token for a new access session. The invitation itself never becomes a session credential. A non-nil check runs inside the redemption's transaction; its error rolls the redemption back, leaving the invitation unused.

func (*Service) RedeemReplacing

func (s *Service) RedeemReplacing(ctx context.Context, token, previous, remoteAddr string, check func(store.AccessGrant) error) (Session, error)

RedeemReplacing is Redeem that also revokes the live access session identified by previous, if any, in the same transaction.

func (*Service) RevokeGrant

func (s *Service) RevokeGrant(ctx context.Context, terminalID, grantID, remoteAddr string) (store.AccessGrant, error)

RevokeGrant revokes one of the terminal's grants and all its sessions. Revoking an already revoked grant returns it unchanged.

type Session

type Session struct {
	Token     string
	ID        int64
	Grant     store.AccessGrant
	ExpiresAt time.Time
}

Session is a live access session. Token is set only by Redeem.

Jump to

Keyboard shortcuts

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