| @@ -1,4 +1,7 @@ |
| 1 | 1 | import { intro, outro, spinner, log } from "@clack/prompts"; |
| 2 | import { mkdirSync, writeFileSync, existsSync } from "node:fs"; |
| 3 | import { join } from "node:path"; |
| 4 | import { homedir } from "node:os"; |
| 2 | 5 | import { waitForAuthCallback } from "../auth-server.js"; |
| 3 | 6 | import { loadConfig, saveConfig } from "../config.js"; |
| 4 | 7 | |
| @@ -41,6 +44,29 @@ |
| 41 | 44 | throw new Error("Authentication timed out (10 minutes)"); |
| 42 | 45 | } |
| 43 | 46 | |
| 47 | async function provisionTlsCerts(hub: string, token: string) { |
| 48 | const s = spinner(); |
| 49 | s.start("Downloading TLS certificates"); |
| 50 | try { |
| 51 | const res = await fetch(`${hub}/api/auth/tls-certs`, { |
| 52 | headers: { Authorization: `Bearer ${token}` }, |
| 53 | }); |
| 54 | if (!res.ok) { |
| 55 | s.stop("TLS certificates not available (non-fatal)"); |
| 56 | return; |
| 57 | } |
| 58 | const { ca, cert, key } = await res.json() as { ca: string; cert: string; key: string }; |
| 59 | const tlsDir = join(homedir(), ".grove", "tls"); |
| 60 | mkdirSync(tlsDir, { recursive: true }); |
| 61 | writeFileSync(join(tlsDir, "ca.crt"), ca); |
| 62 | writeFileSync(join(tlsDir, "client.crt"), cert); |
| 63 | writeFileSync(join(tlsDir, "client.key"), key, { mode: 0o600 }); |
| 64 | s.stop("TLS certificates saved"); |
| 65 | } catch { |
| 66 | s.stop("TLS certificates not available (non-fatal)"); |
| 67 | } |
| 68 | } |
| 69 | |
| 44 | 70 | export async function authLogin(args: string[]) { |
| 45 | 71 | const config = await loadConfig(); |
| 46 | 72 | |
| @@ -58,6 +84,7 @@ |
| 58 | 84 | const token = await deviceCodeFlow(config); |
| 59 | 85 | config.token = token; |
| 60 | 86 | await saveConfig(config); |
| 87 | await provisionTlsCerts(config.hub, token); |
| 61 | 88 | outro("Authenticated! Token saved to ~/.grove/config.json"); |
| 62 | 89 | } catch (err: any) { |
| 63 | 90 | log.error(err.message); |
| @@ -85,6 +112,7 @@ |
| 85 | 112 | s.stop("Authentication received"); |
| 86 | 113 | config.token = token; |
| 87 | 114 | await saveConfig(config); |
| 115 | await provisionTlsCerts(config.hub, token); |
| 88 | 116 | outro("Authenticated! Token saved to ~/.grove/config.json"); |
| 89 | 117 | } catch (err: any) { |
| 90 | 118 | s.stop("Authentication failed"); |
| 91 | 119 | |