create customHeaders handler

This commit is contained in:
GleidsonDaniel 2023-11-22 15:50:53 -03:00
parent 4a98961da1
commit a7b914bfbe
6 changed files with 57 additions and 44 deletions

View File

@ -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
}}
/>

View File

@ -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;

View File

@ -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<TServerInfoResult> {
try {
const response = await RNFetchBlob.fetch('GET', `${server}/api/info`, {
...RocketChatSettings.customHeaders
...customHeaders.getHeaders()
});
try {
const jsonRes: IApiServerInfo = response.json();

View File

@ -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<Response> => {
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);
};

View File

@ -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<FetchBlobResponse> } = {};
@ -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

View File

@ -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 });
}
});