Skip to content

Client-side API key storage with localStorage #20

@anurag629

Description

@anurag629

Phase 1 — Foundation (2/4)

Implement secure client-side API key management using localStorage.

Scope

src/lib/ai/storage.ts

  • getApiKey(provider): string | null
  • setApiKey(provider, key): void
  • removeApiKey(provider): void
  • clearAllApiKeys(): void
  • getSelectedProvider(): AIProvider | null
  • setSelectedProvider(provider): void
  • getSelectedModel(provider): string | null
  • setSelectedModel(provider, modelId): void
  • hasAnyKeyConfigured(): boolean

Storage Keys

ogcops-ai-provider        → selected provider ID
ogcops-ai-model-{provider} → selected model per provider
ogcops-ai-key-{provider}   → API key per provider

Behavior

  • Each provider stores its key independently — switching providers preserves other keys
  • Model selection is per-provider — remembered when switching back
  • hasAnyKeyConfigured() used to conditionally show AI features in the UI
  • All functions are synchronous (localStorage is sync)

Security Considerations

  • Keys stored in localStorage only — no cookies, no sessionStorage
  • Keys never included in URL params (unlike other OGCOPS state)
  • clearAllApiKeys() removes every ogcops-ai-* entry
  • No encryption (localStorage is already accessible to same-origin JS — encryption would be security theater)

Acceptance Criteria

  • All CRUD operations for keys, provider, and model selection work correctly
  • Switching providers preserves previously saved keys
  • clearAllApiKeys() removes all AI-related localStorage entries
  • Functions handle missing/corrupted localStorage gracefully (return null, don't throw)
  • Works in private/incognito mode (localStorage available but not persisted)

Metadata

Metadata

Assignees

No one assigned

    Labels

    aiAI-powered featuresfeatureNew feature requestphase-1Foundation / infrastructure

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions