From a7b914bfbe4eac70d93fa6f6d4cb1880209b127f Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Wed, 22 Nov 2023 15:50:53 -0300 Subject: [PATCH] create customHeaders handler --- app/containers/Avatar/Avatar.tsx | 4 +-- app/lib/methods/customHeaders.ts | 41 ++++++++++++++++++++++++++++++ app/lib/methods/getServerInfo.ts | 4 +-- app/lib/methods/helpers/fetch.ts | 40 +++++------------------------ app/lib/methods/sendFileMessage.ts | 4 +-- app/lib/services/twoFactor.ts | 8 +++--- 6 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 app/lib/methods/customHeaders.ts diff --git a/app/containers/Avatar/Avatar.tsx b/app/containers/Avatar/Avatar.tsx index 7d10b2936..a8ab2b678 100644 --- a/app/containers/Avatar/Avatar.tsx +++ b/app/containers/Avatar/Avatar.tsx @@ -2,12 +2,12 @@ import React from 'react'; import { View } from 'react-native'; import FastImage from 'react-native-fast-image'; import Touchable from 'react-native-platform-touchable'; -import { settings as RocketChatSettings } from '@rocket.chat/sdk'; import { getAvatarURL } from '../../lib/methods/helpers/getAvatarUrl'; import { SubscriptionType } from '../../definitions'; import Emoji from '../markdown/Emoji'; import { IAvatar } from './interfaces'; +import customHeaders from '../../lib/methods/customHeaders'; const Avatar = React.memo( ({ @@ -72,7 +72,7 @@ const Avatar = React.memo( style={avatarStyle} source={{ uri, - headers: RocketChatSettings.customHeaders, + headers: customHeaders.getHeaders(), priority: FastImage.priority.high }} /> diff --git a/app/lib/methods/customHeaders.ts b/app/lib/methods/customHeaders.ts new file mode 100644 index 000000000..1a8426921 --- /dev/null +++ b/app/lib/methods/customHeaders.ts @@ -0,0 +1,41 @@ +import { Platform } from 'react-native'; +import DeviceInfo from 'react-native-device-info'; + +export interface ICustomHeaders { + 'User-Agent'?: string; + Authorization?: string; + 'Content-Type'?: string; + 'X-Auth-Token'?: string; + 'X-User-Id'?: string; + 'x-2fa-code'?: string; + 'x-2fa-method'?: string; +} + +const defaultHeaders = { + 'User-Agent': `RC Mobile; ${ + Platform.OS + } ${DeviceInfo.getSystemVersion()}; v${DeviceInfo.getVersion()} (${DeviceInfo.getBuildNumber()})` +}; + +class CustomHeaders { + headers: ICustomHeaders; + constructor() { + this.headers = { ...defaultHeaders }; + } + + setHeaders(headers: ICustomHeaders) { + this.headers = { ...this.headers, ...headers }; + } + + getHeaders() { + return this.headers as { [key: string]: string }; + } + + resetHeaders() { + this.headers = { ...defaultHeaders }; + } +} + +const customHeaders = new CustomHeaders(); + +export default customHeaders; diff --git a/app/lib/methods/getServerInfo.ts b/app/lib/methods/getServerInfo.ts index c05d4e8f8..8703c2093 100644 --- a/app/lib/methods/getServerInfo.ts +++ b/app/lib/methods/getServerInfo.ts @@ -1,5 +1,4 @@ import RNFetchBlob from 'rn-fetch-blob'; -import { settings as RocketChatSettings } from '@rocket.chat/sdk'; import { KJUR } from 'jsrsasign'; import moment from 'moment'; @@ -11,6 +10,7 @@ import I18n from '../../i18n'; import { SIGNED_SUPPORTED_VERSIONS_PUBLIC_KEY } from '../constants'; import { getServerById } from '../database/services/Server'; import log from './helpers/log'; +import customHeaders from './customHeaders'; interface IServerInfoFailure { success: false; @@ -46,7 +46,7 @@ const verifyJWT = (jwt?: string): ISupportedVersionsData | null => { export async function getServerInfo(server: string): Promise { try { const response = await RNFetchBlob.fetch('GET', `${server}/api/info`, { - ...RocketChatSettings.customHeaders + ...customHeaders.getHeaders() }); try { const jsonRes: IApiServerInfo = response.json(); diff --git a/app/lib/methods/helpers/fetch.ts b/app/lib/methods/helpers/fetch.ts index d15a050e8..09bfddb5c 100644 --- a/app/lib/methods/helpers/fetch.ts +++ b/app/lib/methods/helpers/fetch.ts @@ -1,54 +1,28 @@ -import { Platform } from 'react-native'; -import DeviceInfo from 'react-native-device-info'; -import { settings as RocketChatSettings } from '@rocket.chat/sdk'; +import customHeaders, { ICustomHeaders } from '../customHeaders'; export type TMethods = 'POST' | 'GET' | 'DELETE' | 'PUT' | 'post' | 'get' | 'delete' | 'put'; -interface CustomHeaders { - 'User-Agent'?: string; - Authorization?: string; - 'Content-Type'?: string; - 'X-Auth-Token'?: string; - 'X-User-Id'?: string; -} +export const BASIC_AUTH_KEY = 'BASIC_AUTH_KEY'; interface IOptions { - headers?: CustomHeaders; - signal?: AbortSignal; + headers?: ICustomHeaders; method?: TMethods; body?: any; } -// this form is required by Rocket.Chat's parser in "app/statistics/server/lib/UAParserCustom.js" -export const headers: CustomHeaders = { - 'User-Agent': `RC Mobile; ${ - Platform.OS - } ${DeviceInfo.getSystemVersion()}; v${DeviceInfo.getVersion()} (${DeviceInfo.getBuildNumber()})` -}; - -let _basicAuth; export const setBasicAuth = (basicAuth: string | null): void => { - _basicAuth = basicAuth; if (basicAuth) { - RocketChatSettings.customHeaders = { ...headers, Authorization: `Basic ${_basicAuth}` }; + customHeaders.setHeaders({ Authorization: `Basic ${basicAuth}` }); } else { - RocketChatSettings.customHeaders = headers; + customHeaders.resetHeaders(); } }; -export const BASIC_AUTH_KEY = 'BASIC_AUTH_KEY'; - -RocketChatSettings.customHeaders = headers; export default (url: string, options: IOptions = {}): Promise => { - let customOptions = { ...options, headers: RocketChatSettings.customHeaders }; + let customOptions = { ...options, headers: customHeaders.getHeaders() }; if (options && options.headers) { customOptions = { ...customOptions, headers: { ...options.headers, ...customOptions.headers } }; } - // TODO: Check if this really works and if anyone else has complained about this problem. - // if (RocketChat.controller) { - // // @ts-ignore - // const { signal } = RocketChat.controller; - // customOptions = { ...customOptions, signal }; - // } + return fetch(url, customOptions); }; diff --git a/app/lib/methods/sendFileMessage.ts b/app/lib/methods/sendFileMessage.ts index 8886f6064..689bd1279 100644 --- a/app/lib/methods/sendFileMessage.ts +++ b/app/lib/methods/sendFileMessage.ts @@ -1,5 +1,4 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; -import { settings as RocketChatSettings } from '@rocket.chat/sdk'; import isEmpty from 'lodash/isEmpty'; import { FetchBlobResponse, StatefulPromise } from 'rn-fetch-blob'; import { Alert } from 'react-native'; @@ -10,6 +9,7 @@ import database from '../database'; import FileUpload from './helpers/fileUpload'; import { IFileUpload } from './helpers/fileUpload/interfaces'; import log from './helpers/log'; +import customHeaders from './customHeaders'; const uploadQueue: { [index: string]: StatefulPromise } = {}; @@ -115,7 +115,7 @@ export function sendFileMessage( } const headers = { - ...RocketChatSettings.customHeaders, + ...customHeaders.getHeaders(), 'Content-Type': 'multipart/form-data', 'X-Auth-Token': token, 'X-User-Id': id diff --git a/app/lib/services/twoFactor.ts b/app/lib/services/twoFactor.ts index 928bf8c3f..70567b6bc 100644 --- a/app/lib/services/twoFactor.ts +++ b/app/lib/services/twoFactor.ts @@ -1,6 +1,5 @@ -import { settings } from '@rocket.chat/sdk'; - import { TWO_FACTOR } from '../../containers/TwoFactor'; +import customHeaders from '../methods/customHeaders'; import EventEmitter from '../methods/helpers/events'; interface ITwoFactor { @@ -15,11 +14,10 @@ export const twoFactor = ({ method, invalid }: ITwoFactor): Promise<{ twoFactorC invalid, cancel: () => reject(), submit: (code: string) => { - settings.customHeaders = { - ...settings.customHeaders, + customHeaders.setHeaders({ 'x-2fa-code': code, 'x-2fa-method': method - }; + }); resolve({ twoFactorCode: code, twoFactorMethod: method }); } });