@greenmind/cli 0.0.0-stage → 0.9.31
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/package.json +39 -4
- package/src/bin.ts +15 -0
- package/src/bundle.ts +100 -0
- package/src/client.ts +515 -0
- package/src/command.ts +638 -0
- package/src/commands.ts +251 -0
- package/src/constants.ts +37 -0
- package/src/credentials.ts +159 -0
- package/src/environment-commands.ts +549 -0
- package/src/ext-config.ts +107 -0
- package/src/identity.ts +287 -0
- package/src/index.ts +58 -0
- package/src/json-read.ts +29 -0
- package/src/load.ts +123 -0
- package/src/protocol.ts +30 -0
- package/src/session.ts +265 -0
- package/README.md +0 -3
package/src/session.ts
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import type { ExtToolDiscovery } from '@greenmind/ext-schema'
|
|
2
|
+
import { PlatformClient, readToolDiscovery, type TokenSource } from './client'
|
|
3
|
+
import type { Print } from './commands'
|
|
4
|
+
import {
|
|
5
|
+
BASE_SCOPES,
|
|
6
|
+
CLIENT_ID,
|
|
7
|
+
DEFAULT_PLATFORM_URL,
|
|
8
|
+
ENV_ISSUER,
|
|
9
|
+
ENV_KEY,
|
|
10
|
+
ENV_URL,
|
|
11
|
+
} from './constants'
|
|
12
|
+
import {
|
|
13
|
+
credentialsDir,
|
|
14
|
+
loginKey,
|
|
15
|
+
readLogin,
|
|
16
|
+
type StoredLogin,
|
|
17
|
+
storedPlatforms,
|
|
18
|
+
withLock,
|
|
19
|
+
writeLogin,
|
|
20
|
+
} from './credentials'
|
|
21
|
+
import { deviceLogin, type IdentityOptions, refreshLogin, revokeLogin } from './identity'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Choosing the credential a command calls the platform with, and the sign-in commands that make
|
|
25
|
+
* one. A publish key, from `--key` or the environment, always wins and no stored login is read, so
|
|
26
|
+
* a build server never signs in; otherwise the stored login for the platform's address is used.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export class SessionError extends Error {
|
|
30
|
+
override readonly name = 'SessionError'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface SessionOptions {
|
|
34
|
+
readonly url?: string
|
|
35
|
+
readonly key?: string
|
|
36
|
+
readonly env?: NodeJS.ProcessEnv
|
|
37
|
+
/** The credentials directory; the user's own unless a test names one. */
|
|
38
|
+
readonly dir?: string
|
|
39
|
+
readonly identity?: IdentityOptions
|
|
40
|
+
readonly fetch?: typeof fetch
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* What a command accepts: `optional` runs without one (a check that stays offline), `any` takes a
|
|
45
|
+
* publish key or a person, `person` only a person, since a publish key cannot do what it does.
|
|
46
|
+
*/
|
|
47
|
+
export type Need = 'optional' | 'any' | 'person'
|
|
48
|
+
|
|
49
|
+
class StoredTokens implements TokenSource {
|
|
50
|
+
constructor(
|
|
51
|
+
private readonly dir: string,
|
|
52
|
+
private readonly platformUrl: string,
|
|
53
|
+
private readonly identity: IdentityOptions,
|
|
54
|
+
) {}
|
|
55
|
+
|
|
56
|
+
async accessToken(): Promise<string> {
|
|
57
|
+
const held = readLogin(this.dir, this.platformUrl)
|
|
58
|
+
if (!held) throw notSignedIn(this.platformUrl)
|
|
59
|
+
const now = this.identity.now ?? Date.now
|
|
60
|
+
if (Date.parse(held.expiresAt) - now() > 30_000) return held.accessToken
|
|
61
|
+
const renewed = await refreshLogin(this.dir, this.platformUrl, undefined, this.identity)
|
|
62
|
+
// With nothing to renew it with, the server gets to say the token has ended.
|
|
63
|
+
return renewed?.accessToken ?? held.accessToken
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async refresh(rejected: string): Promise<string | undefined> {
|
|
67
|
+
return (await refreshLogin(this.dir, this.platformUrl, rejected, this.identity))?.accessToken
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function notSignedIn(platformUrl: string | undefined): SessionError {
|
|
72
|
+
return new SessionError(
|
|
73
|
+
platformUrl
|
|
74
|
+
? `You are not signed in to ${platformUrl}. Run \`gm login --url ${platformUrl}\`.`
|
|
75
|
+
: 'You are not signed in. Run `gm login`.',
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function keyOf(options: SessionOptions): string | undefined {
|
|
80
|
+
return options.key ?? (options.env ?? process.env)[ENV_KEY] ?? undefined
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function urlOf(options: SessionOptions): string | undefined {
|
|
84
|
+
return options.url ?? (options.env ?? process.env)[ENV_URL] ?? undefined
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The client a command calls with, or none where the command may go without. `what` begins the
|
|
89
|
+
* message for a command that has nothing to call with, such as "Pushing".
|
|
90
|
+
*/
|
|
91
|
+
export function connect(
|
|
92
|
+
options: SessionOptions,
|
|
93
|
+
need: Exclude<Need, 'optional'>,
|
|
94
|
+
what: string,
|
|
95
|
+
): PlatformClient
|
|
96
|
+
export function connect(
|
|
97
|
+
options: SessionOptions,
|
|
98
|
+
need: Need,
|
|
99
|
+
what: string,
|
|
100
|
+
): PlatformClient | undefined
|
|
101
|
+
export function connect(
|
|
102
|
+
options: SessionOptions,
|
|
103
|
+
need: Need,
|
|
104
|
+
what: string,
|
|
105
|
+
): PlatformClient | undefined {
|
|
106
|
+
const key = keyOf(options)
|
|
107
|
+
const explicitUrl = urlOf(options)
|
|
108
|
+
if (key) {
|
|
109
|
+
if (need === 'person') {
|
|
110
|
+
throw new SessionError(
|
|
111
|
+
`${what} needs you signed in: a publish key (--key or ${ENV_KEY}) cannot do it. Leave the key out and run \`gm login\`.`,
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
if (!explicitUrl) {
|
|
115
|
+
throw new SessionError(
|
|
116
|
+
`Both a platform address (--url or ${ENV_URL}) and a publish key (--key or ${ENV_KEY}) are needed.`,
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
return new PlatformClient({ baseUrl: explicitUrl, publishKey: key, fetch: options.fetch })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const dir = options.dir ?? credentialsDir(options.env)
|
|
123
|
+
if (!explicitUrl && need === 'optional') return undefined
|
|
124
|
+
let platformUrl = explicitUrl
|
|
125
|
+
if (!platformUrl) {
|
|
126
|
+
const stored = storedPlatforms(dir)
|
|
127
|
+
if (stored.length === 1) platformUrl = stored[0]
|
|
128
|
+
else if (need === 'person') throw notSignedIn(undefined)
|
|
129
|
+
}
|
|
130
|
+
if (!platformUrl) {
|
|
131
|
+
throw new SessionError(
|
|
132
|
+
`${what} needs a platform address (--url or ${ENV_URL}) and a publish key (--key or ${ENV_KEY}), or a stored sign-in: run \`gm login\`.`,
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
if (!readLogin(dir, platformUrl)) {
|
|
136
|
+
if (need === 'person') throw notSignedIn(platformUrl)
|
|
137
|
+
throw new SessionError(
|
|
138
|
+
`Both a platform address (--url or ${ENV_URL}) and a publish key (--key or ${ENV_KEY}) are needed, or sign in with \`gm login --url ${platformUrl}\`.`,
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
return new PlatformClient({
|
|
142
|
+
baseUrl: platformUrl,
|
|
143
|
+
tokens: new StoredTokens(dir, platformUrl, options.identity ?? {}),
|
|
144
|
+
fetch: options.fetch,
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A stored sign-in and the platform address it is kept under. */
|
|
149
|
+
export interface SignedIn {
|
|
150
|
+
readonly platformUrl: string
|
|
151
|
+
readonly login: StoredLogin
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The stored sign-in a command run with these options would use, and the platform it is for. A
|
|
156
|
+
* command that has to know what the sign-in carries (its scopes, say) reads it here.
|
|
157
|
+
*/
|
|
158
|
+
export function signedInAs(options: SessionOptions): SignedIn {
|
|
159
|
+
const dir = options.dir ?? credentialsDir(options.env)
|
|
160
|
+
const platformUrl = urlOf(options) ?? storedPlatforms(dir)[0]
|
|
161
|
+
const login = platformUrl ? readLogin(dir, platformUrl) : undefined
|
|
162
|
+
if (!platformUrl || !login) throw notSignedIn(platformUrl)
|
|
163
|
+
return { platformUrl: loginKey(platformUrl), login }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface LoginOptions extends SessionOptions {
|
|
167
|
+
readonly issuer?: string
|
|
168
|
+
readonly clientId?: string
|
|
169
|
+
/** Asked for beside the scopes the platform names. */
|
|
170
|
+
readonly extraScopes?: readonly string[]
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Where and how to sign in to a platform: what the platform itself says (`readToolDiscovery`), each
|
|
175
|
+
* value overridden by a flag or the environment where one is given. With an issuer given by hand,
|
|
176
|
+
* a platform that cannot say is not asked at all, so a stage older than the discovery route can
|
|
177
|
+
* still be signed in to.
|
|
178
|
+
*/
|
|
179
|
+
async function signInPlan(options: LoginOptions, platformUrl: string) {
|
|
180
|
+
const env = options.env ?? process.env
|
|
181
|
+
const givenIssuer = options.issuer ?? env[ENV_ISSUER]
|
|
182
|
+
let told: ExtToolDiscovery | undefined
|
|
183
|
+
try {
|
|
184
|
+
told = await readToolDiscovery(platformUrl, options.fetch)
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (!givenIssuer) throw error
|
|
187
|
+
}
|
|
188
|
+
const scopes = told?.scopes ?? [...BASE_SCOPES]
|
|
189
|
+
return {
|
|
190
|
+
issuer: givenIssuer ?? told?.issuer ?? '',
|
|
191
|
+
clientId: options.clientId ?? told?.clientId ?? CLIENT_ID,
|
|
192
|
+
scopes: [...new Set([...scopes, ...(options.extraScopes ?? [])])],
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Signs in with the device flow and keeps the tokens under the platform's address. */
|
|
197
|
+
export async function runLogin(options: LoginOptions, print: Print): Promise<boolean> {
|
|
198
|
+
const platformUrl = loginKey(urlOf(options) ?? DEFAULT_PLATFORM_URL)
|
|
199
|
+
const dir = options.dir ?? credentialsDir(options.env)
|
|
200
|
+
const plan = await signInPlan(options, platformUrl)
|
|
201
|
+
const identity = options.identity ?? {}
|
|
202
|
+
const issued = await deviceLogin({
|
|
203
|
+
...identity,
|
|
204
|
+
issuer: plan.issuer,
|
|
205
|
+
clientId: plan.clientId,
|
|
206
|
+
scopes: plan.scopes,
|
|
207
|
+
prompt: ({ url, code, completeUrl }) => {
|
|
208
|
+
print(`Open ${completeUrl ?? url} and confirm this code: ${code}`)
|
|
209
|
+
if (completeUrl) print(`Or open ${url} and type it.`)
|
|
210
|
+
print('Waiting for you to approve it...')
|
|
211
|
+
},
|
|
212
|
+
})
|
|
213
|
+
await withLock(dir, async () => writeLogin(dir, platformUrl, issued))
|
|
214
|
+
print(`Signed in to ${platformUrl}${describeUser(issued.user)}.`)
|
|
215
|
+
return true
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function describeUser(user: StoredLogin['user']): string {
|
|
219
|
+
const who = user.name ?? user.email ?? user.subject
|
|
220
|
+
return who ? ` as ${who}` : ''
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Revokes the tokens at the identity server and forgets them. Forgetting happens regardless. */
|
|
224
|
+
export async function runLogout(options: SessionOptions, print: Print): Promise<boolean> {
|
|
225
|
+
const dir = options.dir ?? credentialsDir(options.env)
|
|
226
|
+
const platformUrl = urlOf(options) ?? storedPlatforms(dir)[0]
|
|
227
|
+
const login = platformUrl ? readLogin(dir, platformUrl) : undefined
|
|
228
|
+
if (!platformUrl || !login) {
|
|
229
|
+
print('You are not signed in.')
|
|
230
|
+
return true
|
|
231
|
+
}
|
|
232
|
+
const problem = await revokeLogin(login, options.identity ?? {})
|
|
233
|
+
await withLock(dir, async () => writeLogin(dir, platformUrl, undefined))
|
|
234
|
+
if (problem) {
|
|
235
|
+
print(`Signed out of ${platformUrl} here, but the tokens could not be revoked: ${problem}`)
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
print(`Signed out of ${platformUrl}; its tokens are revoked.`)
|
|
239
|
+
return true
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Who is signed in to which platform. Never prints a token. */
|
|
243
|
+
export function runWhoami(options: SessionOptions, print: Print): boolean {
|
|
244
|
+
const dir = options.dir ?? credentialsDir(options.env)
|
|
245
|
+
const explicit = urlOf(options)
|
|
246
|
+
const platforms = explicit ? [loginKey(explicit)] : storedPlatforms(dir)
|
|
247
|
+
const found = platforms.flatMap((platform) => {
|
|
248
|
+
const login = readLogin(dir, platform)
|
|
249
|
+
return login ? [[platform, login] as const] : []
|
|
250
|
+
})
|
|
251
|
+
if (found.length === 0) {
|
|
252
|
+
print(explicit ? `You are not signed in to ${explicit}.` : 'You are not signed in.')
|
|
253
|
+
return false
|
|
254
|
+
}
|
|
255
|
+
for (const [platform, login] of found) {
|
|
256
|
+
const ends = Date.parse(login.expiresAt)
|
|
257
|
+
print(`${platform}${describeUser(login.user)}`)
|
|
258
|
+
print(` identity server ${login.issuer}`)
|
|
259
|
+
print(` scopes ${login.scope}`)
|
|
260
|
+
print(
|
|
261
|
+
` access token ${ends > Date.now() ? `valid until ${login.expiresAt}` : 'ended; renewed on next use'}`,
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
return true
|
|
265
|
+
}
|
package/README.md
DELETED