Documentation
¶
Overview ¶
Package invite defines single-use, expiring sign-up invitations.
The invite token is a 256-bit random secret handed to the invitee once. Stores keep only its SHA-256 hash, exactly like session secrets.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalid is returned for unknown, used, expired or mismatched invites. ErrInvalid = errors.New("invite: invalid, used or expired") // ErrNotFound is returned by stores for missing invites. ErrNotFound = errors.New("invite: not found") )
Functions ¶
This section is empty.
Types ¶
type Invite ¶
type Invite struct {
ID string // public identifier
TokenHash []byte // SHA-256 of the token
Email string // optional; if set, the signing-up identity must have this email
Roles []string // granted to the new subject (via iam.SignupGrant)
CreatedBy string // subject ID of the inviter (for audit)
CreatedAt time.Time
ExpiresAt time.Time
UsedAt time.Time // zero until consumed
UsedBy string // identity that used it, as "provider:providerID"
}
Invite is the stored record. It never contains the token.
type Manager ¶ added in v2.4.0
type Manager interface {
// ListUsable returns the invites usable at now, newest first.
ListUsable(ctx context.Context, now time.Time) ([]*Invite, error)
// DeleteUsable removes the invite only if it is usable at now. It
// returns ErrNotFound for a missing invite and ErrInvalid for a used or
// expired one, which it keeps.
DeleteUsable(ctx context.Context, id string, now time.Time) error
// Replace atomically removes the invite oldID, which must be usable at
// now, and creates inv. On ErrNotFound or ErrInvalid (as for
// DeleteUsable) nothing changes, so of two concurrent replacements of
// one invite at most one succeeds.
Replace(ctx context.Context, oldID string, now time.Time, inv *Invite) error
}
Manager is optionally implemented by a Store, for administration screens; iam.InviteAdmin needs it. Every method decides usability at the time it is given, like Consume.
type Store ¶
type Store interface {
// Create persists a new invite.
Create(ctx context.Context, inv *Invite) error
// GetByTokenHash returns an invite (used or not), or ErrNotFound.
GetByTokenHash(ctx context.Context, hash []byte) (*Invite, error)
// Consume marks the invite as used by usedBy at time at, but only if it
// is still usable at that time; otherwise ErrInvalid.
Consume(ctx context.Context, hash []byte, usedBy string, at time.Time) (*Invite, error)
// Delete removes an invite (revocation). Missing is not an error.
Delete(ctx context.Context, id string) error
}
Store persists invites. Consume must be atomic: of two concurrent calls for the same hash, at most one succeeds.