|
1 | 1 | --- |
2 | | -title: Auth |
3 | | -description: Authentication for APIs |
4 | | -order: 1 |
| 2 | +title: API Authentication & Security |
| 3 | +description: Secure your Moose API endpoints with JWT tokens or API keys |
| 4 | +order: 3 |
| 5 | +category: apis |
5 | 6 | --- |
6 | 7 |
|
7 | | -# Auth |
| 8 | +import { Callout } from "@/components/mdx"; |
8 | 9 |
|
9 | | -This page is a placeholder. Content migration pending. |
| 10 | +# API Authentication & Security |
| 11 | + |
| 12 | +Moose supports two authentication mechanisms for securing your API endpoints: |
| 13 | + |
| 14 | +- **[API Keys](#api-key-authentication)** - Simple, static authentication for internal applications and getting started |
| 15 | +- **[JWT (JSON Web Tokens)](#jwt-authentication)** - Token-based authentication for integration with existing identity providers |
| 16 | + |
| 17 | +Choose the method that fits your use case, or use both together with custom configuration. |
| 18 | + |
| 19 | +## Do you want to use API Keys? |
| 20 | + |
| 21 | +API keys are the simplest way to secure your Moose endpoints. They're ideal for: |
| 22 | +- Internal applications and microservices |
| 23 | +- Getting started quickly with authentication |
| 24 | +- Scenarios where you control both client and server |
| 25 | + |
| 26 | +### How API Keys Work |
| 27 | + |
| 28 | +API keys use PBKDF2 HMAC SHA256 hashing for secure storage. You generate a token pair (plain-text and hashed) using the Moose CLI, store the hashed version in environment variables, and send the plain-text version in your request headers. |
| 29 | + |
| 30 | +### Step 1: Generate API Keys |
| 31 | + |
| 32 | +Generate tokens and hashed keys using the Moose CLI: |
| 33 | + |
| 34 | +```bash |
| 35 | +moose generate hash-token |
| 36 | +``` |
| 37 | + |
| 38 | +**Output:** |
| 39 | +- **ENV API Keys**: Hashed key for environment variables (use this in your server configuration) |
| 40 | +- **Bearer Token**: Plain-text token for client applications (use this in `Authorization` headers) |
| 41 | + |
| 42 | +<Callout type="info"> |
| 43 | + Use the **hashed key** for environment variables and `moose.config.toml`. Use the **plain-text token** in your `Authorization: Bearer token` headers. |
| 44 | +</Callout> |
| 45 | + |
| 46 | +### Step 2: Configure API Keys with Environment Variables |
| 47 | + |
| 48 | +Set environment variables with the **hashed** API keys you generated: |
| 49 | +```bash |
| 50 | +# For ingest endpoints |
| 51 | +export MOOSE_INGEST_API_KEY='your_pbkdf2_hmac_sha256_hashed_key' |
| 52 | + |
| 53 | +# For analytics endpoints |
| 54 | +export MOOSE_CONSUMPTION_API_KEY='your_pbkdf2_hmac_sha256_hashed_key' |
| 55 | + |
| 56 | +# For admin endpoints |
| 57 | +export MOOSE_ADMIN_TOKEN='your_plain_text_token' |
| 58 | +``` |
| 59 | + |
| 60 | +Or set the admin API key in `moose.config.toml`: |
| 61 | + |
| 62 | +```toml filename="moose.config.toml" |
| 63 | +[authentication] |
| 64 | +admin_api_key = "your_pbkdf2_hmac_sha256_hashed_key" |
| 65 | +``` |
| 66 | + |
| 67 | +<Callout type="info" title="Storing Admin API Keys in Project Configuration File"> |
| 68 | + Storing the `admin_api_key` (which is a PBKDF2 HMAC SHA256 hash) in your `moose.config.toml` file is an acceptable practice, even if the file is version-controlled. This is because the actual plain-text Bearer token (the secret) is not stored. The hash is computationally expensive to reverse, ensuring that your secret is not exposed in the codebase. |
| 69 | +</Callout> |
| 70 | + |
| 71 | +### Step 3: Make Authenticated Requests |
| 72 | + |
| 73 | +All authenticated requests require the `Authorization` header with the **plain-text token**: |
| 74 | + |
| 75 | +```bash |
| 76 | +# Using curl |
| 77 | +curl -H "Authorization: Bearer your_plain_text_token_here" \ |
| 78 | + https://your-moose-instance.com/ingest/YourDataModel |
| 79 | + |
| 80 | +# Using JavaScript |
| 81 | +fetch('https://your-moose-instance.com/api/endpoint', { |
| 82 | + headers: { |
| 83 | + 'Authorization': 'Bearer your_plain_text_token_here' |
| 84 | + } |
| 85 | +}) |
| 86 | +``` |
| 87 | + |
| 88 | +## Do you want to use JWTs? |
| 89 | + |
| 90 | +JWT authentication integrates with existing identity providers and follows standard token-based authentication patterns. Use JWTs when: |
| 91 | +- You have an existing identity provider (Auth0, Okta, etc.) |
| 92 | +- You need user-specific authentication and authorization |
| 93 | +- You want standard OAuth 2.0 / OpenID Connect flows |
| 94 | + |
| 95 | +### How JWT Works |
| 96 | + |
| 97 | +Moose validates JWT tokens using RS256 algorithm with your identity provider's public key. You configure the expected issuer and audience, and Moose handles token verification automatically. |
| 98 | + |
| 99 | +### Step 1: Configure JWT Settings |
| 100 | + |
| 101 | +#### Option A: Configure in `moose.config.toml` |
| 102 | + |
| 103 | +```toml filename=moose.config.toml |
| 104 | +[jwt] |
| 105 | +# Your JWT public key (PEM-formatted RSA public key) |
| 106 | +secret = """ |
| 107 | +-----BEGIN PUBLIC KEY----- |
| 108 | +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy... |
| 109 | +-----END PUBLIC KEY----- |
| 110 | +""" |
| 111 | +# Expected JWT issuer |
| 112 | +issuer = "https://my-auth-server.com/" |
| 113 | +# Expected JWT audience |
| 114 | +audience = "my-moose-app" |
| 115 | +``` |
| 116 | + |
| 117 | +<Callout type="info"> |
| 118 | + The `secret` field should contain your JWT **public key** used to verify signatures using RS256 algorithm. |
| 119 | +</Callout> |
| 120 | + |
| 121 | +#### Option B: Configure with Environment Variables |
| 122 | + |
| 123 | +You can also set these values as environment variables: |
| 124 | + |
| 125 | +```bash filename=".env" copy |
| 126 | +MOOSE_JWT_PUBLIC_KEY=your_jwt_public_key # PEM-formatted RSA public key (overrides `secret` in `moose.config.toml`) |
| 127 | +MOOSE_JWT_ISSUER=your_jwt_issuer # Expected JWT issuer (overrides `issuer` in `moose.config.toml`) |
| 128 | +MOOSE_JWT_AUDIENCE=your_jwt_audience # Expected JWT audience (overrides `audience` in `moose.config.toml`) |
| 129 | +``` |
| 130 | + |
| 131 | +### Step 2: Make Authenticated Requests |
| 132 | + |
| 133 | +Send requests with the JWT token in the `Authorization` header: |
| 134 | + |
| 135 | +```bash |
| 136 | +# Using curl |
| 137 | +curl -H "Authorization: Bearer your_jwt_token_here" \ |
| 138 | + https://your-moose-instance.com/ingest/YourDataModel |
| 139 | + |
| 140 | +# Using JavaScript |
| 141 | +fetch('https://your-moose-instance.com/api/endpoint', { |
| 142 | + headers: { |
| 143 | + 'Authorization': 'Bearer your_jwt_token_here' |
| 144 | + } |
| 145 | +}) |
| 146 | +``` |
| 147 | + |
| 148 | +## Want to use both? Here's the caveats |
| 149 | + |
| 150 | +You can configure both JWT and API Key authentication simultaneously. When both are configured, Moose's authentication behavior depends on the `enforce_on_all_*` flags. |
| 151 | + |
| 152 | +### Understanding Authentication Priority |
| 153 | + |
| 154 | +#### Default Behavior (No Enforcement) |
| 155 | + |
| 156 | +By default, when both JWT and API Keys are configured, Moose tries JWT validation first, then falls back to API Key validation: |
| 157 | + |
| 158 | +```toml filename="moose.config.toml" |
| 159 | +[jwt] |
| 160 | +# JWT configuration |
| 161 | +secret = "..." |
| 162 | +issuer = "https://my-auth-server.com/" |
| 163 | +audience = "my-moose-app" |
| 164 | +# enforce flags default to false |
| 165 | +``` |
| 166 | + |
| 167 | +```bash filename=".env" |
| 168 | +# API Key configuration |
| 169 | +MOOSE_INGEST_API_KEY='your_pbkdf2_hmac_sha256_hashed_key' |
| 170 | +MOOSE_CONSUMPTION_API_KEY='your_pbkdf2_hmac_sha256_hashed_key' |
| 171 | +``` |
| 172 | + |
| 173 | +**For Ingest Endpoints (`/ingest/*`)**: |
| 174 | +- Attempts JWT validation first (RS256 signature check) |
| 175 | +- Falls back to API Key validation (PBKDF2 HMAC SHA256) if JWT fails |
| 176 | + |
| 177 | +**For Analytics Endpoints (`/api/*`)**: |
| 178 | +- Same fallback behavior as ingest endpoints |
| 179 | + |
| 180 | +This allows you to use either authentication method for your clients. |
| 181 | + |
| 182 | +#### Enforcing JWT Only |
| 183 | + |
| 184 | +If you want to **only** accept JWT tokens (no API key fallback), set the enforcement flags: |
| 185 | + |
| 186 | +```toml filename="moose.config.toml" |
| 187 | +[jwt] |
| 188 | +secret = "..." |
| 189 | +issuer = "https://my-auth-server.com/" |
| 190 | +audience = "my-moose-app" |
| 191 | +# Only accept JWT, no API key fallback |
| 192 | +enforce_on_all_ingest_apis = true |
| 193 | +enforce_on_all_consumptions_apis = true |
| 194 | +``` |
| 195 | + |
| 196 | +**Result**: When enforcement is enabled, API Key authentication is disabled even if the environment variables are set. Only valid JWT tokens will be accepted. |
| 197 | + |
| 198 | +### Common Use Cases |
| 199 | + |
| 200 | +#### Use Case 1: Different Auth for Different Endpoints |
| 201 | + |
| 202 | +Configure JWT for user-facing analytics endpoints, API keys for internal ingestion: |
| 203 | + |
| 204 | +```toml filename="moose.config.toml" |
| 205 | +[jwt] |
| 206 | +secret = "..." |
| 207 | +issuer = "https://my-auth-server.com/" |
| 208 | +audience = "my-moose-app" |
| 209 | +enforce_on_all_consumptions_apis = true # JWT only for /api/* |
| 210 | +enforce_on_all_ingest_apis = false # Allow fallback for /ingest/* |
| 211 | +``` |
| 212 | + |
| 213 | +```bash filename=".env" |
| 214 | +MOOSE_INGEST_API_KEY='hashed_key_for_internal_services' |
| 215 | +``` |
| 216 | + |
| 217 | +#### Use Case 2: Migration from API Keys to JWT |
| 218 | + |
| 219 | +Start with both configured, no enforcement. Gradually migrate clients to JWT. Once complete, enable enforcement: |
| 220 | + |
| 221 | +```toml filename="moose.config.toml" |
| 222 | +[jwt] |
| 223 | +secret = "..." |
| 224 | +issuer = "https://my-auth-server.com/" |
| 225 | +audience = "my-moose-app" |
| 226 | +# Start with both allowed during migration |
| 227 | +enforce_on_all_ingest_apis = false |
| 228 | +enforce_on_all_consumptions_apis = false |
| 229 | +# Later, enable to complete migration |
| 230 | +# enforce_on_all_ingest_apis = true |
| 231 | +# enforce_on_all_consumptions_apis = true |
| 232 | +``` |
| 233 | + |
| 234 | +### Admin Endpoints |
| 235 | + |
| 236 | +Admin endpoints use API key authentication exclusively (configured separately from ingest/analytics endpoints). |
| 237 | + |
| 238 | +**Configuration precedence** (highest to lowest): |
| 239 | +1. `--token` CLI parameter (plain-text token) |
| 240 | +2. `MOOSE_ADMIN_TOKEN` environment variable (plain-text token) |
| 241 | +3. `admin_api_key` in `moose.config.toml` (hashed token) |
| 242 | + |
| 243 | +**Example:** |
| 244 | + |
| 245 | +```bash |
| 246 | +# Option 1: CLI parameter |
| 247 | +moose remote plan --token your_plain_text_token |
| 248 | + |
| 249 | +# Option 2: Environment variable |
| 250 | +export MOOSE_ADMIN_TOKEN='your_plain_text_token' |
| 251 | +moose remote plan |
| 252 | + |
| 253 | +# Option 3: Config file |
| 254 | +# In moose.config.toml: |
| 255 | +# [authentication] |
| 256 | +# admin_api_key = "your_pbkdf2_hmac_sha256_hashed_key" |
| 257 | +``` |
| 258 | + |
| 259 | +## Security Best Practices |
| 260 | + |
| 261 | +- **Never commit plain-text tokens to version control** - Always use hashed keys in configuration files |
| 262 | +- **Use environment variables for production** - Keep secrets out of your codebase |
| 263 | +- **Generate unique tokens for different environments** - Separate development, staging, and production credentials |
| 264 | +- **Rotate tokens regularly** - Especially for long-running production deployments |
| 265 | +- **Choose the right method for your use case**: |
| 266 | + - Use **API Keys** for internal services and getting started |
| 267 | + - Use **JWT** when integrating with identity providers or need user-level auth |
| 268 | +- **Store hashed keys safely** - The PBKDF2 HMAC SHA256 hash in `moose.config.toml` is safe to version control, but the plain-text token should only exist in secure environment variables or secret management systems |
| 269 | + |
| 270 | +<Callout type="warning"> |
| 271 | + Never commit plain-text tokens to version control. Use hashed keys in configuration files and environment variables for production. |
| 272 | +</Callout> |
0 commit comments