@owlmeans/cli-auth 0.1.18-rc.14 → 0.1.18-rc.16
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/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/cli-auth/SKILL.md +17 -15
- package/build/consts.local.d.ts +2 -0
- package/build/consts.local.d.ts.map +1 -0
- package/build/consts.local.js +6 -0
- package/build/consts.local.js.map +1 -0
- package/build/env-file/types.d.ts +45 -0
- package/build/env-file/types.d.ts.map +1 -0
- package/build/env-file/types.js +2 -0
- package/build/env-file/types.js.map +1 -0
- package/build/env-file.d.ts +4 -40
- package/build/env-file.d.ts.map +1 -1
- package/build/env-file.js +71 -99
- package/build/env-file.js.map +1 -1
- package/build/holder.d.ts +1 -36
- package/build/holder.d.ts.map +1 -1
- package/build/holder.js +19 -19
- package/build/holder.js.map +1 -1
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js.map +1 -1
- package/build/lock/types.d.ts +28 -0
- package/build/lock/types.d.ts.map +1 -0
- package/build/lock/types.js +2 -0
- package/build/lock/types.js.map +1 -0
- package/build/lock.d.ts +2 -37
- package/build/lock.d.ts.map +1 -1
- package/build/lock.js +35 -43
- package/build/lock.js.map +1 -1
- package/build/open-browser.d.ts.map +1 -1
- package/build/open-browser.js +1 -5
- package/build/open-browser.js.map +1 -1
- package/build/types.d.ts +57 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/package.json +4 -4
- package/src/consts.local.ts +5 -0
- package/src/env-file/types.ts +45 -0
- package/src/env-file.ts +87 -110
- package/src/holder.ts +20 -62
- package/src/index.ts +3 -0
- package/src/lock/types.ts +29 -0
- package/src/lock.ts +33 -61
- package/src/open-browser.ts +1 -6
- package/src/types.ts +58 -0
- package/tests/env-file.spec.ts +18 -18
- package/tests/holder.spec.ts +4 -6
- package/tests/lock.spec.ts +22 -19
package/build/holder.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"holder.js","sourceRoot":"","sources":["../src/holder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAC5C,OAAO,
|
|
1
|
+
{"version":3,"file":"holder.js","sourceRoot":"","sources":["../src/holder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAC5C,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,qBAAqB,EAAE,cAAc,EAAE,aAAa,EAA8D,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AACpM,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAG/C,MAAM,iBAAiB,GAAG,GAAW,EAAE;IACrC,IAAI,GAAG,GAAG,KAAK,CAAA;IACf,IAAI,CAAC;QACH,GAAG,GAAG,QAAQ,EAAE,CAAC,QAAQ,CAAA;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,2FAA2F;IAC7F,CAAC;IAED,OAAO,GAAG,QAAQ,EAAE,MAAM,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,qBAAqB,CAAC,CAAA;AACjE,CAAC,CAAA;AAED;oFACoF;AACpF,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAwC,CAAA;AAExE,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAA2B,EAAkB,EAAE;IAChF,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAA;IACnC,MAAM,MAAM,GAAG,CAAC,OAAe,EAAQ,EAAE,CAAC,IAAI,CA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AAI,CAAC,WAAW,CAAC,KAAK,aAAa,EAAE,CAAC;gBAC7C,MAAM,aAAa,CAAC,YAAY,CAAC,eAAe,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,SAAS,EAAE,CAAC,CAAA;gBACpF,MAAM,CAAC,iDAAiD,CAAC,CAAA;gBAEzD,OAAM;YACR,CAAC;YACD,IAAI,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,aAAa,EAAE,CAAC;gBAC5C,MAAM,IAAI,aAAa,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;YAC3C,CAAC;QACH,CAAC;QAED,OAAO,EAAE,KAAK,IAAmB,EAAE;YACjC,MAAM,OAAO,GAAG,MAAM,KAAK,EAAE,CAAA;YAC7B,IAAI,OAAO,IAAI,IAAI;gBAAE,OAAM;YAE3B,MAAM,MAAM,GAAG,MAAM,iBAAiB,CAAC,2BAA2B,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAA;YACjG,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;gBACnB,MAAM,iBAAiB,CAAC,WAAW,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;YACjH,CAAC;YACD,MAAM,aAAa,CAAC,YAAY,CAAC,eAAe,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,SAAS,EAAE,CAAC,CAAA;QACtF,CAAC;KACF,CAAA;AACH,CAAC,CAAA"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export * from './consts.js';
|
|
2
2
|
export * from './env-file.js';
|
|
3
|
+
export type * from './env-file/types.js';
|
|
3
4
|
export * from './lock.js';
|
|
5
|
+
export type * from './lock/types.js';
|
|
6
|
+
export type * from './types.js';
|
|
4
7
|
export * from './open-browser.js';
|
|
5
8
|
export * from './holder.js';
|
|
6
9
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,WAAW,CAAA;AACzB,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,mBAAmB,qBAAqB,CAAA;AACxC,cAAc,WAAW,CAAA;AACzB,mBAAmB,iBAAiB,CAAA;AACpC,mBAAmB,YAAY,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA"}
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAE7B,cAAc,WAAW,CAAA;AAGzB,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SignInLockInfo } from '../types.js';
|
|
2
|
+
/** What the claiming process states about the sign-in; the lock adds who it is and its nonce. */
|
|
3
|
+
export interface SignInLockDraft extends Omit<SignInLockInfo, 'pid' | 'nonce' | 'apiUrl'> {
|
|
4
|
+
}
|
|
5
|
+
export interface SignInLockClaim {
|
|
6
|
+
/** This process wrote the lock and drives the sign-in. */
|
|
7
|
+
owner: boolean;
|
|
8
|
+
info: SignInLockInfo;
|
|
9
|
+
}
|
|
10
|
+
/** The cross-process sign-in lock at one path. */
|
|
11
|
+
export interface SignInLockHelper {
|
|
12
|
+
/** The lock as written, or `null` for a missing or malformed file. */
|
|
13
|
+
readLock: () => Promise<SignInLockInfo | null>;
|
|
14
|
+
/**
|
|
15
|
+
* Become the one process driving this API URL's sign-in, or find out somebody else already is.
|
|
16
|
+
*
|
|
17
|
+
* Best-effort, not a mutual-exclusion guarantee: two processes racing this at the exact same
|
|
18
|
+
* instant can both conclude they are the owner, and each then drives its own independent device
|
|
19
|
+
* authorization. That costs an extra browser tab, never a corrupted file or a double-spent code —
|
|
20
|
+
* this is a convenience for the ordinary case (a person running two terminal tabs), not a
|
|
21
|
+
* correctness boundary.
|
|
22
|
+
*/
|
|
23
|
+
claimOrJoinLock: (apiUrl: string, info: SignInLockDraft) => Promise<SignInLockClaim>;
|
|
24
|
+
/** Clear the lock, but only the copy of it this process itself wrote — a stale read after
|
|
25
|
+
* somebody else has already reclaimed the same path must never delete THEIR lock instead. */
|
|
26
|
+
releaseLock: (nonce: string) => Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lock/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD,iGAAiG;AACjG,MAAM,WAAW,eAAgB,SAAQ,IAAI,CAAC,cAAc,EAAE,KAAK,GAAG,OAAO,GAAG,QAAQ,CAAC;CAAG;AAE5F,MAAM,WAAW,eAAe;IAC9B,0DAA0D;IAC1D,KAAK,EAAE,OAAO,CAAA;IACd,IAAI,EAAE,cAAc,CAAA;CACrB;AAED,kDAAkD;AAClD,MAAM,WAAW,gBAAgB;IAC/B,sEAAsE;IACtE,QAAQ,EAAE,MAAM,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAA;IAC9C;;;;;;;;OAQG;IACH,eAAe,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,eAAe,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;IACpF;iGAC6F;IAC7F,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC9C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/lock/types.ts"],"names":[],"mappings":""}
|
package/build/lock.d.ts
CHANGED
|
@@ -1,39 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* What one process tells every other one about the sign-in it is driving — the browser URL and
|
|
3
|
-
* code so a SECOND process can show the same "waiting on…" state instead of opening a second
|
|
4
|
-
* browser tab for the same API URL.
|
|
5
|
-
*/
|
|
6
|
-
export interface SignInLockInfo {
|
|
7
|
-
pid: number;
|
|
8
|
-
apiUrl: string;
|
|
9
|
-
verificationUri: string;
|
|
10
|
-
verificationUriComplete?: string;
|
|
11
|
-
userCode?: string;
|
|
12
|
-
/** The device flow's own secret — protected the same way the eventual access token is (mode
|
|
13
|
-
* `0600`, same directory as the credentials file), so a joining process can poll the SAME
|
|
14
|
-
* pending authorization instead of requesting a second one nobody will ever display. */
|
|
15
|
-
deviceCode: string;
|
|
16
|
-
interval: number;
|
|
17
|
-
expiresAt: number;
|
|
18
|
-
/** Proves ownership at release time — a process only clears the lock it itself wrote. */
|
|
19
|
-
nonce: string;
|
|
20
|
-
}
|
|
1
|
+
import type { SignInLockHelper } from './lock/types.js';
|
|
21
2
|
export declare const lockPathFor: (credentialsPath: string) => string;
|
|
22
|
-
export declare const
|
|
23
|
-
/**
|
|
24
|
-
* Become the one process driving this API URL's sign-in, or find out somebody else already is.
|
|
25
|
-
*
|
|
26
|
-
* Best-effort, not a mutual-exclusion guarantee: two processes racing this at the exact same
|
|
27
|
-
* instant can both conclude they are the owner, and each then drives its own independent device
|
|
28
|
-
* authorization. That costs an extra browser tab, never a corrupted file or a double-spent code —
|
|
29
|
-
* this is a convenience for the ordinary case (a person running two terminal tabs), not a
|
|
30
|
-
* correctness boundary.
|
|
31
|
-
*/
|
|
32
|
-
export declare const claimOrJoinLock: (path: string, apiUrl: string, info: Omit<SignInLockInfo, 'pid' | 'nonce' | 'apiUrl'>) => Promise<{
|
|
33
|
-
owner: boolean;
|
|
34
|
-
info: SignInLockInfo;
|
|
35
|
-
}>;
|
|
36
|
-
/** Clear the lock, but only the copy of it this process itself wrote — a stale read after
|
|
37
|
-
* somebody else has already reclaimed the same path must never delete THEIR lock instead. */
|
|
38
|
-
export declare const releaseLock: (path: string, nonce: string) => Promise<void>;
|
|
3
|
+
export declare const makeSignInLockHelper: (path: string) => SignInLockHelper;
|
|
39
4
|
//# sourceMappingURL=lock.d.ts.map
|
package/build/lock.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lock.d.ts","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":"AAIA
|
|
1
|
+
{"version":3,"file":"lock.d.ts","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAoC,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAEzF,eAAO,MAAM,WAAW,oBAAqB,MAAM,KAAG,MAAmC,CAAA;AAEzF,eAAO,MAAM,oBAAoB,SAAU,MAAM,KAAG,gBAwCnD,CAAA"}
|
package/build/lock.js
CHANGED
|
@@ -2,48 +2,40 @@ import { randomBytes } from 'node:crypto';
|
|
|
2
2
|
import { mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { dirname } from 'node:path';
|
|
4
4
|
export const lockPathFor = (credentialsPath) => `${credentialsPath}.lock`;
|
|
5
|
-
export const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
};
|
|
41
|
-
/** Clear the lock, but only the copy of it this process itself wrote — a stale read after
|
|
42
|
-
* somebody else has already reclaimed the same path must never delete THEIR lock instead. */
|
|
43
|
-
export const releaseLock = async (path, nonce) => {
|
|
44
|
-
const existing = await readLock(path);
|
|
45
|
-
if (existing?.nonce === nonce) {
|
|
46
|
-
await unlink(path).catch(() => undefined);
|
|
47
|
-
}
|
|
5
|
+
export const makeSignInLockHelper = (path) => {
|
|
6
|
+
const readLock = async () => {
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(await readFile(path, 'utf-8'));
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const isAlive = (pid) => {
|
|
15
|
+
try {
|
|
16
|
+
process.kill(pid, 0);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const claimOrJoinLock = async (apiUrl, info) => {
|
|
24
|
+
const existing = await readLock();
|
|
25
|
+
if (existing != null && existing.apiUrl === apiUrl && existing.expiresAt > Date.now() && isAlive(existing.pid)) {
|
|
26
|
+
return { owner: false, info: existing };
|
|
27
|
+
}
|
|
28
|
+
const mine = { ...info, apiUrl, pid: process.pid, nonce: randomBytes(8).toString('hex') };
|
|
29
|
+
await mkdir(dirname(path), { recursive: true });
|
|
30
|
+
await writeFile(path, JSON.stringify(mine), { mode: 0o600 });
|
|
31
|
+
return { owner: true, info: mine };
|
|
32
|
+
};
|
|
33
|
+
const releaseLock = async (nonce) => {
|
|
34
|
+
const existing = await readLock();
|
|
35
|
+
if (existing?.nonce === nonce) {
|
|
36
|
+
await unlink(path).catch(() => undefined);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
return { readLock, claimOrJoinLock, releaseLock };
|
|
48
40
|
};
|
|
49
41
|
//# sourceMappingURL=lock.js.map
|
package/build/lock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAInC,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,eAAuB,EAAU,EAAE,CAAC,GAAG,eAAe,OAAO,CAAA;AAEzF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,IAAY,EAAoB,EAAE;IACrE,MAAM,QAAQ,GAAG,KAAK,IAAoC,EAAE;QAC1D,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAmB,CAAA;QACpE,CAAC;QAAC,MAAM,CAAC;YACP,OAA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EAAE,CAAA;QACjC,IAAI,QAAQ,EAAE,KAAK,KAAK,KAAK,EAAE,CAAC;YAC9B,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QAC3C,CAAC;IACH,CAAC,CAAA;IAED,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,WAAW,EAAE,CAAA;AACnD,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"open-browser.d.ts","sourceRoot":"","sources":["../src/open-browser.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"open-browser.d.ts","sourceRoot":"","sources":["../src/open-browser.ts"],"names":[],"mappings":"AAGA;;;;;;;;GAQG;AACH,eAAO,MAAM,WAAW,QAAS,MAAM,QAAO,MAAM,CAAC,UAAU,KAAiB,OAsB/E,CAAA"}
|
package/build/open-browser.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
|
|
3
|
-
darwin: 'open',
|
|
4
|
-
win32: 'start',
|
|
5
|
-
linux: 'xdg-open',
|
|
6
|
-
};
|
|
2
|
+
import { COMMAND_BY_PLATFORM } from './consts.local.js';
|
|
7
3
|
/**
|
|
8
4
|
* Best-effort: open `url` in the person's default browser. `false` on any failure — a CLI whose
|
|
9
5
|
* whole job is print-a-URL-and-poll must still work over SSH, in a container, or on a platform
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"open-browser.js","sourceRoot":"","sources":["../src/open-browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"open-browser.js","sourceRoot":"","sources":["../src/open-browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC1C,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAEv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entialsOptions {
|
|
2
|
+
/** The API origin this credential set is for, and the OAuth `resource` it is scoped to unless
|
|
3
|
+
* `resource` says otherwise. */
|
|
4
|
+
apiUrl: string;
|
|
5
|
+
/** This CLI's OAuth `client_id` — a static one the authorization server declared, or an https
|
|
6
|
+
* Client ID Metadata Document URL. */
|
|
7
|
+
clientId: string;
|
|
8
|
+
deviceName?: string;
|
|
9
|
+
resource?: string;
|
|
10
|
+
scope?: string;
|
|
11
|
+
/** Which key in `~/.owlmeans` (and the environment) carries the token. */
|
|
12
|
+
tokenEnvKey: string;
|
|
13
|
+
/** Which key records the URL a stored token belongs to. A file naming no URL at all is treated
|
|
14
|
+
* as belonging to whichever `apiUrl` is asked for — only an explicit MISMATCH refuses it. */
|
|
15
|
+
apiUrlEnvKey: string;
|
|
16
|
+
env?: NodeJS.ProcessEnv;
|
|
17
|
+
/** Best-effort progress — "open this URL and enter this code", "signed in", a failure. A host
|
|
18
|
+
* wires this to stderr, an MCP `notifications/message`, or nothing at all. */
|
|
19
|
+
onNotify?: (message: string) => void;
|
|
20
|
+
}
|
|
21
|
+
export interface CliCredentials {
|
|
22
|
+
/** The token this call site should use right now: the environment, then the bound file value,
|
|
23
|
+
* or `null` when neither has one. */
|
|
24
|
+
token: () => Promise<string | null>;
|
|
25
|
+
/** Ensure a usable token exists. Starts or joins a device sign-in when there is none, waits up
|
|
26
|
+
* to `waitMs` for it to be approved, and returns the token. The sign-in keeps running in the
|
|
27
|
+
* background past that wait — a later `require()` call picks up wherever it left off, rather
|
|
28
|
+
* than starting over. */
|
|
29
|
+
require: (waitMs?: number) => Promise<string>;
|
|
30
|
+
/** A 401 happened while presenting `rejectedToken`. A token that came from the FILE is forgotten
|
|
31
|
+
* so the next `require()` signs in again; a token that came from the ENVIRONMENT is reported —
|
|
32
|
+
* silently trying another identity behind an operator's back is worse than failing loudly. */
|
|
33
|
+
invalidate: (rejectedToken: string) => Promise<void>;
|
|
34
|
+
/** Revoke the current token at the server and remove it from the file. */
|
|
35
|
+
signOut: () => Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* What one process tells every other one about the sign-in it is driving — the browser URL and
|
|
39
|
+
* code so a SECOND process can show the same "waiting on…" state instead of opening a second
|
|
40
|
+
* browser tab for the same API URL.
|
|
41
|
+
*/
|
|
42
|
+
export interface SignInLockInfo {
|
|
43
|
+
pid: number;
|
|
44
|
+
apiUrl: string;
|
|
45
|
+
verificationUri: string;
|
|
46
|
+
verificationUriComplete?: string;
|
|
47
|
+
userCode?: string;
|
|
48
|
+
/** The device flow's own secret — protected the same way the eventual access token is (mode
|
|
49
|
+
* `0600`, same directory as the credentials file), so a joining process can poll the SAME
|
|
50
|
+
* pending authorization instead of requesting a second one nobody will ever display. */
|
|
51
|
+
deviceCode: string;
|
|
52
|
+
interval: number;
|
|
53
|
+
expiresAt: number;
|
|
54
|
+
/** Proves ownership at release time — a process only clears the lock it itself wrote. */
|
|
55
|
+
nonce: string;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC;oCACgC;IAChC,MAAM,EAAE,MAAM,CAAA;IACd;0CACsC;IACtC,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,0EAA0E;IAC1E,WAAW,EAAE,MAAM,CAAA;IACnB;iGAC6F;IAC7F,YAAY,EAAE,MAAM,CAAA;IACpB,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAA;IACvB;kFAC8E;IAC9E,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;CACrC;AAED,MAAM,WAAW,cAAc;IAC7B;yCACqC;IACrC,KAAK,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IACnC;;;6BAGyB;IACzB,OAAO,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAC7C;;kGAE8F;IAC9F,UAAU,EAAE,CAAC,aAAa,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACpD,0EAA0E;IAC1E,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC7B;AAED;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,eAAe,EAAE,MAAM,CAAA;IACvB,uBAAuB,CAAC,EAAE,MAAM,CAAA;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;4FAEwF;IACxF,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,yFAAyF;IACzF,KAAK,EAAE,MAAM,CAAA;CACd"}
|
package/build/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/cli-auth",
|
|
3
|
-
"version": "0.1.18-rc.
|
|
3
|
+
"version": "0.1.18-rc.16",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "OAuth device-authorization sign-in for a command-line tool: a dotenv-style ~/.owlmeans credentials file (overridable, environment always wins), a cross-process sign-in lock so several invocations converge on one browser round trip, a browser opener, and a small credential holder a CLI wraps its API calls with.",
|
|
6
6
|
"type": "module",
|
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@owlmeans/error": "^0.1.18-rc.
|
|
27
|
-
"@owlmeans/oauth": "^0.1.18-rc.
|
|
26
|
+
"@owlmeans/error": "^0.1.18-rc.38",
|
|
27
|
+
"@owlmeans/oauth": "^0.1.18-rc.16"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@owlmeans/dep-config": "workspace:*",
|
|
31
|
-
"@types/bun": "^1.4.
|
|
31
|
+
"@types/bun": "^1.4.2",
|
|
32
32
|
"nodemon": "^3.1.14",
|
|
33
33
|
"typescript": "^7.0.2"
|
|
34
34
|
},
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface SetEnvValuesResult {
|
|
2
|
+
/** The file is readable by somebody other than its owner — reported, never silently tightened. */
|
|
3
|
+
insecurePermissions: boolean
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** The dotenv-style `~/.owlmeans` credentials file, with the process environment layered over it. */
|
|
7
|
+
export interface EnvFileHelper {
|
|
8
|
+
/**
|
|
9
|
+
* Where the credentials file lives: `OWLMEANS_CREDENTIALS`, or `~/.owlmeans`.
|
|
10
|
+
*
|
|
11
|
+
* A CLI that talks to more than one deployment (a staging environment, a self-hosted instance)
|
|
12
|
+
* points this at a different file per deployment — the file is never merged with another one, and
|
|
13
|
+
* a token it holds is meaningless anywhere but the API URL it was signed in against.
|
|
14
|
+
*/
|
|
15
|
+
resolveEnvFile: (env?: NodeJS.ProcessEnv) => string
|
|
16
|
+
/**
|
|
17
|
+
* Parse a dotenv-shaped body: `KEY=value`, an optional `export ` prefix, `#` comments, one level
|
|
18
|
+
* of quoting. Deliberately small — a dotenv library would add a dependency for a format this
|
|
19
|
+
* package itself writes, and this is the one shape it ever needs to read back.
|
|
20
|
+
*/
|
|
21
|
+
parseEnv: (content: string) => Record<string, string>
|
|
22
|
+
/** The credentials file's values alone, with no environment overlay — what a token is bound to. */
|
|
23
|
+
readCredentialsFile: (env?: NodeJS.ProcessEnv) => Promise<Record<string, string>>
|
|
24
|
+
/**
|
|
25
|
+
* The file, with the process environment layered over it — environment wins, but an environment
|
|
26
|
+
* value that is the EMPTY STRING is treated as unset.
|
|
27
|
+
*
|
|
28
|
+
* The empty-string rule exists because a harness config commonly expands an unset shell variable
|
|
29
|
+
* to `''` (`${VIABLE_API_TOKEN:-}`), and a literal empty override must not shadow a real value the
|
|
30
|
+
* file holds — that would make "I have not set this" indistinguishable from "I am overriding this
|
|
31
|
+
* to nothing", and the file is always the more deliberate of the two.
|
|
32
|
+
*/
|
|
33
|
+
loadOwlmeansEnv: (env?: NodeJS.ProcessEnv) => Promise<Record<string, string>>
|
|
34
|
+
/**
|
|
35
|
+
* Replace the named keys in the credentials file, keeping every other line — comments, a key this
|
|
36
|
+
* call did not touch, blank lines — exactly where they were. An `undefined` value removes the key.
|
|
37
|
+
*
|
|
38
|
+
* Written atomically (a temp file in the same directory, then a rename) so a process killed
|
|
39
|
+
* mid-write never leaves a half-written credentials file behind, and created with mode `0600`
|
|
40
|
+
* because this file can hold a live access token. An existing file that is readable by anyone but
|
|
41
|
+
* its owner is reported back rather than silently tightened — permissions someone else set on
|
|
42
|
+
* purpose are theirs to change.
|
|
43
|
+
*/
|
|
44
|
+
setEnvValues: (path: string, values: Record<string, string | undefined>) => Promise<SetEnvValuesResult>
|
|
45
|
+
}
|
package/src/env-file.ts
CHANGED
|
@@ -3,121 +3,98 @@ import { chmod, mkdir, readFile, rename, stat, writeFile } from 'node:fs/promise
|
|
|
3
3
|
import { homedir } from 'node:os'
|
|
4
4
|
import { dirname, join } from 'node:path'
|
|
5
5
|
import { DEFAULT_CREDENTIALS_FILENAME, ENV_CREDENTIALS_FILE } from './consts.js'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(line)
|
|
31
|
-
if (match == null) continue
|
|
32
|
-
|
|
33
|
-
let value = match[2].trim()
|
|
34
|
-
if ((value.startsWith('"') && value.endsWith('"') && value.length > 1)
|
|
35
|
-
|| (value.startsWith("'") && value.endsWith("'") && value.length > 1)) {
|
|
36
|
-
value = value.slice(1, -1)
|
|
6
|
+
import type { EnvFileHelper, SetEnvValuesResult } from './env-file/types.js'
|
|
7
|
+
|
|
8
|
+
export const createEnvFileHelper = (): EnvFileHelper => {
|
|
9
|
+
const resolveEnvFile = (env: NodeJS.ProcessEnv = process.env): string =>
|
|
10
|
+
env[ENV_CREDENTIALS_FILE] != null && env[ENV_CREDENTIALS_FILE] !== ''
|
|
11
|
+
? env[ENV_CREDENTIALS_FILE]
|
|
12
|
+
: join(homedir(), DEFAULT_CREDENTIALS_FILENAME)
|
|
13
|
+
|
|
14
|
+
const parseEnv = (content: string): Record<string, string> => {
|
|
15
|
+
const values: Record<string, string> = {}
|
|
16
|
+
for (const raw of content.split('\n')) {
|
|
17
|
+
const line = raw.trim()
|
|
18
|
+
if (line === '' || line.startsWith('#')) continue
|
|
19
|
+
|
|
20
|
+
const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(line)
|
|
21
|
+
if (match == null) continue
|
|
22
|
+
|
|
23
|
+
let value = match[2].trim()
|
|
24
|
+
if ((value.startsWith('"') && value.endsWith('"') && value.length > 1)
|
|
25
|
+
|| (value.startsWith("'") && value.endsWith("'") && value.length > 1)) {
|
|
26
|
+
value = value.slice(1, -1)
|
|
27
|
+
}
|
|
28
|
+
values[match[1]] = value
|
|
37
29
|
}
|
|
38
|
-
|
|
30
|
+
|
|
31
|
+
return values
|
|
39
32
|
}
|
|
40
33
|
|
|
41
|
-
|
|
42
|
-
|
|
34
|
+
const readFileIfPresent = async (path: string): Promise<string> =>
|
|
35
|
+
await readFile(path, 'utf-8').catch(() => '')
|
|
43
36
|
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* The file, with the process environment layered over it — environment wins, but an environment
|
|
55
|
-
* value that is the EMPTY STRING is treated as unset.
|
|
56
|
-
*
|
|
57
|
-
* The empty-string rule exists because a harness config commonly expands an unset shell variable
|
|
58
|
-
* to `''` (`${VIABLE_API_TOKEN:-}`), and a literal empty override must not shadow a real value the
|
|
59
|
-
* file holds — that would make "I have not set this" indistinguishable from "I am overriding this
|
|
60
|
-
* to nothing", and the file is always the more deliberate of the two.
|
|
61
|
-
*/
|
|
62
|
-
export const loadOwlmeansEnv = async (env: NodeJS.ProcessEnv = process.env): Promise<Record<string, string>> => {
|
|
63
|
-
const file = await readCredentialsFile(env)
|
|
64
|
-
const fromEnv: Record<string, string> = {}
|
|
65
|
-
Object.entries(env).forEach(([key, value]) => {
|
|
66
|
-
if (value != null && value !== '') fromEnv[key] = value
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
return { ...file, ...fromEnv }
|
|
70
|
-
}
|
|
37
|
+
const readCredentialsFile = async (env: NodeJS.ProcessEnv = process.env): Promise<Record<string, string>> =>
|
|
38
|
+
parseEnv(await readFileIfPresent(resolveEnvFile(env)))
|
|
39
|
+
|
|
40
|
+
const loadOwlmeansEnv = async (env: NodeJS.ProcessEnv = process.env): Promise<Record<string, string>> => {
|
|
41
|
+
const file = await readCredentialsFile(env)
|
|
42
|
+
const fromEnv: Record<string, string> = {}
|
|
43
|
+
Object.entries(env).forEach(([key, value]) => {
|
|
44
|
+
if (value != null && value !== '') fromEnv[key] = value
|
|
45
|
+
})
|
|
71
46
|
|
|
72
|
-
|
|
73
|
-
* Replace the named keys in the credentials file, keeping every other line — comments, a key this
|
|
74
|
-
* call did not touch, blank lines — exactly where they were.
|
|
75
|
-
*
|
|
76
|
-
* Written atomically (a temp file in the same directory, then a rename) so a process killed
|
|
77
|
-
* mid-write never leaves a half-written credentials file behind, and created with mode `0600`
|
|
78
|
-
* because this file can hold a live access token. An existing file that is readable by anyone but
|
|
79
|
-
* its owner is reported back rather than silently tightened — permissions someone else set on
|
|
80
|
-
* purpose are theirs to change.
|
|
81
|
-
*/
|
|
82
|
-
export const setEnvValues = async (
|
|
83
|
-
path: string, values: Record<string, string | undefined>
|
|
84
|
-
): Promise<{ insecurePermissions: boolean }> => {
|
|
85
|
-
const existing = await readFileIfPresent(path)
|
|
86
|
-
const lines = existing === '' ? [] : existing.split('\n')
|
|
87
|
-
const claimed = new Set<string>()
|
|
88
|
-
|
|
89
|
-
const rewritten = lines.map(line => {
|
|
90
|
-
const trimmed = line.trim()
|
|
91
|
-
const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(trimmed)
|
|
92
|
-
if (match == null || !(match[1] in values)) return line
|
|
93
|
-
|
|
94
|
-
claimed.add(match[1])
|
|
95
|
-
const value = values[match[1]]
|
|
96
|
-
|
|
97
|
-
return value == null ? null : `${match[1]}=${value}`
|
|
98
|
-
}).filter((line): line is string => line != null)
|
|
99
|
-
|
|
100
|
-
Object.entries(values).forEach(([key, value]) => {
|
|
101
|
-
if (claimed.has(key) || value == null) return
|
|
102
|
-
rewritten.push(`${key}=${value}`)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
const body = `${rewritten.join('\n').replace(/\n+$/, '')}\n`
|
|
106
|
-
|
|
107
|
-
await mkdir(dirname(path), { recursive: true })
|
|
108
|
-
const tmp = join(dirname(path), `.${DEFAULT_CREDENTIALS_FILENAME}.${randomBytes(6).toString('hex')}.tmp`)
|
|
109
|
-
await writeFile(tmp, body, { mode: 0o600 })
|
|
110
|
-
await rename(tmp, path)
|
|
111
|
-
await chmod(path, 0o600).catch(() => undefined)
|
|
112
|
-
|
|
113
|
-
let insecurePermissions = false
|
|
114
|
-
try {
|
|
115
|
-
const info = await stat(path)
|
|
116
|
-
insecurePermissions = (info.mode & 0o077) !== 0
|
|
117
|
-
} catch {
|
|
118
|
-
// Nothing to report if the stat itself fails right after a successful write — unusual enough
|
|
119
|
-
// that guessing at a permission problem would be noise.
|
|
47
|
+
return { ...file, ...fromEnv }
|
|
120
48
|
}
|
|
121
49
|
|
|
122
|
-
|
|
50
|
+
const setEnvValues = async (
|
|
51
|
+
path: string, values: Record<string, string | undefined>
|
|
52
|
+
): Promise<SetEnvValuesResult> => {
|
|
53
|
+
const existing = await readFileIfPresent(path)
|
|
54
|
+
const lines = existing === '' ? [] : existing.split('\n')
|
|
55
|
+
const claimed = new Set<string>()
|
|
56
|
+
|
|
57
|
+
const rewritten = lines.map(line => {
|
|
58
|
+
const trimmed = line.trim()
|
|
59
|
+
const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(trimmed)
|
|
60
|
+
if (match == null || !(match[1] in values)) return line
|
|
61
|
+
|
|
62
|
+
claimed.add(match[1])
|
|
63
|
+
const value = values[match[1]]
|
|
64
|
+
|
|
65
|
+
return value == null ? null : `${match[1]}=${value}`
|
|
66
|
+
}).filter((line): line is string => line != null)
|
|
67
|
+
|
|
68
|
+
Object.entries(values).forEach(([key, value]) => {
|
|
69
|
+
if (claimed.has(key) || value == null) return
|
|
70
|
+
rewritten.push(`${key}=${value}`)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
const body = `${rewritten.join('\n').replace(/\n+$/, '')}\n`
|
|
74
|
+
|
|
75
|
+
await mkdir(dirname(path), { recursive: true })
|
|
76
|
+
const tmp = join(dirname(path), `.${DEFAULT_CREDENTIALS_FILENAME}.${randomBytes(6).toString('hex')}.tmp`)
|
|
77
|
+
await writeFile(tmp, body, { mode: 0o600 })
|
|
78
|
+
await rename(tmp, path)
|
|
79
|
+
await chmod(path, 0o600).catch(() => undefined)
|
|
80
|
+
|
|
81
|
+
let insecurePermissions = false
|
|
82
|
+
try {
|
|
83
|
+
const info = await stat(path)
|
|
84
|
+
insecurePermissions = (info.mode & 0o077) !== 0
|
|
85
|
+
} catch {
|
|
86
|
+
// Nothing to report if the stat itself fails right after a successful write — unusual enough
|
|
87
|
+
// that guessing at a permission problem would be noise.
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { insecurePermissions }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { resolveEnvFile, parseEnv, readCredentialsFile, loadOwlmeansEnv, setEnvValues }
|
|
123
94
|
}
|
|
95
|
+
|
|
96
|
+
export const envFileHelper = createEnvFileHelper()
|
|
97
|
+
|
|
98
|
+
/** @deprecated compat:factory-refactor — use `envFileHelper.loadOwlmeansEnv(…)` */
|
|
99
|
+
export const loadOwlmeansEnv = async (env: NodeJS.ProcessEnv = process.env): Promise<Record<string, string>> =>
|
|
100
|
+
await envFileHelper.loadOwlmeansEnv(env)
|