@sendhoney/cli 0.1.13 → 0.1.14
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 +3 -3
- package/bin/sendhoney +35 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,9 +11,9 @@ 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.14
|
|
15
15
|
|
|
16
|
-
sendhoney --version # should print 0.1.
|
|
16
|
+
sendhoney --version # should print 0.1.14
|
|
17
17
|
|
|
18
18
|
# Homebrew
|
|
19
19
|
brew tap sendhoney/tap && brew install sendhoney
|
|
@@ -112,7 +112,7 @@ sendhoney widget install --org-id ORG_ID --widget-type both --file ./dist/index.
|
|
|
112
112
|
|
|
113
113
|
## Authentication
|
|
114
114
|
|
|
115
|
-
`sendhoney login` uses Keycloak Authorization Code + PKCE and refreshes expired access tokens automatically. On success it prints the authenticated user, resolved `orgId`, and organization data. Use `SENDHONEY_TOKEN` for CI / service accounts.
|
|
115
|
+
`sendhoney login` uses Keycloak Authorization Code + PKCE and refreshes expired access tokens automatically. The loopback callback completes immediately after the code exchange; workspace hydration then retries automatically with bounded backoff. On success it prints the authenticated user, resolved `orgId`, and organization data. If the workspace API is unavailable, `auth status` reports `AUTHENTICATED_WORKSPACE_PENDING` without requiring another browser action. Use `SENDHONEY_TOKEN` for CI / service accounts.
|
|
116
116
|
|
|
117
117
|
Credentials are stored in `$XDG_CONFIG_HOME/sendhoney/config.json` (or `~/.config/sendhoney/config.json`) with mode `0600`; tokens are never printed by the CLI. After browser login, the CLI exchanges the PKCE code, hydrates the Keycloak user profile and current organization, and stores the resolved `orgId` in the local session. `sendhoney widget install` can therefore use the saved organization without a second ID lookup.
|
|
118
118
|
|
package/bin/sendhoney
CHANGED
|
@@ -77,9 +77,14 @@ async function browserLogin(args, config) {
|
|
|
77
77
|
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() })
|
|
78
78
|
const tokens = await tokenResponse.json().catch(() => ({}))
|
|
79
79
|
if (!tokenResponse.ok || !tokens.access_token) throw new Error(`Keycloak login failed (${tokenResponse.status}): ${tokens.error_description || tokens.error || 'no access token'}`)
|
|
80
|
-
const session = await persistLoginSession(config, args, { authUrl, realm, clientId }, tokens)
|
|
80
|
+
const session = await persistLoginSession(config, args, { authUrl, realm, clientId }, tokens, { hydrate: false })
|
|
81
81
|
res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(loginCallbackPage(true, session.organization || {}))
|
|
82
|
-
clearTimeout(timeout); server.close()
|
|
82
|
+
clearTimeout(timeout); server.close()
|
|
83
|
+
stdout.write('✓ Keycloak authentication complete. Resolving workspace…\n')
|
|
84
|
+
const hydratedSession = await hydrateWorkspaceWithRetries(config, args, { authUrl, realm, clientId }, tokens, session)
|
|
85
|
+
if (hydratedSession.orgId) stdout.write(`✓ Workspace ready: ${hydratedSession.organization?.name || hydratedSession.orgId}\n`)
|
|
86
|
+
else stdout.write('! Authentication succeeded, but workspace resolution is still pending.\n')
|
|
87
|
+
resolve({ tokens, session: hydratedSession })
|
|
83
88
|
} catch (failure) {
|
|
84
89
|
res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(loginCallbackPage(false)); clearTimeout(timeout); server.close(); reject(failure)
|
|
85
90
|
}
|
|
@@ -118,12 +123,16 @@ function organizationIdFrom(value) {
|
|
|
118
123
|
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()
|
|
119
124
|
}
|
|
120
125
|
|
|
121
|
-
|
|
126
|
+
const workspaceRetryDelays = [0, 1000, 2000, 4000, 8000]
|
|
127
|
+
|
|
128
|
+
function sleep(milliseconds) { return new Promise((resolve) => setTimeout(resolve, milliseconds)) }
|
|
129
|
+
|
|
130
|
+
async function persistLoginSession(config, args, auth, tokens, { hydrate = true } = {}) {
|
|
122
131
|
const apiUrl = args['api-url'] || config.apiUrl || 'https://api.sendhoney.app'
|
|
123
132
|
const headers = { Accept: 'application/json', Authorization: `Bearer ${tokens.access_token}` }
|
|
124
133
|
const [userResponse, orgResponse] = await Promise.all([
|
|
125
134
|
fetch(`${auth.authUrl}/realms/${encodeURIComponent(auth.realm)}/protocol/openid-connect/userinfo`, { headers }).catch(() => null),
|
|
126
|
-
fetch(`${apiUrl.replace(/\/$/, '')}/api/v1/org/me`, { headers }).catch(() => null),
|
|
135
|
+
hydrate ? fetch(`${apiUrl.replace(/\/$/, '')}/api/v1/org/me`, { headers }).catch(() => null) : Promise.resolve(null),
|
|
127
136
|
])
|
|
128
137
|
const user = userResponse ? await userResponse.json().catch(() => ({})) : {}
|
|
129
138
|
let organization = orgResponse ? await orgResponse.json().catch(() => ({})) : {}
|
|
@@ -138,20 +147,37 @@ async function persistLoginSession(config, args, auth, tokens) {
|
|
|
138
147
|
organization = { ...onboarding, ...fullOrganization, orgId }
|
|
139
148
|
}
|
|
140
149
|
}
|
|
141
|
-
const session = {
|
|
150
|
+
const session = {
|
|
151
|
+
authenticated: true,
|
|
152
|
+
authenticatedAt: new Date().toISOString(),
|
|
153
|
+
workspaceState: orgId ? 'AUTHENTICATED_WORKSPACE_READY' : 'AUTHENTICATED_WORKSPACE_PENDING',
|
|
154
|
+
user,
|
|
155
|
+
organization,
|
|
156
|
+
orgId: orgId || undefined,
|
|
157
|
+
}
|
|
142
158
|
saveConfig({
|
|
143
159
|
...config, apiUrl, authUrl: auth.authUrl, realm: auth.realm, clientId: auth.clientId,
|
|
144
160
|
accessToken: tokens.access_token, refreshToken: tokens.refresh_token || config.refreshToken,
|
|
145
161
|
idToken: tokens.id_token, expiresAt: Date.now() + (tokens.expires_in || 300) * 1000,
|
|
146
162
|
...(orgId ? { orgId } : {}), session,
|
|
147
163
|
})
|
|
148
|
-
return { apiUrl, orgId, user, organization }
|
|
164
|
+
return { apiUrl, orgId, user, organization, workspaceState: session.workspaceState }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function hydrateWorkspaceWithRetries(config, args, auth, tokens, initialSession) {
|
|
168
|
+
let session = initialSession
|
|
169
|
+
for (const delay of workspaceRetryDelays) {
|
|
170
|
+
if (delay) await sleep(delay)
|
|
171
|
+
session = await persistLoginSession(config, args, auth, tokens)
|
|
172
|
+
if (session.orgId) return session
|
|
173
|
+
}
|
|
174
|
+
return session
|
|
149
175
|
}
|
|
150
176
|
|
|
151
177
|
function authStatus() {
|
|
152
178
|
const config = loadConfig()
|
|
153
179
|
const expiresAt = Number(config.expiresAt || 0)
|
|
154
|
-
json({ authenticated: Boolean(config.accessToken) && (!expiresAt || expiresAt > Date.now()), expiresAt: expiresAt ? new Date(expiresAt).toISOString() : undefined, orgId: config.orgId || config.session?.orgId, user: config.session?.user, organization: config.session?.organization, configFile })
|
|
180
|
+
json({ authenticated: Boolean(config.accessToken) && (!expiresAt || expiresAt > Date.now()), workspaceState: config.session?.workspaceState || (config.orgId ? 'AUTHENTICATED_WORKSPACE_READY' : 'AUTHENTICATED_WORKSPACE_PENDING'), expiresAt: expiresAt ? new Date(expiresAt).toISOString() : undefined, orgId: config.orgId || config.session?.orgId, user: config.session?.user, organization: config.session?.organization, configFile })
|
|
155
181
|
}
|
|
156
182
|
function redactCredential(value) {
|
|
157
183
|
if (Array.isArray(value)) return value.map(redactCredential)
|
|
@@ -413,7 +439,8 @@ async function login(args) {
|
|
|
413
439
|
})
|
|
414
440
|
const body = await response.json().catch(() => ({}))
|
|
415
441
|
if (!response.ok || !body.access_token) die(`login failed (${response.status}): ${body.error_description || body.error || 'no access token'}`)
|
|
416
|
-
const
|
|
442
|
+
const initialSession = await persistLoginSession(config, args, { authUrl, realm, clientId }, body, { hydrate: false })
|
|
443
|
+
const session = await hydrateWorkspaceWithRetries(config, args, { authUrl, realm, clientId }, body, initialSession)
|
|
417
444
|
json({ ok: true, username, authenticated: true, user: session.user, organization: session.organization, orgId: session.orgId || undefined, apiUrl: session.apiUrl })
|
|
418
445
|
}
|
|
419
446
|
|
package/package.json
CHANGED