feat(rbac): add multi-use share-link invitations for invite_only mode (#1504)
* feat(rbac): add multi-use share-link invitations for invite_only mode When the auth.registration_mode toggle flips to invite_only, the public /auth/register endpoint returns 403 — but until now there was no channel for unregistered users to actually join a tenant. The existing invitation table required a registered invitee, so new emails could not enter the system at all. This change introduces a share-link model: - Owner generates a multi-use registration link with a per-tenant role - Recipient opens the link, registers with their own email, and is added to the tenant on the same request - Token is stored in plaintext (not hashed) so the management UI can re-display the URL on demand without a "copy now or revoke" trap. Threat model is bounded by 7-day TTL, revocability, and the fact that all the link grants is membership in one tenant. - accepted_count tracks how many users joined through the link so the Owner can tell whether a link is fresh or has already been spread Per-user invitations (registered email -> in-app inbox accept) are unchanged. Share-link rows use empty invitee_user_id as discriminator; the partial unique index on (tenant_id, invitee_user_id) was relaxed to skip empty values so multiple share links can coexist per tenant. Frontend reuses Login.vue for /register?token=xxx instead of a parallel page, and adds icon-only revoke / remove actions plus a distinct "active" status tag for share-link rows. Migration 000054 adds the token column, the accepted_count column, and the relaxed pending unique index in a single step. * feat(auth): update invitation token handling and add rate limiting - Refactor the invitation token retrieval to use POST instead of GET, enhancing security by preventing the plaintext token from appearing in logs and browser history. - Update the API endpoint from `/auth/invitations/:token` to `/auth/invitations/lookup` to reflect the new method. - Introduce a rate limiter for unauthenticated share-link endpoints to mitigate brute-force attacks and abuse, ensuring a maximum of 30 requests per minute per IP. - Adjust the registration flow to maintain user experience while securing the invitation process.
This commit is contained in:
@@ -26,6 +26,14 @@ type TenantInvitationRepository interface {
|
||||
// they hit the unique index.
|
||||
GetPendingByPair(ctx context.Context, tenantID uint64, inviteeUserID string) (*types.TenantInvitation, error)
|
||||
|
||||
// GetActiveByToken looks up the share-link row matching the
|
||||
// supplied plaintext token. Returns (nil, nil) if no row matches
|
||||
// or the row is no longer pending. "Active" rather than "pending
|
||||
// for a single user" because share-link rows are multi-use:
|
||||
// AcceptByToken does NOT consume them, only Revoke or expiry
|
||||
// removes them from circulation.
|
||||
GetActiveByToken(ctx context.Context, token string) (*types.TenantInvitation, error)
|
||||
|
||||
// ListByTenant returns invitations for the tenant ordered by
|
||||
// id DESC (newest first). includeTerminal controls whether
|
||||
// non-pending rows (accepted/declined/revoked/expired) are
|
||||
@@ -71,4 +79,10 @@ type TenantInvitationRepository interface {
|
||||
// events (we don't — the volume could be high and the audit-log
|
||||
// table already records the original invitation_sent rows).
|
||||
SweepExpired(ctx context.Context, now time.Time) (int64, error)
|
||||
|
||||
// IncrementAcceptedCount atomically bumps accepted_count by 1.
|
||||
// Used by AcceptByToken so the management UI can show "N 人已通过
|
||||
// 此链接加入" for share-link rows. Per-user invitations also call
|
||||
// this on accept; the count just caps at 1 there.
|
||||
IncrementAcceptedCount(ctx context.Context, id uint64) error
|
||||
}
|
||||
|
||||
@@ -64,4 +64,34 @@ type TenantInvitationService interface {
|
||||
// after running the lazy sweep, so the inbox badge can poll a
|
||||
// lightweight endpoint without paginating the full list.
|
||||
CountPendingByInvitee(ctx context.Context, inviteeUserID string) (int64, error)
|
||||
|
||||
// CreateShareLink generates a multi-use share-link invitation for
|
||||
// the tenant. The plaintext token is persisted on the row and also
|
||||
// returned for the handler to compose the registration URL — the
|
||||
// token stays available via list/get for as long as the row is
|
||||
// pending so Owners can re-share without "copy now or lose it"
|
||||
// pressure. Multiple share-link rows can coexist on the same
|
||||
// tenant (different roles, or just multiple campaigns).
|
||||
CreateShareLink(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
role types.TenantRole,
|
||||
invitedBy *string,
|
||||
message string,
|
||||
) (inv *types.TenantInvitation, plainToken string, err error)
|
||||
|
||||
// LookupByToken resolves a plaintext token to its active share-link
|
||||
// row. Returns ErrInvitationTokenInvalid for unknown / expired /
|
||||
// revoked tokens to avoid leaking which tokens used to exist. The
|
||||
// row is NOT consumed; the same token can be looked up arbitrarily
|
||||
// many times until it expires or the Owner revokes it.
|
||||
LookupByToken(ctx context.Context, plainToken string) (*types.TenantInvitation, error)
|
||||
|
||||
// AcceptByToken creates a tenant_members row binding newUserID to
|
||||
// the share-link's tenant + role. Unlike Accept, this does NOT flip
|
||||
// the invitation row into a terminal state — share-link rows stay
|
||||
// pending so subsequent invitees can also register through the
|
||||
// same link. Idempotent: if the user already has membership, the
|
||||
// existing row is returned.
|
||||
AcceptByToken(ctx context.Context, plainToken string, newUserID string) (*types.TenantMember, error)
|
||||
}
|
||||
|
||||
@@ -64,9 +64,22 @@ type TenantInvitation struct {
|
||||
ID uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
// TenantID references tenants.id.
|
||||
TenantID uint64 `json:"tenant_id" gorm:"not null;index"`
|
||||
// InviteeUserID references users.id (always a registered user; the
|
||||
// handler resolves the email to a User row before insertion).
|
||||
InviteeUserID string `json:"invitee_user_id" gorm:"type:varchar(36);not null;index"`
|
||||
// InviteeUserID references users.id when this row represents a
|
||||
// per-user invitation (Owner picked a registered email). For
|
||||
// share-link rows the field is the empty-string sentinel — there
|
||||
// is no specific invitee yet; whoever holds the link can register.
|
||||
// The (tenant_id, invitee_user_id) partial unique index in
|
||||
// migration 000054 was relaxed to skip empty values so multiple
|
||||
// share-link rows can coexist per tenant.
|
||||
InviteeUserID string `json:"invitee_user_id" gorm:"type:varchar(36);not null;default:'';index"`
|
||||
// Token holds the plaintext registration token for share-link
|
||||
// rows. Empty for per-user invitations (those use the in-app
|
||||
// inbox, not a URL). Stored plaintext so the management UI can
|
||||
// re-display the link on demand — the threat model is bounded by
|
||||
// short TTL, revocability, and the fact that all the link grants
|
||||
// is membership in this one tenant. Excluded from JSON to keep
|
||||
// the wire shape small; handlers emit invite_url instead.
|
||||
Token string `json:"-" gorm:"column:token;type:varchar(64);not null;default:'';index"`
|
||||
// InvitedBy records the user id that issued this invitation. NULL
|
||||
// for invitations created via service-internal / synthetic actors
|
||||
// (mirrors the same treatment TenantMember.InvitedBy gets).
|
||||
@@ -87,6 +100,13 @@ type TenantInvitation struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
// AcceptedCount counts how many users have completed registration
|
||||
// through this invitation. Per-user invitations cap out at 1 (and
|
||||
// the row flips to accepted in the same step). Share-link rows can
|
||||
// accumulate many — this is what the management UI surfaces as the
|
||||
// "已加入 N 人" caption so Owners can see whether a link is fresh
|
||||
// or has already been used widely.
|
||||
AcceptedCount int `json:"accepted_count" gorm:"column:accepted_count;not null;default:0"`
|
||||
}
|
||||
|
||||
// TableName binds TenantInvitation to the tenant_invitations table.
|
||||
@@ -122,4 +142,17 @@ type TenantInvitationResponse struct {
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
RespondedAt *time.Time `json:"responded_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// InviteURL is set on share-link rows that are still pending.
|
||||
// Composed by the handler from FrontendBaseURL + the row's plaintext
|
||||
// token, so the management UI can render a "copy link" button per
|
||||
// row without ever exposing the raw token through the JSON wire.
|
||||
InviteURL string `json:"invite_url,omitempty"`
|
||||
// IsShareLink is a convenience flag so the SPA doesn't have to
|
||||
// special-case "invitee_user_id == ''" everywhere when distinguishing
|
||||
// share-link rows from per-user invitations.
|
||||
IsShareLink bool `json:"is_share_link,omitempty"`
|
||||
// AcceptedCount mirrors TenantInvitation.AcceptedCount. Surfaced
|
||||
// for share-link rows so the management UI can show "N 人已通过此
|
||||
// 链接加入"; informational only for per-user invitations.
|
||||
AcceptedCount int `json:"accepted_count,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user