Merge pull request #156 from getmaxun/jwt-fix

fix: gracefully handle jwt token decoding
This commit is contained in:
Karishma Shukla
2024-11-11 04:37:03 +05:30
committed by GitHub
3 changed files with 137 additions and 88 deletions

View File

@@ -1,6 +1,6 @@
import { Model, DataTypes, Optional } from 'sequelize'; import { Model, DataTypes, Optional } from "sequelize";
import sequelize from '../storage/db'; import sequelize from "../storage/db";
import { WorkflowFile, Where, What, WhereWhatPair } from 'maxun-core'; import { WorkflowFile, Where, What, WhereWhatPair } from "maxun-core";
interface RobotMeta { interface RobotMeta {
name: string; name: string;
@@ -15,23 +15,42 @@ interface RobotWorkflow {
workflow: WhereWhatPair[]; workflow: WhereWhatPair[];
} }
interface IntegrationData {
google_sheets?: {
email: string;
sheet_id: string;
sheet_name: string;
access_token: string;
refresh_token: string;
};
airtable?: {
base_id: string;
table_name: string;
access_token: string;
refresh_token: string;
};
}
interface RobotAttributes { interface RobotAttributes {
id: string; id: string;
userId?: number; userId?: number;
recording_meta: RobotMeta; recording_meta: RobotMeta;
recording: RobotWorkflow; recording: RobotWorkflow;
google_sheet_email?: string | null;
google_sheet_name?: string | null;
google_sheet_id?: string | null;
google_access_token?: string | null;
google_refresh_token?: string | null;
schedule?: ScheduleConfig | null; schedule?: ScheduleConfig | null;
integrations?: IntegrationData | null;
} }
interface ScheduleConfig { interface ScheduleConfig {
runEvery: number; runEvery: number;
runEveryUnit: 'MINUTES' | 'HOURS' | 'DAYS' | 'WEEKS' | 'MONTHS'; runEveryUnit: "MINUTES" | "HOURS" | "DAYS" | "WEEKS" | "MONTHS";
startFrom: 'SUNDAY' | 'MONDAY' | 'TUESDAY' | 'WEDNESDAY' | 'THURSDAY' | 'FRIDAY' | 'SATURDAY'; startFrom:
| "SUNDAY"
| "MONDAY"
| "TUESDAY"
| "WEDNESDAY"
| "THURSDAY"
| "FRIDAY"
| "SATURDAY";
atTimeStart?: string; atTimeStart?: string;
atTimeEnd?: string; atTimeEnd?: string;
timezone: string; timezone: string;
@@ -41,19 +60,18 @@ interface ScheduleConfig {
cronExpression?: string; cronExpression?: string;
} }
interface RobotCreationAttributes extends Optional<RobotAttributes, 'id'> { } interface RobotCreationAttributes extends Optional<RobotAttributes, "id"> {}
class Robot extends Model<RobotAttributes, RobotCreationAttributes> implements RobotAttributes { class Robot
extends Model<RobotAttributes, RobotCreationAttributes>
implements RobotAttributes
{
public id!: string; public id!: string;
public userId!: number; public userId!: number;
public recording_meta!: RobotMeta; public recording_meta!: RobotMeta;
public recording!: RobotWorkflow; public recording!: RobotWorkflow;
public google_sheet_email!: string | null;
public google_sheet_name?: string | null;
public google_sheet_id?: string | null;
public google_access_token!: string | null;
public google_refresh_token!: string | null;
public schedule!: ScheduleConfig | null; public schedule!: ScheduleConfig | null;
public integrations!: IntegrationData | null;
} }
Robot.init( Robot.init(
@@ -75,25 +93,10 @@ Robot.init(
type: DataTypes.JSONB, type: DataTypes.JSONB,
allowNull: false, allowNull: false,
}, },
google_sheet_email: { integrations: {
type: DataTypes.STRING, type: DataTypes.JSONB,
allowNull: true,
},
google_sheet_name: {
type: DataTypes.STRING,
allowNull: true,
},
google_sheet_id: {
type: DataTypes.STRING,
allowNull: true,
},
google_access_token: {
type: DataTypes.STRING,
allowNull: true,
},
google_refresh_token: {
type: DataTypes.STRING,
allowNull: true, allowNull: true,
defaultValue: {},
}, },
schedule: { schedule: {
type: DataTypes.JSONB, type: DataTypes.JSONB,
@@ -102,7 +105,7 @@ Robot.init(
}, },
{ {
sequelize, sequelize,
tableName: 'robot', tableName: "robot",
timestamps: false, timestamps: false,
} }
); );

View File

@@ -6,13 +6,14 @@ import Robot from "../../models/Robot";
interface GoogleSheetUpdateTask { interface GoogleSheetUpdateTask {
robotId: string; robotId: string;
runId: string; runId: string;
status: 'pending' | 'completed' | 'failed'; status: "pending" | "completed" | "failed";
retries: number; retries: number;
} }
const MAX_RETRIES = 5; const MAX_RETRIES = 5;
export let googleSheetUpdateTasks: { [runId: string]: GoogleSheetUpdateTask } = {}; export let googleSheetUpdateTasks: { [runId: string]: GoogleSheetUpdateTask } =
{};
export async function updateGoogleSheet(robotId: string, runId: string) { export async function updateGoogleSheet(robotId: string, runId: string) {
try { try {
@@ -24,20 +25,26 @@ export async function updateGoogleSheet(robotId: string, runId: string) {
const plainRun = run.toJSON(); const plainRun = run.toJSON();
if (plainRun.status === 'success') { if (plainRun.status === "success") {
let data: { [key: string]: any }[] = []; let data: { [key: string]: any }[] = [];
if (plainRun.serializableOutput && Object.keys(plainRun.serializableOutput).length > 0) { if (
data = plainRun.serializableOutput['item-0'] as { [key: string]: any }[]; plainRun.serializableOutput &&
Object.keys(plainRun.serializableOutput).length > 0
} else if (plainRun.binaryOutput && plainRun.binaryOutput['item-0']) { ) {
data = plainRun.serializableOutput["item-0"] as {
[key: string]: any;
}[];
} else if (plainRun.binaryOutput && plainRun.binaryOutput["item-0"]) {
// Handle binaryOutput by setting the URL as a data entry // Handle binaryOutput by setting the URL as a data entry
const binaryUrl = plainRun.binaryOutput['item-0'] as string; const binaryUrl = plainRun.binaryOutput["item-0"] as string;
// Create a placeholder object with the binary URL // Create a placeholder object with the binary URL
data = [{ "Screenshot URL": binaryUrl }]; data = [{ "Screenshot URL": binaryUrl }];
} }
const robot = await Robot.findOne({ where: { 'recording_meta.id': robotId } }); const robot = await Robot.findOne({
where: { "recording_meta.id": robotId },
});
if (!robot) { if (!robot) {
throw new Error(`Robot not found for robotId: ${robotId}`); throw new Error(`Robot not found for robotId: ${robotId}`);
@@ -45,30 +52,47 @@ export async function updateGoogleSheet(robotId: string, runId: string) {
const plainRobot = robot.toJSON(); const plainRobot = robot.toJSON();
const spreadsheetId = plainRobot.google_sheet_id; const spreadsheetId = plainRobot.integrations?.google_sheets?.sheet_id;
if (plainRobot.google_sheet_email && spreadsheetId) {
console.log(`Preparing to write data to Google Sheet for robot: ${robotId}, spreadsheetId: ${spreadsheetId}`); if (plainRobot.integrations?.google_sheets?.email && spreadsheetId) {
console.log(
`Preparing to write data to Google Sheet for robot: ${robotId}, spreadsheetId: ${spreadsheetId}`
);
const headers = Object.keys(data[0]); const headers = Object.keys(data[0]);
const rows = data.map((row: { [key: string]: any }) => Object.values(row)); const rows = data.map((row: { [key: string]: any }) =>
Object.values(row)
);
const outputData = [headers, ...rows]; const outputData = [headers, ...rows];
await writeDataToSheet(robotId, spreadsheetId, outputData); await writeDataToSheet(robotId, spreadsheetId, outputData);
console.log(`Data written to Google Sheet successfully for Robot: ${robotId} and Run: ${runId}`); console.log(
`Data written to Google Sheet successfully for Robot: ${robotId} and Run: ${runId}`
);
} else { } else {
console.log('Google Sheets integration not configured.'); console.log("Google Sheets integration not configured.");
} }
} else { } else {
console.log('Run status is not success or serializableOutput is missing.'); console.log(
"Run status is not success or serializableOutput is missing."
);
} }
} catch (error: any) { } catch (error: any) {
console.error(`Failed to write data to Google Sheet for Robot: ${robotId} and Run: ${runId}: ${error.message}`); console.error(
`Failed to write data to Google Sheet for Robot: ${robotId} and Run: ${runId}: ${error.message}`
);
} }
}; }
export async function writeDataToSheet(robotId: string, spreadsheetId: string, data: any[]) { export async function writeDataToSheet(
robotId: string,
spreadsheetId: string,
data: any[]
) {
try { try {
const robot = await Robot.findOne({ where: { 'recording_meta.id': robotId } }); const robot = await Robot.findOne({
where: { "recording_meta.id": robotId },
});
if (!robot) { if (!robot) {
throw new Error(`Robot not found for robotId: ${robotId}`); throw new Error(`Robot not found for robotId: ${robotId}`);
@@ -76,8 +100,11 @@ export async function writeDataToSheet(robotId: string, spreadsheetId: string, d
const plainRobot = robot.toJSON(); const plainRobot = robot.toJSON();
if (!plainRobot.google_access_token || !plainRobot.google_refresh_token) { const access_token = plainRobot.integrations?.google_sheets?.access_token;
throw new Error('Google Sheets access not configured for user'); const refresh_token = plainRobot.integrations?.google_sheets?.refresh_token;
if (!access_token || !refresh_token) {
throw new Error("Google Sheets access not configured for user");
} }
const oauth2Client = new google.auth.OAuth2( const oauth2Client = new google.auth.OAuth2(
@@ -87,11 +114,11 @@ export async function writeDataToSheet(robotId: string, spreadsheetId: string, d
); );
oauth2Client.setCredentials({ oauth2Client.setCredentials({
access_token: plainRobot.google_access_token, access_token: access_token,
refresh_token: plainRobot.google_refresh_token, refresh_token: refresh_token,
}); });
oauth2Client.on('tokens', async (tokens) => { oauth2Client.on("tokens", async (tokens) => {
if (tokens.refresh_token) { if (tokens.refresh_token) {
await robot.update({ google_refresh_token: tokens.refresh_token }); await robot.update({ google_refresh_token: tokens.refresh_token });
} }
@@ -100,22 +127,22 @@ export async function writeDataToSheet(robotId: string, spreadsheetId: string, d
} }
}); });
const sheets = google.sheets({ version: 'v4', auth: oauth2Client }); const sheets = google.sheets({ version: "v4", auth: oauth2Client });
const resource = { values: data }; const resource = { values: data };
console.log('Attempting to write to spreadsheet:', spreadsheetId); console.log("Attempting to write to spreadsheet:", spreadsheetId);
const response = await sheets.spreadsheets.values.append({ const response = await sheets.spreadsheets.values.append({
spreadsheetId, spreadsheetId,
range: 'Sheet1!A1', range: "Sheet1!A1",
valueInputOption: 'USER_ENTERED', valueInputOption: "USER_ENTERED",
requestBody: resource, requestBody: resource,
}); });
if (response.status === 200) { if (response.status === 200) {
console.log('Data successfully appended to Google Sheet.'); console.log("Data successfully appended to Google Sheet.");
} else { } else {
console.error('Google Sheets append failed:', response); console.error("Google Sheets append failed:", response);
} }
logger.log(`info`, `Data written to Google Sheet: ${spreadsheetId}`); logger.log(`info`, `Data written to Google Sheet: ${spreadsheetId}`);
@@ -130,33 +157,42 @@ export const processGoogleSheetUpdates = async () => {
let hasPendingTasks = false; let hasPendingTasks = false;
for (const runId in googleSheetUpdateTasks) { for (const runId in googleSheetUpdateTasks) {
const task = googleSheetUpdateTasks[runId]; const task = googleSheetUpdateTasks[runId];
console.log(`Processing task for runId: ${runId}, status: ${task.status}`); console.log(
`Processing task for runId: ${runId}, status: ${task.status}`
);
if (task.status === 'pending') { if (task.status === "pending") {
hasPendingTasks = true; hasPendingTasks = true;
try { try {
await updateGoogleSheet(task.robotId, task.runId); await updateGoogleSheet(task.robotId, task.runId);
console.log(`Successfully updated Google Sheet for runId: ${runId}`); console.log(`Successfully updated Google Sheet for runId: ${runId}`);
delete googleSheetUpdateTasks[runId]; delete googleSheetUpdateTasks[runId];
} catch (error: any) { } catch (error: any) {
console.error(`Failed to update Google Sheets for run ${task.runId}:`, error); console.error(
`Failed to update Google Sheets for run ${task.runId}:`,
error
);
if (task.retries < MAX_RETRIES) { if (task.retries < MAX_RETRIES) {
googleSheetUpdateTasks[runId].retries += 1; googleSheetUpdateTasks[runId].retries += 1;
console.log(`Retrying task for runId: ${runId}, attempt: ${task.retries}`); console.log(
`Retrying task for runId: ${runId}, attempt: ${task.retries}`
);
} else { } else {
googleSheetUpdateTasks[runId].status = 'failed'; googleSheetUpdateTasks[runId].status = "failed";
console.log(`Max retries reached for runId: ${runId}. Marking task as failed.`); console.log(
`Max retries reached for runId: ${runId}. Marking task as failed.`
);
} }
} }
} }
} }
if (!hasPendingTasks) { if (!hasPendingTasks) {
console.log('No pending tasks. Exiting loop.'); console.log("No pending tasks. Exiting loop.");
break; break;
} }
console.log('Waiting for 5 seconds before checking again...'); console.log("Waiting for 5 seconds before checking again...");
await new Promise(resolve => setTimeout(resolve, 5000)); await new Promise((resolve) => setTimeout(resolve, 5000));
} }
}; };

View File

@@ -60,18 +60,28 @@ const AuthProvider = ({ children }: AuthProviderProps) => {
// Function to check token expiration // Function to check token expiration
const checkTokenExpiration = (token: string) => { const checkTokenExpiration = (token: string) => {
const decodedToken: any = jwtDecode(token); if (!token || typeof token !== 'string') {
const currentTime = Date.now(); console.warn("Invalid token provided for decoding");
const tokenExpiryTime = decodedToken.exp * 1000; // Convert to milliseconds logoutUser();
const timeUntilExpiry = tokenExpiryTime - currentTime; return;
}
try {
const decodedToken: any = jwtDecode(token);
const currentTime = Date.now();
const tokenExpiryTime = decodedToken.exp * 1000; // Convert to milliseconds
if (timeUntilExpiry > 0) { if (tokenExpiryTime > currentTime) {
setTimeout(logoutUser, timeUntilExpiry); // Auto-logout when token expires setTimeout(logoutUser, tokenExpiryTime - currentTime); // Auto-logout when token expires
} else { } else {
logoutUser(); // Immediately logout if token is expired logoutUser(); // Immediately logout if token is expired
}
} catch (error) {
console.error("Error decoding token:", error);
logoutUser(); // Logout on error
} }
}; };
useEffect(() => { useEffect(() => {
const storedUser = window.localStorage.getItem('user'); const storedUser = window.localStorage.getItem('user');
if (storedUser) { if (storedUser) {