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
- Variables
- func CSRFToken(sessionToken string) string
- func HashToken(token string) []byte
- func Status(grant store.AccessGrant, now time.Time) string
- func VerifyCSRFToken(sessionToken, csrf string) bool
- type Config
- type GrantRequest
- type Invitation
- type Listener
- type Service
- func (s *Service) Audit(ctx context.Context, eventType, remoteAddr string, details map[string]string) error
- func (s *Service) Authenticate(ctx context.Context, token string) (Session, error)
- func (s *Service) CreateGrant(ctx context.Context, req GrantRequest) (Invitation, error)
- func (s *Service) Grants(ctx context.Context, terminalID string) ([]store.AccessGrant, error)
- func (s *Service) Logout(ctx context.Context, token, remoteAddr string) error
- func (s *Service) Now() time.Time
- func (s *Service) Redeem(ctx context.Context, token, remoteAddr string, ...) (Session, error)
- func (s *Service) RedeemReplacing(ctx context.Context, token, previous, remoteAddr string, ...) (Session, error)
- func (s *Service) RevokeGrant(ctx context.Context, terminalID, grantID, remoteAddr string) (store.AccessGrant, error)
- type Session
Constants ¶
const ( // MinGrantTTL is the shortest configurable grant lifetime. MinGrantTTL = time.Minute // MaxLabelBytes bounds a grant's label. MaxLabelBytes = 64 )
const ( StatusActive = "active" StatusExpired = "expired" StatusExhausted = "exhausted" StatusRevoked = "revoked" StatusReplaced = "replaced" )
Grant statuses reported by Status.
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 ¶
Functions ¶
func Status ¶
func Status(grant store.AccessGrant, now time.Time) string
Status summarizes whether grant can still be redeemed at now.
func VerifyCSRFToken ¶
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 ¶
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 ¶
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) 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.