Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

1.4 KB
Raw Blame
import mongoose, { Schema, Document, Types } from 'mongoose';

export interface IAgentApiKey extends Document {
  userId: Types.ObjectId;
  name: string;
  keyHash: string;
  keyPrefix: string;
  lastUsedAt?: Date;
  expiresAt?: Date;
  createdAt: Date;
  updatedAt: Date;
  tenantId?: string;
}

const agentApiKeySchema: Schema<IAgentApiKey> = new Schema(
  {
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true,
      index: true,
    },
    name: {
      type: String,
      required: true,
      trim: true,
      maxlength: 100,
    },
    keyHash: {
      type: String,
      required: true,
      select: false,
      index: true,
    },
    keyPrefix: {
      type: String,
      required: true,
      index: true,
    },
    lastUsedAt: {
      type: Date,
    },
    expiresAt: {
      type: Date,
    },
    tenantId: {
      type: String,
      index: true,
    },
  },
  { timestamps: true },
);

agentApiKeySchema.index({ userId: 1, name: 1, tenantId: 1 });

/**
 * TTL index for automatic cleanup of expired keys.
 * MongoDB deletes documents when expiresAt passes (expireAfterSeconds: 0 means immediate).
 * Note: Expired keys are permanently removed, not soft-deleted.
 * If audit trails are needed, remove this index and check expiration programmatically.
 */
agentApiKeySchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });

export default agentApiKeySchema;