@sendhoney/cli 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/bin/sendhoney +37 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ SendHoney platform CLI for authenticated API, CRUD, release, and ThunderCompute
|
|
|
11
11
|
npm install -g @sendhoney/cli
|
|
12
12
|
|
|
13
13
|
# npm (pinned)
|
|
14
|
-
npm install -g @sendhoney/cli@0.1.
|
|
14
|
+
npm install -g @sendhoney/cli@0.1.11
|
|
15
15
|
|
|
16
16
|
# Homebrew
|
|
17
17
|
brew tap sendhoney/tap && brew install sendhoney
|
package/bin/sendhoney
CHANGED
|
@@ -36,14 +36,22 @@ function openBrowser(url) {
|
|
|
36
36
|
function base64Url(value) { return value.toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_') }
|
|
37
37
|
function pkceChallenge(verifier) { return base64Url(createHash('sha256').update(verifier).digest()) }
|
|
38
38
|
|
|
39
|
-
function
|
|
39
|
+
function escapeHtml(value) {
|
|
40
|
+
return String(value || '').replace(/[&<>'"]/g, (character) => ({ '&': '&', '<': '<', '>': '>', "'": ''', '"': '"' }[character]))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function loginCallbackPage(success, organization = {}) {
|
|
40
44
|
const title = success ? 'Return to your terminal' : 'Sign-in could not be completed'
|
|
41
45
|
const message = success
|
|
42
46
|
? 'SendHoney received the authorization. The CLI is exchanging it securely now; return to your terminal for the final login result.'
|
|
43
47
|
: 'The SendHoney CLI could not complete sign-in. Close this window and try again from your terminal.'
|
|
44
48
|
const icon = success ? '✓' : '!'
|
|
45
49
|
const accent = success ? '#0f766e' : '#b42318'
|
|
46
|
-
|
|
50
|
+
const logo = organization.logoUrl || organization.logo_url || organization.logo || organization.organization?.logoUrl || organization.organization?.logo_url || organization.organization?.logo
|
|
51
|
+
const logoUrl = /^https?:\/\//i.test(String(logo || '')) ? escapeHtml(logo) : ''
|
|
52
|
+
const clientName = escapeHtml(organization.name || organization.companyName || organization.organization?.name || 'Your workspace')
|
|
53
|
+
const clientBrand = success && logoUrl ? `<div class="client-brand"><img src="${logoUrl}" alt="${clientName} logo"><span>${clientName}</span></div>` : ''
|
|
54
|
+
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${title} · SendHoney</title><style>*,*:before,*:after{box-sizing:border-box}body{margin:0;min-height:100vh;display:grid;place-items:center;padding:24px;background:#f7fbfb;color:#102a2a;font-family:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}.card{width:min(100%,480px);padding:40px;border:1px solid #d9e9e7;border-radius:24px;background:#fff;box-shadow:0 20px 60px #0f766e18;text-align:center}.brand,.client-brand{display:inline-flex;align-items:center;gap:10px;color:#0f766e;font-size:18px;font-weight:800;letter-spacing:-.02em}.brand{margin-bottom:16px}.client-brand{margin:0 auto 28px;padding:10px 14px;border:1px solid #d9e9e7;border-radius:14px;color:#214040;font-size:14px}.client-brand img{width:30px;height:30px;object-fit:contain;border-radius:8px}.mark{display:grid;place-items:center;width:32px;height:32px;border-radius:10px;background:#0f766e;color:#fff;font-size:20px}.status{display:grid;place-items:center;width:64px;height:64px;margin:0 auto 22px;border-radius:20px;background:${accent}18;color:${accent};font-size:32px;font-weight:800}.title{margin:0;font-size:30px;letter-spacing:-.04em}.message{margin:14px auto 0;max-width:30rem;color:#587070;font-size:16px;line-height:1.6}.hint{margin-top:28px;color:#789090;font-size:13px}</style></head><body><main class="card"><div class="brand"><span class="mark">SH</span><span>SendHoney</span></div>${clientBrand}<div class="status">${icon}</div><h1 class="title">${title}</h1><p class="message">${message}</p><p class="hint">You may safely close this tab.</p></main></body></html>`
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
async function browserLogin(args, config) {
|
|
@@ -58,25 +66,29 @@ async function browserLogin(args, config) {
|
|
|
58
66
|
authorization.search = new URLSearchParams({ client_id: clientId, redirect_uri: callback, response_type: 'code', scope: 'openid profile email offline_access', state, code_challenge: pkceChallenge(verifier), code_challenge_method: 'S256' }).toString()
|
|
59
67
|
const result = await new Promise((resolve, reject) => {
|
|
60
68
|
const timeout = setTimeout(() => { server.close(); reject(new Error('login timed out')) }, 300000)
|
|
61
|
-
server.on('request', (req, res) => {
|
|
69
|
+
server.on('request', async (req, res) => {
|
|
62
70
|
const url = new URL(req.url || '/', callback)
|
|
63
71
|
if (url.pathname !== '/') { res.end('SendHoney CLI login'); return }
|
|
64
72
|
if (url.searchParams.get('state') !== state) { res.statusCode = 400; res.end('Invalid login state'); clearTimeout(timeout); server.close(); reject(new Error('invalid login state')); return }
|
|
65
73
|
const code = url.searchParams.get('code'); const error = url.searchParams.get('error')
|
|
66
|
-
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
if (error || !code) { res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(loginCallbackPage(false)); clearTimeout(timeout); server.close(); reject(new Error(error || 'missing authorization code')); return }
|
|
75
|
+
try {
|
|
76
|
+
const tokenResponse = await fetch(`${authUrl}/realms/${encodeURIComponent(realm)}/protocol/openid-connect/token`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ grant_type: 'authorization_code', client_id: clientId, code, redirect_uri: callback, code_verifier: verifier }).toString() })
|
|
77
|
+
const tokens = await tokenResponse.json().catch(() => ({}))
|
|
78
|
+
if (!tokenResponse.ok || !tokens.access_token) throw new Error(`Keycloak login failed (${tokenResponse.status}): ${tokens.error_description || tokens.error || 'no access token'}`)
|
|
79
|
+
const session = await persistLoginSession(config, args, { authUrl, realm, clientId }, tokens)
|
|
80
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(loginCallbackPage(true, session.organization || {}))
|
|
81
|
+
clearTimeout(timeout); server.close(); resolve({ tokens, session })
|
|
82
|
+
} catch (failure) {
|
|
83
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(loginCallbackPage(false)); clearTimeout(timeout); server.close(); reject(failure)
|
|
84
|
+
}
|
|
69
85
|
})
|
|
70
86
|
const loginUrl = authorization.toString()
|
|
71
87
|
stdout.write(`Keycloak login URL:\n${loginUrl}\n\n`)
|
|
72
88
|
openBrowser(loginUrl)
|
|
73
89
|
stdout.write(`Opening Keycloak login in your browser (${callback})\n`)
|
|
74
90
|
})
|
|
75
|
-
|
|
76
|
-
const tokens = await tokenResponse.json().catch(() => ({}))
|
|
77
|
-
if (!tokenResponse.ok || !tokens.access_token) die(`Keycloak login failed (${tokenResponse.status}): ${tokens.error_description || tokens.error || 'no access token'}`)
|
|
78
|
-
const session = await persistLoginSession(config, args, { authUrl, realm, clientId }, tokens)
|
|
79
|
-
return json({ ok: true, authenticated: true, orgId: session.orgId || undefined, apiUrl: session.apiUrl })
|
|
91
|
+
return json({ ok: true, authenticated: true, orgId: result.session.orgId || undefined, apiUrl: result.session.apiUrl })
|
|
80
92
|
}
|
|
81
93
|
|
|
82
94
|
async function refreshToken(config) {
|
|
@@ -102,7 +114,7 @@ function json(value) { process.stdout.write(`${JSON.stringify(value, null, 2)}\n
|
|
|
102
114
|
function die(message, code = 1) { process.stderr.write(`sendhoney: ${message}\n`); process.exit(code) }
|
|
103
115
|
function organizationIdFrom(value) {
|
|
104
116
|
if (!value || typeof value !== 'object') return ''
|
|
105
|
-
return String(value.orgId || value.organizationId || value.tenantId || value.organization?.id || value.organization?.orgId || value.workspace?.organizationId || value.organizations?.[0]?.id || '').trim()
|
|
117
|
+
return String(value.orgId || value.org_id || value.organizationId || value.tenantId || value.organization?.id || value.organization?.orgId || value.workspace?.organizationId || value.organizations?.[0]?.id || '').trim()
|
|
106
118
|
}
|
|
107
119
|
|
|
108
120
|
async function persistLoginSession(config, args, auth, tokens) {
|
|
@@ -113,8 +125,18 @@ async function persistLoginSession(config, args, auth, tokens) {
|
|
|
113
125
|
fetch(`${apiUrl.replace(/\/$/, '')}/api/v1/org/me`, { headers }).catch(() => null),
|
|
114
126
|
])
|
|
115
127
|
const user = userResponse ? await userResponse.json().catch(() => ({})) : {}
|
|
116
|
-
|
|
117
|
-
|
|
128
|
+
let organization = orgResponse ? await orgResponse.json().catch(() => ({})) : {}
|
|
129
|
+
let orgId = organizationIdFrom(organization)
|
|
130
|
+
if (!orgId && user.email) {
|
|
131
|
+
const onboardingResponse = await fetch(`${apiUrl.replace(/\/$/, '')}/organizations/onboarding-status?email=${encodeURIComponent(user.email)}`, { headers }).catch(() => null)
|
|
132
|
+
const onboarding = onboardingResponse ? await onboardingResponse.json().catch(() => ({})) : {}
|
|
133
|
+
orgId = organizationIdFrom(onboarding)
|
|
134
|
+
if (orgId) {
|
|
135
|
+
const organizationResponse = await fetch(`${apiUrl.replace(/\/$/, '')}/organizations/${encodeURIComponent(orgId)}`, { headers }).catch(() => null)
|
|
136
|
+
const fullOrganization = organizationResponse ? await organizationResponse.json().catch(() => ({})) : {}
|
|
137
|
+
organization = { ...onboarding, ...fullOrganization, orgId }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
118
140
|
const session = { authenticated: true, authenticatedAt: new Date().toISOString(), user, organization, orgId: orgId || undefined }
|
|
119
141
|
saveConfig({
|
|
120
142
|
...config, apiUrl, authUrl: auth.authUrl, realm: auth.realm, clientId: auth.clientId,
|
|
@@ -122,7 +144,7 @@ async function persistLoginSession(config, args, auth, tokens) {
|
|
|
122
144
|
idToken: tokens.id_token, expiresAt: Date.now() + (tokens.expires_in || 300) * 1000,
|
|
123
145
|
...(orgId ? { orgId } : {}), session,
|
|
124
146
|
})
|
|
125
|
-
return { apiUrl, orgId }
|
|
147
|
+
return { apiUrl, orgId, user, organization }
|
|
126
148
|
}
|
|
127
149
|
|
|
128
150
|
function authStatus() {
|
package/package.json
CHANGED