ToolJet/plugins/packages/common/lib/utils.helper.ts
Muhsin Shah C P 638fcf7223
[Feature] Now multiple users can login through RestAPI-Oauth 🚀 (#4052)
* Implemented multiple access token feature
- working on refresh token case

* worked on refresh token flow

* added multiple token ability to openapi plugin too
- fixed some bugs

* changed tokenData to token_data
- fixed some issues

* added user type with user id

* changed user type

* Rewrote some function
- added switch for enabling and disable multi auth
- fixed some bugs
- refactored the code

* fixed view app issue

* Fixed public app issue

* cleaning some code

* (public_app) add a check to avoid oauth login redirect when there is no access token

* reverted all changes of openapi (temporary)
- will add multi token functionality once done with restapi

* fixed a bug

* fixed a bug

* refactored some code

* changed the switch text

* pr changes

* changed token_data back to tokenData

* cleaning code

* removed token data from datasources query

* removed some lines

* added a comment
2022-09-19 20:27:37 +05:30

66 lines
1.7 KiB
TypeScript

import { QueryError } from './query.error';
const CACHED_CONNECTIONS: any = {};
export function parseJson(jsonString: string, errorMessage?: string): object {
try {
return JSON.parse(jsonString);
} catch (err) {
throw new QueryError(errorMessage, err.message, {});
}
}
export function cacheConnection(dataSourceId: string, connection: any): any {
const updatedAt = new Date();
CACHED_CONNECTIONS[dataSourceId] = { connection, updatedAt };
}
export function getCachedConnection(dataSourceId: string | number, dataSourceUpdatedAt: any): any {
const cachedData = CACHED_CONNECTIONS[dataSourceId];
if (cachedData) {
const updatedAt = new Date(dataSourceUpdatedAt || null);
const cachedAt = new Date(cachedData.updatedAt || null);
const diffTime = (cachedAt.getTime() - updatedAt.getTime()) / 1000;
if (diffTime < 0) {
return null;
} else {
return cachedData['connection'];
}
}
}
export function cleanSensitiveData(data, keys) {
if (!data || typeof data !== 'object') return;
const dataObj = { ...data };
clearData(dataObj, keys);
return dataObj;
}
function clearData(data, keys) {
if (!data || typeof data !== 'object') return;
for (const key in data) {
if (keys.includes(key)) {
delete data[key];
} else {
clearData(data[key], keys);
}
}
}
export const getCurrentToken = (isMultiAuthEnabled: boolean, tokenData: any, userId: string, isAppPublic: boolean) => {
if (isMultiAuthEnabled) {
if (!tokenData || !Array.isArray(tokenData)) return null;
return !isAppPublic
? tokenData.find((token: any) => token.user_id === userId)
: userId
? tokenData.find((token: any) => token.user_id === userId)
: tokenData[0];
} else {
return tokenData;
}
};