@sendhoney/cli 0.1.6 → 0.1.9

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.
Files changed (3) hide show
  1. package/README.md +6 -2
  2. package/bin/sendhoney +36 -14
  3. 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.3
14
+ npm install -g @sendhoney/cli@0.1.9
15
15
 
16
16
  # Homebrew
17
17
  brew tap sendhoney/tap && brew install sendhoney
@@ -63,6 +63,9 @@ sendhoney credentials validate --secret-refs @secret-refs.json
63
63
  sendhoney login
64
64
  sendhoney resources current
65
65
 
66
+ # Inspect the local non-secret session state
67
+ sendhoney auth status
68
+
66
69
  # Generate the decoded.africa widget snippet using the saved organization
67
70
  sendhoney widget install --widget-type both
68
71
 
@@ -85,6 +88,7 @@ sendhoney widget install --org-id ORG_ID --widget-type both --file ./dist/index.
85
88
  | `login [--token TOKEN] \| [--username USER --password PASS]` | Keycloak PKCE browser login or direct token/password auth |
86
89
  | `logout` | Clear stored credentials |
87
90
  | `whoami` | Return the authenticated user profile |
91
+ | `auth status` | Show authenticated state, expiry, user, organization, and resolved org ID without tokens |
88
92
  | `menu [--operator]` | Interactive resource menu grouped by domain |
89
93
  | `resources list \| current` | List organization modules / current org |
90
94
  | `endpoints list` | List published API endpoints |
@@ -108,7 +112,7 @@ sendhoney widget install --org-id ORG_ID --widget-type both --file ./dist/index.
108
112
 
109
113
  `sendhoney login` uses Keycloak Authorization Code + PKCE and refreshes expired access tokens automatically. Use `SENDHONEY_TOKEN` for CI / service accounts.
110
114
 
111
- 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.
115
+ 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.
112
116
 
113
117
  ---
114
118
 
package/bin/sendhoney CHANGED
@@ -37,9 +37,9 @@ function base64Url(value) { return value.toString('base64').replace(/=/g, '').re
37
37
  function pkceChallenge(verifier) { return base64Url(createHash('sha256').update(verifier).digest()) }
38
38
 
39
39
  function loginCallbackPage(success) {
40
- const title = success ? 'You’re signed in' : 'Sign-in could not be completed'
40
+ const title = success ? 'Return to your terminal' : 'Sign-in could not be completed'
41
41
  const message = success
42
- ? 'SendHoney CLI is connected. You can close this window and return to your terminal.'
42
+ ? 'SendHoney received the authorization. The CLI is exchanging it securely now; return to your terminal for the final login result.'
43
43
  : 'The SendHoney CLI could not complete sign-in. Close this window and try again from your terminal.'
44
44
  const icon = success ? '✓' : '!'
45
45
  const accent = success ? '#0f766e' : '#b42318'
@@ -74,9 +74,9 @@ async function browserLogin(args, config) {
74
74
  })
75
75
  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: result.code, redirect_uri: result.callback, code_verifier: verifier }).toString() })
76
76
  const tokens = await tokenResponse.json().catch(() => ({}))
77
- if (!tokenResponse.ok || !tokens.access_token) die(`Keycloak login failed (${tokenResponse.status})`)
78
- saveConfig({ ...config, apiUrl: args['api-url'] || config.apiUrl || 'https://api.sendhoney.app', authUrl, realm, clientId, accessToken: tokens.access_token, refreshToken: tokens.refresh_token, expiresAt: Date.now() + (tokens.expires_in || 300) * 1000 })
79
- return json({ ok: true, apiUrl: args['api-url'] || config.apiUrl || 'https://api.sendhoney.app' })
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 })
80
80
  }
81
81
 
82
82
  async function refreshToken(config) {
@@ -104,6 +104,32 @@ function organizationIdFrom(value) {
104
104
  if (!value || typeof value !== 'object') return ''
105
105
  return String(value.orgId || value.organizationId || value.tenantId || value.organization?.id || value.organization?.orgId || value.workspace?.organizationId || value.organizations?.[0]?.id || '').trim()
106
106
  }
107
+
108
+ async function persistLoginSession(config, args, auth, tokens) {
109
+ const apiUrl = args['api-url'] || config.apiUrl || 'https://api.sendhoney.app'
110
+ const headers = { Accept: 'application/json', Authorization: `Bearer ${tokens.access_token}` }
111
+ const [userResponse, orgResponse] = await Promise.all([
112
+ fetch(`${auth.authUrl}/realms/${encodeURIComponent(auth.realm)}/protocol/openid-connect/userinfo`, { headers }).catch(() => null),
113
+ fetch(`${apiUrl.replace(/\/$/, '')}/api/v1/org/me`, { headers }).catch(() => null),
114
+ ])
115
+ const user = userResponse ? await userResponse.json().catch(() => ({})) : {}
116
+ const organization = orgResponse ? await orgResponse.json().catch(() => ({})) : {}
117
+ const orgId = organizationIdFrom(organization)
118
+ const session = { authenticated: true, authenticatedAt: new Date().toISOString(), user, organization, orgId: orgId || undefined }
119
+ saveConfig({
120
+ ...config, apiUrl, authUrl: auth.authUrl, realm: auth.realm, clientId: auth.clientId,
121
+ accessToken: tokens.access_token, refreshToken: tokens.refresh_token || config.refreshToken,
122
+ idToken: tokens.id_token, expiresAt: Date.now() + (tokens.expires_in || 300) * 1000,
123
+ ...(orgId ? { orgId } : {}), session,
124
+ })
125
+ return { apiUrl, orgId }
126
+ }
127
+
128
+ function authStatus() {
129
+ const config = loadConfig()
130
+ const expiresAt = Number(config.expiresAt || 0)
131
+ 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 })
132
+ }
107
133
  function redactCredential(value) {
108
134
  if (Array.isArray(value)) return value.map(redactCredential)
109
135
  if (!value || typeof value !== 'object') return value
@@ -354,7 +380,7 @@ async function login(args) {
354
380
  }
355
381
  const authUrl = (args['auth-url'] || process.env.SENDHONEY_AUTH_URL || config.authUrl || 'https://auth.sendhoney.app').replace(/\/$/, '')
356
382
  const realm = args.realm || process.env.SENDHONEY_KEYCLOAK_REALM || config.realm || 'users'
357
- const clientId = args['client-id'] || process.env.SENDHONEY_CLIENT_ID || config.clientId || 'sendhoney-mobile'
383
+ const clientId = args['client-id'] || process.env.SENDHONEY_CLIENT_ID || config.clientId || 'sendhoney-cli'
358
384
  const username = args.username || process.env.SENDHONEY_USERNAME
359
385
  const password = args.password || process.env.SENDHONEY_PASSWORD //no-secret-check
360
386
  if (!username || !password) die('login requires --username and --password (or SENDHONEY_USERNAME/SENDHONEY_PASSWORD)')
@@ -364,13 +390,8 @@ async function login(args) {
364
390
  })
365
391
  const body = await response.json().catch(() => ({}))
366
392
  if (!response.ok || !body.access_token) die(`login failed (${response.status}): ${body.error_description || body.error || 'no access token'}`)
367
- const nextConfig = { ...config, apiUrl: args['api-url'] || config.apiUrl || 'https://api.sendhoney.app', authUrl, realm, clientId, accessToken: body.access_token, refreshToken: body.refresh_token, expiresAt: Date.now() + (body.expires_in || 300) * 1000 }
368
- saveConfig(nextConfig)
369
- const orgResponse = await fetch(`${nextConfig.apiUrl.replace(/\/$/, '')}/api/v1/org/me`, { headers: { Accept: 'application/json', Authorization: `Bearer ${body.access_token}` } }).catch(() => null)
370
- const orgBody = orgResponse ? await orgResponse.json().catch(() => ({})) : {}
371
- const orgId = organizationIdFrom(orgBody)
372
- if (orgId) saveConfig({ ...nextConfig, orgId })
373
- json({ ok: true, username, apiUrl: args['api-url'] || config.apiUrl || 'https://api.sendhoney.app' })
393
+ const session = await persistLoginSession(config, args, { authUrl, realm, clientId }, body)
394
+ json({ ok: true, username, authenticated: true, orgId: session.orgId || undefined, apiUrl: session.apiUrl })
374
395
  }
375
396
 
376
397
  async function main() {
@@ -389,7 +410,8 @@ async function main() {
389
410
  if (args.token || process.env.SENDHONEY_TOKEN || args.username || process.env.SENDHONEY_USERNAME) return login(args)
390
411
  return browserLogin(args, config)
391
412
  }
392
- if (command === 'logout') { const c = loadConfig(); delete c.accessToken; delete c.refreshToken; delete c.expiresAt; saveConfig(c); return json({ ok: true }) }
413
+ if (command === 'logout') { const c = loadConfig(); delete c.accessToken; delete c.refreshToken; delete c.idToken; delete c.expiresAt; delete c.session; saveConfig(c); return json({ ok: true }) }
414
+ if (command === 'auth' && subcommand === 'status') return authStatus()
393
415
  if (command === 'menu') return interactiveMenu(args)
394
416
  if (command === 'credentials') return credentials(args, subcommand)
395
417
  if (command === 'widget' && subcommand === 'install') return widgetInstall(args)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sendhoney/cli",
3
- "version": "0.1.6",
3
+ "version": "0.1.9",
4
4
  "description": "SendHoney platform CLI for authenticated API, CRUD, release, and ThunderCompute operations.",
5
5
  "private": false,
6
6
  "bin": { "sendhoney": "bin/sendhoney" },