diff --git a/package-lock.json b/package-lock.json index 8fe9edd..5cf6735 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,20 @@ { - "name": "@parrots.design/parrot-cd", - "version": "0.0.3", + "name": "figcd", + "version": "0.0.14", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "@parrots.design/parrot-cd", - "version": "0.0.3", + "name": "figcd", + "version": "0.0.14", "license": "MIT", "dependencies": { "commander": "^11.0.0", + "cookie": "^0.6.0", "read": "^2.1.0" }, "bin": { - "parrot-cd": "bin/cli.js" + "figcd": "bin/cli.js" }, "devDependencies": {} }, @@ -25,6 +26,14 @@ "node": ">=16" } }, + "node_modules/cookie": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/mute-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-1.0.0.tgz", diff --git a/package.json b/package.json index 549f7a6..e8dc960 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "version": "0.0.14", "dependencies": { "commander": "^11.0.0", + "cookie": "^0.6.0", "read": "^2.1.0" }, "name": "figcd", diff --git a/src/auth-helper.js b/src/auth-helper.js index f84085d..87dc147 100644 --- a/src/auth-helper.js +++ b/src/auth-helper.js @@ -1,9 +1,13 @@ const fs = require('fs'); -var read = require('read'); +const read = require('read'); +const cookie = require('cookie'); let figmaEmail; let figmaPassword; +const figmaCookie = process.env.FIGMA_COOKIE; +const figmaTsid = process.env.FIGMA_TSID; + const figmaUrl = 'https://www.figma.com/'; function cookiesSufficient(cookies) { @@ -18,7 +22,6 @@ async function wait(ms) { module.exports = { authenticate: async function () { - if (!figmaEmail) { figmaEmail = await read({ prompt: 'Please enter the email address of your Figma account:', @@ -37,6 +40,8 @@ module.exports = { "headers": { "accept": "application/json", "content-type": "application/json", + "tsid": figmaTsid, + "cookie": figmaCookie, "x-csrf-bypass": "yes", }, "referrer": "https://www.figma.com/login", @@ -63,24 +68,22 @@ module.exports = { || secondFactorTriggerLoginResult.reason.missing === undefined)) { console.log('something went wrong - got 400 but expected two factor request'); throw new Error('something went wrong - got 400 but expected two factor request'); - } else if (secondFactorTriggerLogin.status === 400 - && (secondFactorTriggerLoginResult.reason !== undefined - && !secondFactorTriggerLoginResult.reason.sms)) { - - console.log('Non SMS second factor currently not supported'); - throw new Error('Non SMS second factor currently not supported'); } else if (secondFactorTriggerLogin.status !== 400) { console.log('something went wrong - expected two factor response but got status' + secondFactorTriggerLogin.status); } - const secondFactor = await read({ + const secondFactor = secondFactorTriggerLoginResult.reason.phone_number ? await read({ prompt: 'SMS sent to number ending in (' + secondFactorTriggerLoginResult.reason.phone_number + '): please enter the Authentication code:' + }) : await read({ + prompt: 'Please enter the TOTP authentication code:' }); const loginResponse = await fetch("https://www.figma.com/api/session/login", { "headers": { "accept": "application/json", "content-type": "application/json", + "tsid": figmaTsid, + "cookie": figmaCookie, "x-csrf-bypass": "yes", }, "referrer": "https://www.figma.com/login", @@ -95,15 +98,17 @@ module.exports = { "mode": "cors", "credentials": "include" }); - const loginResponseResult = await loginResponse.json(); + // const loginResponseResult = await loginResponse.json(); - const cookiesReceived = loginResponse.headers.get('set-cookie').split('; '); - const authnTokenCookie = {}; + const cookiesReceived = loginResponse.headers?.getSetCookie() + const authnTokenCookie = { + name: '__Host-figma.authn', + value: undefined + }; cookiesReceived.forEach(rawCookie => { - const [name, value] = rawCookie.split('='); - if (name === '__Host-figma.authn') { - authnTokenCookie.name = name; - authnTokenCookie.value = value; + const parsedCookie = cookie.parse(rawCookie); + if (parsedCookie[authnTokenCookie.name]) { + authnTokenCookie.value = encodeURIComponent(parsedCookie[authnTokenCookie.name]); } });