Skip to content

Implement Secure Credential Management System #45

@coderabbitai

Description

@coderabbitai

Overview

Build a secure system for storing and managing API keys, OAuth tokens, and other credentials used by plugins.

Context

Related to PR #40 - Plugins need secure credential storage with encryption.

Implementation Steps

1. Database Schema

Update prisma/schema.prisma:

model Credential {
  id            String    @id @default(cuid())
  userId        String
  name          String
  type          String    // 'api-key', 'oauth2', 'custom'
  pluginName    String
  
  // Encrypted credential data
  encryptedData String    @db.Text
  
  // OAuth2 specific
  accessToken   String?   @db.Text
  refreshToken  String?   @db.Text
  expiresAt     DateTime?
  
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt
  
  user          User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  
  @@index([userId, pluginName])
}

2. Credential Manager

Create src/lib/plugin-system/credential-manager.ts:

  • saveCredential(): Encrypt and store credentials
  • getCredential(): Decrypt and return credentials
  • refreshOAuth2Token(): Auto-refresh expired OAuth tokens
  • deleteCredential(): Securely delete credentials
  • listUserCredentials(): List credentials for a user

3. Encryption Implementation

  • Use AES-256-GCM for encryption
  • Store encryption key in environment variable: CREDENTIAL_ENCRYPTION_KEY
  • IV (Initialization Vector) generated per credential
  • Auth tag for integrity verification

4. OAuth2 Flow

Create src/app/api/auth/oauth/callback/route.ts:

  • Handle OAuth callback
  • Exchange code for tokens
  • Store encrypted tokens
  • Redirect back to plugin configuration

5. Credential UI

Create src/components/credentials/:

  • credential-selector.tsx: Select existing credential or create new
  • credential-form.tsx: Form for API key / custom auth
  • oauth-button.tsx: Button to initiate OAuth flow
  • credential-list.tsx: Manage user's credentials

6. Security Measures

  • Never log decrypted credentials
  • Use secure HTTP-only cookies for OAuth state
  • Implement CSRF protection
  • Rate limiting on credential endpoints
  • Audit log for credential access

Acceptance Criteria

  • Prisma schema updated and migration created
  • Encryption/decryption functions work correctly
  • OAuth2 flow tested with Google (example)
  • Token refresh works automatically
  • Credentials UI components functional
  • No credentials exposed in logs or errors
  • Security audit passed
  • Documentation written

Security Checklist

  • Encryption key is 32 bytes (256 bits)
  • New IV generated for each encryption
  • Auth tags verified on decryption
  • OAuth state parameter used (CSRF)
  • Credentials never sent to frontend (only IDs)

Dependencies

Estimated Effort

  • Large (10-12 hours)

Backlink: #40

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions