@pnpm/network.web-auth 1000.0.0 → 1100.0.0
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/lib/index.d.ts +2 -1
- package/lib/index.js +1 -0
- package/lib/promptBrowserOpen.d.ts +38 -0
- package/lib/promptBrowserOpen.js +114 -0
- package/lib/withOtpHandling.d.ts +13 -3
- package/lib/withOtpHandling.js +7 -1
- package/package.json +3 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { generateQrCode } from './generateQrCode.js';
|
|
2
2
|
export { pollForWebAuthToken, type PollForWebAuthTokenParams, type WebAuthContext, type WebAuthFetchOptions, type WebAuthFetchResponse, type WebAuthFetchResponseHeaders, } from './pollForWebAuthToken.js';
|
|
3
|
+
export { promptBrowserOpen, type PromptBrowserOpenContext, type PromptBrowserOpenExecFile, type PromptBrowserOpenParams, type PromptBrowserOpenProcess, type PromptBrowserOpenReadlineInterface, } from './promptBrowserOpen.js';
|
|
3
4
|
export { WebAuthTimeoutError } from './WebAuthTimeoutError.js';
|
|
4
|
-
export { isOtpError, type OtpContext, type OtpEnquirer, type OtpHandlingParams, OtpNonInteractiveError, type OtpPromptOptions, type OtpPromptResponse, OtpSecondChallengeError, SyntheticOtpError, withOtpHandling, } from './withOtpHandling.js';
|
|
5
|
+
export { isOtpError, type OtpContext, type OtpEnquirer, type OtpHandlingParams, OtpNonInteractiveError, type OtpProcess, type OtpPromptOptions, type OtpPromptResponse, OtpSecondChallengeError, SyntheticOtpError, withOtpHandling, } from './withOtpHandling.js';
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { generateQrCode } from './generateQrCode.js';
|
|
2
2
|
export { pollForWebAuthToken, } from './pollForWebAuthToken.js';
|
|
3
|
+
export { promptBrowserOpen, } from './promptBrowserOpen.js';
|
|
3
4
|
export { WebAuthTimeoutError } from './WebAuthTimeoutError.js';
|
|
4
5
|
export { isOtpError, OtpNonInteractiveError, OtpSecondChallengeError, SyntheticOtpError, withOtpHandling, } from './withOtpHandling.js';
|
|
5
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export interface PromptBrowserOpenReadlineInterface {
|
|
2
|
+
once: (event: string, listener: () => void) => void;
|
|
3
|
+
close: () => void;
|
|
4
|
+
}
|
|
5
|
+
export interface PromptBrowserOpenExecFile {
|
|
6
|
+
(file: string, args: readonly string[], callback: (error: Error | null) => void): unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface PromptBrowserOpenProcess {
|
|
9
|
+
platform?: NodeJS.Platform;
|
|
10
|
+
stdin: {
|
|
11
|
+
isTTY?: boolean;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface PromptBrowserOpenContext {
|
|
15
|
+
createReadlineInterface?: () => PromptBrowserOpenReadlineInterface;
|
|
16
|
+
execFile?: PromptBrowserOpenExecFile;
|
|
17
|
+
globalInfo: (message: string) => void;
|
|
18
|
+
globalWarn: (message: string) => void;
|
|
19
|
+
process: PromptBrowserOpenProcess;
|
|
20
|
+
}
|
|
21
|
+
export interface PromptBrowserOpenParams {
|
|
22
|
+
authUrl: string;
|
|
23
|
+
context: PromptBrowserOpenContext;
|
|
24
|
+
pollPromise: Promise<string>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Wraps a token-polling promise with an optional "Press ENTER to open in
|
|
28
|
+
* browser" prompt.
|
|
29
|
+
*
|
|
30
|
+
* While the poll runs in the background, listens for the user pressing Enter
|
|
31
|
+
* to open the authentication URL in their browser. When the poll completes
|
|
32
|
+
* (regardless of whether the user pressed Enter), the keyboard listener is
|
|
33
|
+
* cleaned up.
|
|
34
|
+
*
|
|
35
|
+
* Error-tolerant: failures in the keyboard listener or browser opening are
|
|
36
|
+
* logged as warnings and do not interrupt the poll.
|
|
37
|
+
*/
|
|
38
|
+
export declare function promptBrowserOpen({ authUrl, context, pollPromise }: PromptBrowserOpenParams): Promise<string>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a token-polling promise with an optional "Press ENTER to open in
|
|
3
|
+
* browser" prompt.
|
|
4
|
+
*
|
|
5
|
+
* While the poll runs in the background, listens for the user pressing Enter
|
|
6
|
+
* to open the authentication URL in their browser. When the poll completes
|
|
7
|
+
* (regardless of whether the user pressed Enter), the keyboard listener is
|
|
8
|
+
* cleaned up.
|
|
9
|
+
*
|
|
10
|
+
* Error-tolerant: failures in the keyboard listener or browser opening are
|
|
11
|
+
* logged as warnings and do not interrupt the poll.
|
|
12
|
+
*/
|
|
13
|
+
export async function promptBrowserOpen({ authUrl, context, pollPromise, }) {
|
|
14
|
+
const { createReadlineInterface, execFile, globalInfo, globalWarn, process } = context;
|
|
15
|
+
if (!createReadlineInterface || !execFile || !process.stdin.isTTY) {
|
|
16
|
+
return pollPromise;
|
|
17
|
+
}
|
|
18
|
+
// Validate the URL before passing it to a shell command. On Windows,
|
|
19
|
+
// cmd.exe re-parses execFile arguments and would interpret shell
|
|
20
|
+
// metacharacters (&, |, etc.) in the URL as operators.
|
|
21
|
+
let parsedUrl;
|
|
22
|
+
try {
|
|
23
|
+
parsedUrl = new URL(authUrl);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return pollPromise;
|
|
27
|
+
}
|
|
28
|
+
if (parsedUrl.protocol !== 'https:' && parsedUrl.protocol !== 'http:') {
|
|
29
|
+
return pollPromise;
|
|
30
|
+
}
|
|
31
|
+
const canonicalUrl = parsedUrl.href;
|
|
32
|
+
let cmd;
|
|
33
|
+
let args;
|
|
34
|
+
switch (process.platform) {
|
|
35
|
+
case 'darwin':
|
|
36
|
+
cmd = 'open';
|
|
37
|
+
args = [canonicalUrl];
|
|
38
|
+
break;
|
|
39
|
+
case 'win32': {
|
|
40
|
+
cmd = 'cmd';
|
|
41
|
+
// Windows edge cases for opening URLs from Node.js:
|
|
42
|
+
//
|
|
43
|
+
// The clean approach would be calling the Win32 ShellExecuteW API
|
|
44
|
+
// directly, which is what native Windows programs use. However,
|
|
45
|
+
// ShellExecuteW is a native API, not an executable — Node.js cannot
|
|
46
|
+
// call it from child_process without a native addon.
|
|
47
|
+
//
|
|
48
|
+
// All process-spawning alternatives have drawbacks:
|
|
49
|
+
// - cmd /c start: cmd.exe re-parses args; special characters in
|
|
50
|
+
// URLs (&, |, ^, %, etc.) are treated as shell
|
|
51
|
+
// operators
|
|
52
|
+
// - explorer.exe: breaks on URLs with query strings (?key=value),
|
|
53
|
+
// opening File Explorer instead of the browser
|
|
54
|
+
// (https://github.com/dotnet/runtime/issues/108817)
|
|
55
|
+
// - url.dll: undocumented, can strip query params on Win 7+
|
|
56
|
+
// - PowerShell: slow startup, own escaping issues
|
|
57
|
+
//
|
|
58
|
+
// Since pnpm already ships native addons, a small Rust/N-API addon
|
|
59
|
+
// calling ShellExecuteW directly could replace this in the future.
|
|
60
|
+
//
|
|
61
|
+
// For now, use cmd /c start with ^ escaping for special characters.
|
|
62
|
+
const escapedUrl = canonicalUrl.replace(/[&|<>^%()!]/g, '^$&');
|
|
63
|
+
args = ['/c', 'start', '', escapedUrl];
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
case 'linux':
|
|
67
|
+
cmd = 'xdg-open';
|
|
68
|
+
args = [canonicalUrl];
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
return pollPromise;
|
|
72
|
+
}
|
|
73
|
+
let rl;
|
|
74
|
+
try {
|
|
75
|
+
rl = createReadlineInterface();
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
globalWarn(`Could not set up keyboard listener: ${String(err)}`);
|
|
79
|
+
return pollPromise;
|
|
80
|
+
}
|
|
81
|
+
globalInfo('Press ENTER to open the URL in your browser.');
|
|
82
|
+
rl.once('line', () => {
|
|
83
|
+
runExecFile(execFile, cmd, args).catch((err) => {
|
|
84
|
+
globalWarn(`Could not open browser automatically: ${String(err)}`);
|
|
85
|
+
globalInfo('Please open the URL shown above manually.');
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
// Only await pollPromise — do NOT await the Enter keypress.
|
|
89
|
+
//
|
|
90
|
+
// The Enter listener is a fire-and-forget side effect. Users may authenticate
|
|
91
|
+
// on their phone (via QR code or pasted URL) without ever pressing Enter, so
|
|
92
|
+
// the poll must be able to complete independently.
|
|
93
|
+
//
|
|
94
|
+
// npm uses Promise.all([opener, poll]) which blocks the entire flow until the
|
|
95
|
+
// user presses Enter — even if authentication already succeeded on another
|
|
96
|
+
// device: <https://github.com/npm/npm-profile/blob/d1a48be4/lib/index.js#L85-L98>
|
|
97
|
+
try {
|
|
98
|
+
return await pollPromise;
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
rl.close();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function runExecFile(execFile, cmd, args) {
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
execFile(cmd, args, (err) => {
|
|
107
|
+
if (err)
|
|
108
|
+
reject(err);
|
|
109
|
+
else
|
|
110
|
+
resolve();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=promptBrowserOpen.js.map
|
package/lib/withOtpHandling.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PnpmError } from '@pnpm/error';
|
|
2
2
|
import type { WebAuthFetchOptions, WebAuthFetchResponse } from './pollForWebAuthToken.js';
|
|
3
|
+
import type { PromptBrowserOpenExecFile, PromptBrowserOpenReadlineInterface } from './promptBrowserOpen.js';
|
|
3
4
|
export interface OtpEnquirer {
|
|
4
5
|
prompt: (options: OtpPromptOptions) => Promise<OtpPromptResponse | undefined>;
|
|
5
6
|
}
|
|
@@ -14,16 +15,25 @@ export interface OtpPromptResponse {
|
|
|
14
15
|
interface OtpDate {
|
|
15
16
|
now: () => number;
|
|
16
17
|
}
|
|
18
|
+
export interface OtpProcess {
|
|
19
|
+
platform?: NodeJS.Platform;
|
|
20
|
+
stdin: {
|
|
21
|
+
isTTY?: boolean;
|
|
22
|
+
};
|
|
23
|
+
stdout: {
|
|
24
|
+
isTTY?: boolean;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
17
27
|
export interface OtpContext {
|
|
18
28
|
Date: OtpDate;
|
|
19
29
|
setTimeout: (cb: () => void, ms: number) => void;
|
|
30
|
+
createReadlineInterface?: () => PromptBrowserOpenReadlineInterface;
|
|
20
31
|
enquirer: OtpEnquirer;
|
|
32
|
+
execFile?: PromptBrowserOpenExecFile;
|
|
21
33
|
fetch: (url: string, options: WebAuthFetchOptions) => Promise<WebAuthFetchResponse>;
|
|
22
34
|
globalInfo: (message: string) => void;
|
|
23
35
|
globalWarn: (message: string) => void;
|
|
24
|
-
process:
|
|
25
|
-
isTTY?: boolean;
|
|
26
|
-
}>;
|
|
36
|
+
process: OtpProcess;
|
|
27
37
|
}
|
|
28
38
|
interface OtpErrorBody {
|
|
29
39
|
authUrl?: string;
|
package/lib/withOtpHandling.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PnpmError } from '@pnpm/error';
|
|
2
2
|
import { generateQrCode } from './generateQrCode.js';
|
|
3
3
|
import { pollForWebAuthToken } from './pollForWebAuthToken.js';
|
|
4
|
+
import { promptBrowserOpen } from './promptBrowserOpen.js';
|
|
4
5
|
export const isOtpError = (error) => error != null &&
|
|
5
6
|
typeof error === 'object' &&
|
|
6
7
|
'code' in error &&
|
|
@@ -35,11 +36,16 @@ export async function withOtpHandling({ context, fetchOptions, operation, }) {
|
|
|
35
36
|
if (error.body?.authUrl && error.body?.doneUrl) {
|
|
36
37
|
const qrCode = generateQrCode(error.body.authUrl);
|
|
37
38
|
globalInfo(`Authenticate your account at:\n${error.body.authUrl}\n\n${qrCode}`);
|
|
38
|
-
|
|
39
|
+
const pollPromise = pollForWebAuthToken({
|
|
39
40
|
context,
|
|
40
41
|
doneUrl: error.body.doneUrl,
|
|
41
42
|
fetchOptions,
|
|
42
43
|
});
|
|
44
|
+
otp = await promptBrowserOpen({
|
|
45
|
+
authUrl: error.body.authUrl,
|
|
46
|
+
context,
|
|
47
|
+
pollPromise,
|
|
48
|
+
});
|
|
43
49
|
}
|
|
44
50
|
else {
|
|
45
51
|
const enquirerResponse = await enquirer.prompt({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pnpm/network.web-auth",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1100.0.0",
|
|
4
4
|
"description": "Web-based authentication flow with QR code display and token polling",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pnpm",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"qrcode-terminal": "^0.12.0",
|
|
29
|
-
"@pnpm/error": "
|
|
29
|
+
"@pnpm/error": "1100.0.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@jest/globals": "30.3.0",
|
|
33
33
|
"@types/qrcode-terminal": "^0.12.2",
|
|
34
|
-
"@pnpm/network.web-auth": "
|
|
34
|
+
"@pnpm/network.web-auth": "1100.0.0"
|
|
35
35
|
},
|
|
36
36
|
"engines": {
|
|
37
37
|
"node": ">=22.13"
|