Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 25, 2025

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Phase 1] RBAC & Scoped API Tokens (Multi-Tenant Authorization)</issue_title>
<issue_description>## Priority: P0 (Critical)
Phase: 1 - E-Commerce Core
Estimate: 3 days
Type: Story

Context

Introduce Role-Based Access Control with per-organization scoping and scoped API tokens to prevent privilege escalation and cross-tenant data exposure.

Scope

  • Roles: OWNER, ADMIN, MANAGER, STAFF, VIEWER (enum)
  • Membership: extend existing Membership model (ensure unique (userId, organizationId))
  • Permission Matrix (draft):
    • OWNER: all + billing + destructive
    • ADMIN: all minus billing destructive
    • MANAGER: products, inventory, orders, limited staff management
    • STAFF: orders fulfillment + inventory adjust
    • VIEWER: read-only
  • API Tokens: ApiToken model (tokenHash, organizationId, scopes[], lastUsedAt, expiresAt, createdByUserId)
  • Scopes (initial): products:rw, inventory:rw, orders:rw, payments:rw, webhooks:rw, billing:r, analytics:r
  • Middleware: server-side guard builder requireScope(scopes: string[]) & requireRole(minRole) utilities
  • Prisma query pattern enforcement: ALWAYS filter by organization and authorized scope

Acceptance Criteria

  • Server actions reject access without required role/scope
  • API Token can be created by OWNER/ADMIN only
  • Token hashed (SHA-256) stored, raw shown once
  • Invalid/expired token returns 401 (never ambiguous 404 for auth boundaries)
  • Attempts logged with structured context (orgId, userId/tokenId, route, scopes)
  • Scopes enforced on at least 3 critical endpoints (product create, inventory adjustment, order read)
  • Role downgrade prevents previous elevated actions immediately

Data Model (Draft)

model ApiToken {
  id              String   @id @default(cuid())
  organizationId  String
  tokenHash       String   @unique
  name            String
  scopes          String[] // store as text[] in Postgres
  expiresAt       DateTime?
  lastUsedAt      DateTime?
  createdByUserId String
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt

  @@index([organizationId])
}

Dependencies

  • Builds on: existing Membership & Organization models
  • Enables: future audit trail (#TBD Observability) & external integrations security

Metrics

  • Unauthorized access attempts captured 100%
  • Token scope mismatch error rate baseline recorded
  • Mean authorization check overhead < 2ms

Testing Checklist

  • Token with missing scope denied
  • Role downgrade reflects immediately (simulate OWNER→STAFF)
  • Expired token fails
  • Organization isolation enforced (attempt cross-org access fails)

Risk

High integrity & security impact (score: 18). Prevents cross-tenant leakage & abuse.

References

  • docs/GITHUB_ISSUES_COMPARISON_ANALYSIS.md (RBAC gap section)
    </issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Nov 25, 2025
@vercel
Copy link

vercel bot commented Nov 25, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
stormcomui Ready Ready Preview Comment Nov 25, 2025 2:37am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

[Phase 1] RBAC & Scoped API Tokens (Multi-Tenant Authorization)

2 participants