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:
@@ -36,6 +36,21 @@ export interface LoginResponse {
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
// active_tenant mirrors `tenant` for endpoints that distinguish home
|
||||
// tenant from current tenant (e.g. /auth/register-by-invite). Only
|
||||
// one of `tenant` / `active_tenant` is populated by any given endpoint.
|
||||
active_tenant?: {
|
||||
id: number
|
||||
name: string
|
||||
description?: string
|
||||
api_key?: string
|
||||
status?: string
|
||||
business?: string
|
||||
storage_quota?: number
|
||||
storage_used?: number
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
}
|
||||
token?: string
|
||||
refresh_token?: string
|
||||
}
|
||||
@@ -417,3 +432,59 @@ export async function validateToken(): Promise<{ success: boolean; valid?: boole
|
||||
|
||||
|
||||
|
||||
|
||||
// ---- share-link registration --------------------------------------------
|
||||
|
||||
// InviteLookup is the public projection of a share-link row used by
|
||||
// /register?token=xxx — enough to render the registration page header
|
||||
// ("X invited you to Y") without leaking sensitive inviter fields.
|
||||
export interface InviteLookup {
|
||||
tenant_id: number
|
||||
tenant_name?: string
|
||||
role: string
|
||||
expires_at: string
|
||||
}
|
||||
|
||||
export interface InviteLookupResponse {
|
||||
success: boolean
|
||||
data?: InviteLookup
|
||||
message?: string
|
||||
}
|
||||
|
||||
export interface RegisterByInviteRequest {
|
||||
token: string
|
||||
email: string
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a share-link token (no auth) into the context the
|
||||
* registration page needs (tenant name, role, expiry). Returns 410
|
||||
* when the link is invalid / revoked / expired.
|
||||
*
|
||||
* Uses POST + body (rather than GET + path) so the plaintext token
|
||||
* never appears in access logs, browser history, or tracing spans.
|
||||
*/
|
||||
export async function getInvitationByToken(token: string): Promise<InviteLookupResponse> {
|
||||
try {
|
||||
const response = await post(`/api/v1/auth/invitations/lookup`, { token })
|
||||
return response as unknown as InviteLookupResponse
|
||||
} catch (error: any) {
|
||||
return { success: false, message: error.message || '' }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete registration via a share-link token. The invitee supplies
|
||||
* their own email — the token is the authorisation, not an identity
|
||||
* lock.
|
||||
*/
|
||||
export async function registerByInvite(data: RegisterByInviteRequest): Promise<LoginResponse> {
|
||||
try {
|
||||
const response = await post('/api/v1/auth/register-by-invite', data)
|
||||
return response as unknown as LoginResponse
|
||||
} catch (error: any) {
|
||||
return { success: false, message: error.message || t('error.auth.registerFailed') }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,17 @@ export interface TenantInvitation {
|
||||
expires_at: string
|
||||
responded_at?: string | null
|
||||
created_at: string
|
||||
// invite_url is set on share-link rows that are still pending. The
|
||||
// backend re-emits it on every list/get so Owners can copy the
|
||||
// link on demand without "copy now or revoke" pressure.
|
||||
invite_url?: string
|
||||
// is_share_link distinguishes share-link rows (no specific invitee,
|
||||
// multi-use, copyable URL) from per-user invitations.
|
||||
is_share_link?: boolean
|
||||
// accepted_count counts how many users have completed registration
|
||||
// through this invitation. Surfaced in the management UI for
|
||||
// share-link rows ("已加入 N 人").
|
||||
accepted_count?: number
|
||||
}
|
||||
|
||||
export interface ListInvitationsResponse {
|
||||
@@ -191,3 +202,35 @@ export async function declineInvitation(invId: number): Promise<SimpleResponse>
|
||||
`/api/v1/me/invitations/${invId}/decline`,
|
||||
)) as unknown as SimpleResponse
|
||||
}
|
||||
|
||||
// ---- share-link API ----------------------------------------------------
|
||||
|
||||
export interface CreateInviteLinkRequest {
|
||||
role: TenantRole
|
||||
message?: string
|
||||
}
|
||||
|
||||
export interface CreateInviteLinkResponse {
|
||||
success: boolean
|
||||
data?: TenantInvitation
|
||||
message?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a multi-use share-link invitation for the tenant. The
|
||||
* returned row carries `invite_url` (composed from the persisted
|
||||
* plaintext token) which the SPA copies into clipboards. The link
|
||||
* stays valid until expiry or revocation; revoking is the same DELETE
|
||||
* as a per-user invitation.
|
||||
*
|
||||
* Backend: POST /api/v1/tenants/:id/invite-links (Owner+).
|
||||
*/
|
||||
export async function createInviteLink(
|
||||
tenantId: number,
|
||||
body: CreateInviteLinkRequest,
|
||||
): Promise<CreateInviteLinkResponse> {
|
||||
return (await post(
|
||||
`/api/v1/tenants/${tenantId}/invite-links`,
|
||||
body,
|
||||
)) as unknown as CreateInviteLinkResponse
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user