poe-code 3.0.208 → 3.0.209

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/dist/index.js CHANGED
@@ -35457,6 +35457,212 @@ var init_check_auth = __esm({
35457
35457
  }
35458
35458
  });
35459
35459
 
35460
+ // packages/poe-oauth/src/authorization-state.ts
35461
+ import crypto5 from "node:crypto";
35462
+ function parseAuthorizationState2(value) {
35463
+ if (value === null || value.length === 0) {
35464
+ return null;
35465
+ }
35466
+ try {
35467
+ const decoded = Buffer.from(value, "base64url").toString("utf8");
35468
+ const parsed = JSON.parse(decoded);
35469
+ if (parsed.v !== 1 || typeof parsed.n !== "string" || parsed.n.length === 0 || typeof parsed.i !== "string" || parsed.i.length === 0 || typeof parsed.r !== "boolean") {
35470
+ return null;
35471
+ }
35472
+ return {
35473
+ issuer: parsed.i,
35474
+ requireIssuer: parsed.r
35475
+ };
35476
+ } catch {
35477
+ return null;
35478
+ }
35479
+ }
35480
+ var init_authorization_state2 = __esm({
35481
+ "packages/poe-oauth/src/authorization-state.ts"() {
35482
+ "use strict";
35483
+ }
35484
+ });
35485
+
35486
+ // packages/poe-oauth/src/loopback-authorization.ts
35487
+ import http2 from "node:http";
35488
+ async function createLoopbackAuthorizationSession2(options = {}) {
35489
+ const callbackPath = options.callbackPath ?? "/callback";
35490
+ const server = options.createServer ? options.createServer() : http2.createServer();
35491
+ const port = await startServer2(server);
35492
+ const redirectUri = `http://127.0.0.1:${port}${callbackPath}`;
35493
+ return {
35494
+ redirectUri,
35495
+ async waitForCode(authorizationUrl) {
35496
+ return waitForAuthorizationCode2(server, authorizationUrl, options, callbackPath);
35497
+ },
35498
+ close() {
35499
+ server.closeAllConnections?.();
35500
+ server.close();
35501
+ }
35502
+ };
35503
+ }
35504
+ async function startServer2(server) {
35505
+ return new Promise((resolve2) => {
35506
+ server.listen(0, "127.0.0.1", () => {
35507
+ const address = server.address();
35508
+ resolve2(address.port);
35509
+ });
35510
+ });
35511
+ }
35512
+ function waitForAuthorizationCode2(server, authorizationUrl, options, callbackPath) {
35513
+ const expectedAuthorization = readExpectedAuthorizationCallback2(authorizationUrl);
35514
+ return new Promise((resolve2, reject) => {
35515
+ let settled = false;
35516
+ const settle = (fn) => {
35517
+ if (!settled) {
35518
+ settled = true;
35519
+ fn();
35520
+ }
35521
+ };
35522
+ server.on("request", (req, res) => {
35523
+ const url = new URL(req.url ?? "/", "http://127.0.0.1");
35524
+ if (url.pathname !== callbackPath) {
35525
+ res.writeHead(404);
35526
+ res.end("Not found");
35527
+ return;
35528
+ }
35529
+ const error2 = url.searchParams.get("error");
35530
+ if (error2 !== null) {
35531
+ const description = url.searchParams.get("error_description") ?? error2;
35532
+ res.writeHead(400);
35533
+ res.end(`Authorization failed: ${description}`);
35534
+ settle(() => reject(new Error(`OAuth authorization failed: ${error2} \u2014 ${description}`)));
35535
+ return;
35536
+ }
35537
+ try {
35538
+ const code = validateAuthorizationCallbackParameters2({
35539
+ code: url.searchParams.get("code"),
35540
+ state: url.searchParams.get("state"),
35541
+ iss: url.searchParams.get("iss")
35542
+ }, expectedAuthorization);
35543
+ res.writeHead(200, { "Content-Type": "text/html" });
35544
+ res.end(buildSuccessPage2(options.landingPage));
35545
+ settle(() => resolve2(code));
35546
+ } catch (error3) {
35547
+ res.writeHead(400);
35548
+ res.end(error3 instanceof Error ? error3.message : "Invalid OAuth callback");
35549
+ settle(() => reject(error3 instanceof Error ? error3 : new Error(String(error3))));
35550
+ }
35551
+ });
35552
+ if (options.readLine !== void 0) {
35553
+ options.readLine().then((input) => {
35554
+ const callbackParameters = extractCallbackParametersFromInput2(input);
35555
+ if (callbackParameters === null) {
35556
+ settle(() => reject(new Error("OAuth callback missing authorization code")));
35557
+ return;
35558
+ }
35559
+ try {
35560
+ const code = validateAuthorizationCallbackParameters2(
35561
+ callbackParameters,
35562
+ expectedAuthorization
35563
+ );
35564
+ settle(() => resolve2(code));
35565
+ } catch (error2) {
35566
+ settle(() => reject(error2 instanceof Error ? error2 : new Error(String(error2))));
35567
+ }
35568
+ }).catch(() => void 0);
35569
+ }
35570
+ if (options.openBrowser !== void 0) {
35571
+ options.openBrowser(authorizationUrl).catch((error2) => {
35572
+ settle(() => reject(error2));
35573
+ });
35574
+ }
35575
+ });
35576
+ }
35577
+ function extractCallbackParametersFromInput2(input) {
35578
+ const trimmed = input.replaceAll("\r", "").replaceAll("\n", "").trim();
35579
+ if (trimmed.length === 0) {
35580
+ return null;
35581
+ }
35582
+ try {
35583
+ const url = new URL(trimmed);
35584
+ return {
35585
+ code: url.searchParams.get("code"),
35586
+ state: url.searchParams.get("state"),
35587
+ iss: url.searchParams.get("iss")
35588
+ };
35589
+ } catch {
35590
+ return {
35591
+ code: trimmed,
35592
+ state: null,
35593
+ iss: null
35594
+ };
35595
+ }
35596
+ }
35597
+ function readExpectedAuthorizationCallback2(authorizationUrl) {
35598
+ const url = new URL(authorizationUrl);
35599
+ const state = url.searchParams.get("state");
35600
+ const parsedState = parseAuthorizationState2(state);
35601
+ return {
35602
+ state,
35603
+ issuer: parsedState?.issuer ?? null,
35604
+ requireIssuer: parsedState?.requireIssuer ?? false
35605
+ };
35606
+ }
35607
+ function validateAuthorizationCallbackParameters2(callback, expected) {
35608
+ if (callback.code === null || callback.code.length === 0) {
35609
+ throw new Error("OAuth callback missing authorization code");
35610
+ }
35611
+ if (expected.state !== null) {
35612
+ if (callback.state === null || callback.state.length === 0) {
35613
+ throw new Error("OAuth callback missing state");
35614
+ }
35615
+ if (callback.state !== expected.state) {
35616
+ throw new Error("OAuth callback state mismatch");
35617
+ }
35618
+ }
35619
+ if (expected.requireIssuer) {
35620
+ if (callback.iss === null || callback.iss.length === 0) {
35621
+ throw new Error("OAuth callback missing issuer");
35622
+ }
35623
+ }
35624
+ if (callback.iss !== null && callback.iss.length > 0 && expected.issuer !== null && callback.iss !== expected.issuer) {
35625
+ throw new Error("OAuth callback issuer mismatch");
35626
+ }
35627
+ return callback.code;
35628
+ }
35629
+ function escapeHtml2(text5) {
35630
+ return text5.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;");
35631
+ }
35632
+ function buildSuccessPage2(landingPage) {
35633
+ const title = landingPage?.title ?? "Connected";
35634
+ const body = landingPage?.body ?? "You can close this tab and return to your terminal.";
35635
+ return [
35636
+ "<!DOCTYPE html>",
35637
+ `<html><head><meta charset=utf-8><title>${escapeHtml2(title)}</title></head>`,
35638
+ '<body style="font-family:system-ui,sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0">',
35639
+ '<div style="text-align:center">',
35640
+ `<h1>${escapeHtml2(title)}</h1>`,
35641
+ `<p style="color:#666">${escapeHtml2(body)}</p>`,
35642
+ "</div></body></html>"
35643
+ ].join("");
35644
+ }
35645
+ var init_loopback_authorization2 = __esm({
35646
+ "packages/poe-oauth/src/loopback-authorization.ts"() {
35647
+ "use strict";
35648
+ init_authorization_state2();
35649
+ }
35650
+ });
35651
+
35652
+ // packages/poe-oauth/src/pkce.ts
35653
+ import crypto6 from "node:crypto";
35654
+ function generateCodeVerifier2() {
35655
+ return crypto6.randomBytes(32).toString("base64url");
35656
+ }
35657
+ function generateCodeChallenge2(verifier) {
35658
+ return crypto6.createHash("sha256").update(verifier).digest("base64url");
35659
+ }
35660
+ var init_pkce2 = __esm({
35661
+ "packages/poe-oauth/src/pkce.ts"() {
35662
+ "use strict";
35663
+ }
35664
+ });
35665
+
35460
35666
  // packages/poe-oauth/src/oauth-client.ts
