Commit b264918
feat(oauth): auto-persist encryption key and auto-detect url (#532)
* feat(oauth): auto-persist encryption key to ~/.ha-mcp/oauth_key
Users no longer need to manually generate OAUTH_ENCRYPTION_KEY.
The key is automatically generated on first run and persisted to
~/.ha-mcp/oauth_key, surviving server restarts.
Key priority:
1. OAUTH_ENCRYPTION_KEY env var (advanced users/multi-instance)
2. ~/.ha-mcp/oauth_key (auto-generated, persists)
3. Temporary key (dev/testing only)
Benefits:
- Zero configuration for users
- Tokens survive restarts automatically
- Secure file permissions (0600)
- Multi-instance support via file copy or env var
- Actual security = HA LLAT revocation (OAuth token is just container)
Added tests for key persistence and env var override.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* docs: remove OAUTH_ENCRYPTION_KEY from setup instructions
Key is now auto-generated and persisted to ~/.ha-mcp/oauth_key.
Users no longer need to manually generate or set the encryption key.
Updated:
- Removed OAUTH_ENCRYPTION_KEY from Docker/uvx examples
- Removed from environment variables table
- Updated FAQ to explain automatic persistence
- Added multi-instance deployment instructions
* feat(oauth): auto-detect base URL from incoming requests
Users no longer need to set MCP_BASE_URL. The server automatically
detects its public URL from request headers (X-Forwarded-Host,
X-Forwarded-Proto) when using tunnels or reverse proxies.
How it works:
1. MCP_BASE_URL env var (if set) - Manual override
2. Auto-detect from first request - Uses proxy headers
3. Cache detected URL - Reused for subsequent requests
Benefits:
- Zero configuration for typical users
- Works automatically with Cloudflare Tunnel, ngrok, etc.
- Respects X-Forwarded-* headers from reverse proxies
- Manual override still available for advanced setups
Changes:
- Made base_url optional in HomeAssistantOAuthProvider
- Added _get_base_url(request) method for auto-detection
- Updated all base_url usage to support auto-detection
- Updated __main__.py to not require MCP_BASE_URL
- Updated docs to remove MCP_BASE_URL from setup
Tests:
- Added test_base_url_auto_detection - Verifies detection from headers
- Added test_base_url_configured_takes_precedence - Ensures override works
- All 43 OAuth tests pass ✅
Example:
# Before (required MCP_BASE_URL)
docker run -e MCP_BASE_URL=https://tunnel.com ha-mcp-oauth
# After (auto-detected)
docker run -p 8086:8086 ha-mcp-oauth
# Just access via tunnel - URL detected automatically!
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 7445cb3 commit b264918
4 files changed
Lines changed: 216 additions & 47 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
32 | 30 | | |
33 | 31 | | |
34 | 32 | | |
35 | 33 | | |
36 | 34 | | |
37 | 35 | | |
38 | | - | |
39 | | - | |
40 | 36 | | |
41 | 37 | | |
42 | 38 | | |
43 | | - | |
| 39 | + | |
44 | 40 | | |
45 | | - | |
| 41 | + | |
46 | 42 | | |
47 | | - | |
48 | | - | |
49 | | - | |
| 43 | + | |
| 44 | + | |
50 | 45 | | |
51 | 46 | | |
52 | 47 | | |
| |||
73 | 68 | | |
74 | 69 | | |
75 | 70 | | |
76 | | - | |
| 71 | + | |
77 | 72 | | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
84 | 76 | | |
85 | 77 | | |
86 | | - | |
| 78 | + | |
87 | 79 | | |
88 | 80 | | |
89 | 81 | | |
| |||
97 | 89 | | |
98 | 90 | | |
99 | 91 | | |
100 | | - | |
| 92 | + | |
101 | 93 | | |
102 | | - | |
| 94 | + | |
103 | 95 | | |
| 96 | + | |
104 | 97 | | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 98 | + | |
110 | 99 | | |
111 | 100 | | |
| 101 | + | |
| 102 | + | |
112 | 103 | | |
113 | 104 | | |
114 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
642 | 642 | | |
643 | 643 | | |
644 | 644 | | |
645 | | - | |
| 645 | + | |
646 | 646 | | |
647 | 647 | | |
648 | 648 | | |
| |||
662 | 662 | | |
663 | 663 | | |
664 | 664 | | |
665 | | - | |
| 665 | + | |
666 | 666 | | |
667 | 667 | | |
668 | 668 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
| 83 | + | |
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
141 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
142 | 148 | | |
143 | 149 | | |
144 | 150 | | |
145 | | - | |
| 151 | + | |
146 | 152 | | |
147 | | - | |
148 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
149 | 157 | | |
| 158 | + | |
150 | 159 | | |
151 | 160 | | |
152 | 161 | | |
153 | 162 | | |
154 | | - | |
155 | | - | |
156 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
157 | 194 | | |
158 | | - | |
159 | | - | |
160 | | - | |
| 195 | + | |
| 196 | + | |
161 | 197 | | |
162 | | - | |
| 198 | + | |
| 199 | + | |
163 | 200 | | |
164 | 201 | | |
165 | 202 | | |
| |||
180 | 217 | | |
181 | 218 | | |
182 | 219 | | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
183 | 269 | | |
184 | 270 | | |
185 | 271 | | |
| |||
201 | 287 | | |
202 | 288 | | |
203 | 289 | | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
204 | 293 | | |
205 | 294 | | |
206 | | - | |
| 295 | + | |
207 | 296 | | |
208 | 297 | | |
209 | 298 | | |
| |||
346 | 435 | | |
347 | 436 | | |
348 | 437 | | |
349 | | - | |
350 | | - | |
| 438 | + | |
| 439 | + | |
351 | 440 | | |
352 | 441 | | |
353 | 442 | | |
| |||
435 | 524 | | |
436 | 525 | | |
437 | 526 | | |
438 | | - | |
| 527 | + | |
439 | 528 | | |
440 | 529 | | |
441 | 530 | | |
442 | 531 | | |
443 | 532 | | |
444 | 533 | | |
445 | 534 | | |
446 | | - | |
| 535 | + | |
447 | 536 | | |
448 | 537 | | |
449 | 538 | | |
| |||
452 | 541 | | |
453 | 542 | | |
454 | 543 | | |
455 | | - | |
| 544 | + | |
456 | 545 | | |
457 | 546 | | |
458 | 547 | | |
459 | 548 | | |
460 | 549 | | |
461 | 550 | | |
462 | 551 | | |
463 | | - | |
| 552 | + | |
464 | 553 | | |
465 | 554 | | |
466 | 555 | | |
| |||
0 commit comments