Authentication & Authorization

Comprehensive guide to Prism's API key authentication, rate limiting, and quota management.

Table of Contents


Overview

Prism provides a complete authentication and authorization system with:

  • API Key Authentication: SHA-256 hashed keys with SQLite storage

  • Rate Limiting: Token bucket algorithm with configurable limits

  • Method Permissions: Restrict access to specific RPC methods

  • Daily Quotas: Request count limits with automatic reset

  • In-Memory Caching: 60-second cache for authentication checks

Architecture


API Key Management

Enabling Authentication

When enabled, all requests require a valid API key.

Creating API Keys

Use the CLI to create and manage keys:

Parameters:

Parameter
Description
Example

--name

Unique identifier for the key

production-api

--description

Human-readable description

Main production service

--rate-limit

Max tokens in bucket

100

--refill-rate

Tokens added per second

10

--daily-limit

Max requests per day

100000

--expires-in-days

Days until expiration

365

--methods

Comma-separated allowed methods

eth_getLogs,eth_blockNumber

Output:

Listing API Keys

Output:

Revoking API Keys

Using API Keys

Include the API key in requests:

Method 1: Header (Recommended)

Method 2: Query Parameter

API Key Format

API keys follow the format: rpc_<32-alphanumeric-characters>

  • Prefix: rpc_

  • Total Length: 36 characters

  • Random Portion: 32 alphanumeric characters (A-Z, a-z, 0-9)

Example: rpc_A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6

Storage: Keys are hashed using Argon2id before storage. A SHA-256 blind index enables O(1) database lookups. The original key is never stored.


Rate Limiting

Prism implements a token bucket rate limiter per API key.

How It Works

Token Bucket Algorithm:

  1. Each API key has a bucket with max_tokens

  2. Bucket refills at refill_rate tokens/second

  3. Each request consumes 1 token

  4. Request allowed if tokens ≥ 1

  5. Request rejected if tokens < 1

Configuration

Example:

  • Bucket size: 100 tokens

  • Refill rate: 10 tokens/second

  • Sustainable rate: 10 requests/second

  • Burst capacity: 100 requests (then throttled to 10/sec)

Rate Limit Behavior

Scenario: Burst then sustain

Result:

  • First second: 100 requests accepted

  • Subsequent seconds: 10 requests/second sustained

Rate Limit Exceeded Response

HTTP Status: 429 Too Many Requests

Rate Limit Headers

Prism includes rate limit information in response headers:

Header
Description

X-RateLimit-Limit

Maximum requests per window

X-RateLimit-Remaining

Tokens remaining

X-RateLimit-Reset

Unix timestamp when limit resets


Method-Level Permissions

Restrict API keys to specific RPC methods.

Configuration

Allow specific methods:

Allow multiple methods:

Allow all methods (omit --methods or use "all"):

Permission Check

When a request arrives:

  1. Extract method from request

  2. Check if key has permission

  3. Allow or deny

Example:

Permission Denied Response

HTTP Status: 403 Forbidden

Use Cases

Read-only access:

Logs-only access (for event indexers):

Admin access (all methods):


Quota Management

Daily request quotas with automatic midnight reset.

Configuration

Quota Tracking

Quota State:

  • requests_made_today: Current request count

  • quota_reset_at: Midnight UTC when quota resets

  • daily_request_limit: Maximum allowed requests

Quota Check:

  1. Is current time > quota_reset_at?

    • YES: Reset requests_made_today to 0, update quota_reset_at to next midnight

  2. Is requests_made_today < daily_request_limit?

    • YES: Allow request, increment counter

    • NO: Deny request

Quota Exceeded Response

HTTP Status: 429 Too Many Requests

Quota Headers


Security Best Practices

1. Secure API Key Storage

DON'T:

  • Store keys in version control

  • Hardcode keys in source code

  • Share keys between environments

DO:

  • Use environment variables

  • Use secret management systems (AWS Secrets Manager, HashiCorp Vault)

  • Rotate keys regularly

Example (Node.js):

2. Use Method Restrictions

Principle of least privilege: grant only necessary methods.

3. Set Appropriate Rate Limits

Balance performance and cost:

4. Monitor Usage

Regularly review API key usage:

5. Rotate Keys Regularly

6. Use HTTPS in Production

DON'T:

DO:

7. Implement Retry Logic

Handle rate limit and quota errors gracefully:


Database Schema

API keys are stored in SQLite with the following schema:

Security Notes:

  • key_hash: Argon2id with memory=64MB, iterations=3, parallelism=4

  • blind_index: SHA-256 for timing-attack resistant O(1) lookups

  • The original API key is never stored


Metrics

Authentication metrics are exposed at /metrics:


Next: Learn about Monitoring for comprehensive observability.

Last updated