|
35 | 35 | method: builtin |
36 | 36 | session_max_age: 24h # How long sessions last (default: 24h) |
37 | 37 | secure_cookies: true # Set true if serving over HTTPS |
38 | | - api_key: "your-secret-key" # Optional: for API access without login |
| 38 | + api_key_hash: "$2a$10$..." # Optional: bcrypt hash of API key (see below) |
39 | 39 | users: |
40 | 40 | - username: admin |
41 | 41 | password_hash: "$2a$10$..." # bcrypt hash |
@@ -225,13 +225,44 @@ client_secret: ${OIDC_CLIENT_SECRET} |
225 | 225 |
|
226 | 226 | ## API Key Authentication |
227 | 227 |
|
228 | | -When `api_key` is set in the auth config, you can authenticate API requests using the `X-Api-Key` header instead of a session cookie. This is useful for integrations, scripts, and automated tools. |
| 228 | +When `api_key_hash` is set in the auth config, you can authenticate API requests using the `X-Api-Key` header instead of a session cookie. This is useful for integrations, scripts, and automated tools. |
229 | 229 |
|
230 | 230 | ```bash |
231 | 231 | curl -H "X-Api-Key: your-secret-key" https://muximux.example.com/api/apps |
232 | 232 | ``` |
233 | 233 |
|
234 | | -The API key is checked using constant-time comparison to prevent timing attacks. |
| 234 | +### How It Works |
| 235 | + |
| 236 | +The API key is stored as a **bcrypt hash** in `config.yaml` -- not as plaintext. When a request arrives with `X-Api-Key`, Muximux verifies it against the stored hash using `bcrypt.CompareHashAndPassword`. This means: |
| 237 | + |
| 238 | +- The original API key cannot be recovered from the config file |
| 239 | +- If `config.yaml` is compromised, the attacker cannot extract the key |
| 240 | +- Verification is constant-time, preventing timing attacks |
| 241 | + |
| 242 | +### Generating an API Key Hash |
| 243 | + |
| 244 | +Use the same tools as for password hashes: |
| 245 | + |
| 246 | +```bash |
| 247 | +# Using the hashpw utility |
| 248 | +./hashpw 'my-api-key' |
| 249 | +
|
| 250 | +# Using htpasswd |
| 251 | +htpasswd -nbBC 10 "" 'my-api-key' | cut -d: -f2 |
| 252 | +
|
| 253 | +# Using Python |
| 254 | +python3 -c "import bcrypt; print(bcrypt.hashpw(b'my-api-key', bcrypt.gensalt()).decode())" |
| 255 | +``` |
| 256 | + |
| 257 | +Then add the hash to your config: |
| 258 | + |
| 259 | +```yaml |
| 260 | +auth: |
| 261 | + method: builtin |
| 262 | + api_key_hash: "$2a$10$..." |
| 263 | +``` |
| 264 | + |
| 265 | +You can also set the API key through the **Settings > Security** panel in the Muximux UI. The UI hashes the key automatically before storing it. |
235 | 266 |
|
236 | 267 | --- |
237 | 268 |
|
|
0 commit comments