Skip to content

Commit ceb85cb

Browse files
committed
chore: added automatic url opening
[ci skip]
1 parent 5991220 commit ceb85cb

File tree

7 files changed

+176
-132
lines changed

7 files changed

+176
-132
lines changed

npmDepsHash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sha256-23Yx1RJcaNEL1YmNz/LRFYL+5g++B+aoT0CSSbreAx8=
1+
sha256-44tI1RhQERi0VnHlcRaYWnvTUd3r+cJ4JTQPSTZ2hXk=

package-lock.json

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,21 @@
126126
},
127127
"optionalDependencies": {
128128
"@matrixai/db": "*",
129+
"@matrixai/exec-darwin-universal": "*",
130+
"@matrixai/exec-linux-x64": "*",
129131
"@matrixai/mdns-linux-x64": "*",
130132
"@matrixai/quic-darwin-universal": "*",
131133
"@matrixai/quic-linux-x64": "*",
132134
"@matrixai/quic-win32-x64": "*",
133-
"@matrixai/exec-darwin-universal": "*",
134-
"@matrixai/exec-linux-x64": "*",
135135
"fd-lock": "*",
136136
"sodium-native": "*"
137137
},
138138
"devDependencies": {
139-
"@matrixai/lint": "^0.2.11",
139+
"@fast-check/jest": "^2.1.1",
140140
"@matrixai/errors": "^2.1.3",
141-
"@matrixai/logger": "^4.0.3",
142141
"@matrixai/exec": "^1.0.3",
143-
"@fast-check/jest": "^2.1.1",
142+
"@matrixai/lint": "^0.2.11",
143+
"@matrixai/logger": "^4.0.3",
144144
"@swc/core": "1.3.82",
145145
"@swc/jest": "^0.2.29",
146146
"@types/jest": "^29.5.14",
@@ -161,6 +161,7 @@
161161
"mocked-env": "^1.3.5",
162162
"nexpect": "^0.6.0",
163163
"node-gyp-build": "^4.8.4",
164+
"open": "^10.1.2",
164165
"polykey": "^2.4.0",
165166
"shelljs": "^0.8.5",
166167
"shx": "^0.3.4",

src/auth/CommandLogin.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type PolykeyClient from 'polykey/PolykeyClient.js';
22
import CommandPolykey from '../CommandPolykey.js';
33
import * as binProcessors from '../utils/processors.js';
4-
import * as binParsers from '../utils/parsers.js';
54
import * as binUtils from '../utils/index.js';
65
import * as binOptions from '../utils/options.js';
7-
import * as errors from '../errors.js';
86

97
class CommandLogin extends CommandPolykey {
108
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
@@ -20,6 +18,7 @@ class CommandLogin extends CommandPolykey {
2018
const { default: PolykeyClient } = await import(
2119
'polykey/PolykeyClient.js'
2220
);
21+
const { default: open } = await import('open');
2322
const clientOptions = await binProcessors.processClientOptions(
2423
options.nodePath,
2524
options.nodeId,
@@ -51,19 +50,31 @@ class CommandLogin extends CommandPolykey {
5150
// Get a signed token by the agent
5251
const response = await binUtils.retryAuthentication(
5352
(auth) =>
54-
pkClient.rpcClient.methods.authSignToken({ metadata: auth }),
53+
pkClient.rpcClient.methods.authIdentityToken({ metadata: auth }),
5554
meta,
5655
);
5756

5857
// Send the returned JWT to the returnURL provided by the initial token
5958
const compactHeader = binUtils.jsonToCompactJWT(response);
60-
const targetURL = new URL(url.endsWith('/') ? url.slice(0, url.length) : url);
61-
const subPath: string = options.returnURLPath ?? '/api/oauth2/oidc'
59+
const targetURL = new URL(
60+
url.endsWith('/') ? url.slice(0, url.length) : url,
61+
);
62+
const subPath: string = options.returnURLPath ?? '/api/oauth2/oidc';
6263
targetURL.pathname = subPath.startsWith('/') ? subPath : `/${subPath}`;
6364
targetURL.searchParams.append('token', compactHeader);
6465

65-
// TEMPORARY: Print out the resulting URL
66-
process.stdout.write(`Open the following URL in your browser:\n\t${targetURL}`)
66+
// Print out the URL to stderr
67+
process.stderr.write(
68+
`Open the following URL in your browser:\n\t${targetURL}\n`,
69+
);
70+
71+
// Try to open the URL in the browser
72+
try {
73+
process.stderr.write('Opening URL in browser...\n');
74+
await open(targetURL.toString());
75+
} catch (e) {
76+
process.stderr.write(`Failed to open browser: ${e.message}\n`);
77+
}
6778
} finally {
6879
if (pkClient! != null) await pkClient.stop();
6980
}

src/errors.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,6 @@ class ErrorPolykeyCLIInvalidJWT<T> extends ErrorPolykeyCLI<T> {
201201
exitCode = sysexits.USAGE;
202202
}
203203

204-
class ErrorPolykeyCLILoginFailed<T> extends ErrorPolykeyCLI<T> {
205-
static description = 'Failed to login using Polykey';
206-
exitCode = sysexits.SOFTWARE;
207-
}
208-
209204
export {
210205
ErrorPolykeyCLI,
211206
ErrorPolykeyCLIUncaughtException,
@@ -235,5 +230,4 @@ export {
235230
ErrorPolykeyCLIEditSecret,
236231
ErrorPolykeyCLITouchSecret,
237232
ErrorPolykeyCLIInvalidJWT,
238-
ErrorPolykeyCLILoginFailed,
239233
};

src/utils/parsers.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -193,30 +193,6 @@ const parsePort: (data: string) => Port = validateParserToArgParser(
193193
const parseSeedNodes: (data: string) => [SeedNodes, boolean] =
194194
validateParserToArgParser(nodesUtils.parseSeedNodes);
195195

196-
// Compact JWTs are in xxxx.yyyy.zzzz format where x is the protected
197-
// header, y is the payload, and z is the binary signature.
198-
const parseCompactJWT = (token: string): [string, string, string] => {
199-
// Clean up whitespaces
200-
token = token.trim();
201-
202-
// Confirm part amount
203-
const parts = token.split('.');
204-
if (parts.length !== 3) {
205-
throw new InvalidArgumentError(
206-
'JWT must contain three dot-separated parts',
207-
);
208-
}
209-
210-
// Validate base64 encoding
211-
for (const part of parts) {
212-
if (!part || !base64UrlRegex.test(part)) {
213-
throw new InvalidArgumentError('JWT is not correctly encoded');
214-
}
215-
}
216-
217-
return [parts[0], parts[1], parts[2]];
218-
};
219-
220196
export {
221197
vaultNameRegex,
222198
secretPathRegex,
@@ -245,5 +221,4 @@ export {
245221
parseProviderId,
246222
parseIdentityId,
247223
parseProviderIdList,
248-
parseCompactJWT,
249224
};

0 commit comments

Comments
 (0)