Skip to content

Commit

Permalink
fix(auth): allow app totp + fix broken cookie auth opral#2 opral#3
Browse files Browse the repository at this point in the history
  • Loading branch information
macintoshhelper committed Jul 10, 2024
1 parent 31db2ea commit 38dd166
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
19 changes: 14 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"version": "0.0.14",
"dependencies": {
"commander": "^11.0.0",
"cookie": "^0.6.0",
"read": "^2.1.0"
},
"name": "figcd",
Expand Down
37 changes: 21 additions & 16 deletions src/auth-helper.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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:',
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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]);
}
});

Expand Down

0 comments on commit 38dd166

Please sign in to comment.