35461
35667
  function createOAuthClient(config) {
35462
35668
  const fetchFn = config.fetch ?? globalThis.fetch;
@@ -35464,15 +35670,15 @@ function createOAuthClient(config) {
35464
35670
  authorize: () => startAuthorization(config, fetchFn)
35465
35671
  };
35466
35672
  }
35467
- function generateCodeVerifier2() {
35468
- return generateCodeVerifier();
35673
+ function generateCodeVerifier3() {
35674
+ return generateCodeVerifier2();
35469
35675
  }
35470
35676
  async function startAuthorization(config, fetchFn) {
35471
35677
  const authorizationEndpoint = config.authorizationEndpoint ?? DEFAULT_AUTHORIZATION_ENDPOINT;
35472
35678
  const tokenEndpoint = config.tokenEndpoint ?? DEFAULT_TOKEN_ENDPOINT;
35473
- const codeVerifier = generateCodeVerifier2();
35474
- const codeChallenge = generateCodeChallenge(codeVerifier);
35475
- const loopbackSession = await createLoopbackAuthorizationSession({
35679
+ const codeVerifier = generateCodeVerifier3();
35680
+ const codeChallenge = generateCodeChallenge2(codeVerifier);
35681
+ const loopbackSession = await createLoopbackAuthorizationSession2({
35476
35682
  openBrowser: config.openBrowser,
35477
35683
  readLine: config.readLine,
35478
35684
  createServer: config.createServer,
@@ -35559,7 +35765,8 @@ var DEFAULT_AUTHORIZATION_ENDPOINT, DEFAULT_TOKEN_ENDPOINT;
35559
35765
  var init_oauth_client = __esm({
35560
35766
  "packages/poe-oauth/src/oauth-client.ts"() {
35561
35767
  "use strict";
35562
- init_dist();
35768
+ init_loopback_authorization2();
35769
+ init_pkce2();
35563
35770
  DEFAULT_AUTHORIZATION_ENDPOINT = "https://poe.com/oauth/authorize";
35564
35771
  DEFAULT_TOKEN_ENDPOINT = "https://api.poe.com/token";
35565
35772
  }
@@ -81069,7 +81276,7 @@ var init_package2 = __esm({
81069
81276
  "package.json"() {
81070
81277
  package_default2 = {
81071
81278
  name: "poe-code",
81072
- version: "3.0.208",
81279
+ version: "3.0.209",
81073
81280
  description: "CLI tool to configure Poe API for developer workflows.",
81074
81281
  type: "module",
81075
81282
  main: "./dist/index.js",