From 2ec7a6bba69d4db1ac05882a4945487f5e69dddf Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Fri, 1 Feb 2019 19:38:04 -0300 Subject: [PATCH] MM-13859 set the auth token when fetching images from the server (#2537) * MM-13859 set the auth token when fetching images from the server * set the image_cache siteURL from redux * HH suggestion Co-Authored-By: enahum * Removing comment --- app/store/index.js | 9 +++++++++ app/utils/image_cache_manager.js | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/store/index.js b/app/store/index.js index e50d74054..bde927cd4 100644 --- a/app/store/index.js +++ b/app/store/index.js @@ -8,12 +8,14 @@ import {createTransform, persistStore} from 'redux-persist'; import {ErrorTypes, GeneralTypes} from 'mattermost-redux/action_types'; import {General, RequestStatus} from 'mattermost-redux/constants'; +import {getConfig} from 'mattermost-redux/selectors/entities/general'; import configureStore from 'mattermost-redux/store'; import EventEmitter from 'mattermost-redux/utils/event_emitter'; import {NavigationTypes, ViewTypes} from 'app/constants'; import appReducer from 'app/reducers'; import {throttle} from 'app/utils/general'; +import {getSiteUrl, setSiteUrl} from 'app/utils/image_cache_manager'; import {createSentryMiddleware} from 'app/utils/sentry/middleware'; import mattermostBucket from 'app/mattermost_bucket'; @@ -187,6 +189,12 @@ export default function configureAppStore(initialState) { // check to see if the logout request was successful store.subscribe(async () => { const state = store.getState(); + const config = getConfig(state); + + if (getSiteUrl() !== config?.SiteURL) { + setSiteUrl(config.SiteURL); + } + if ((state.requests.users.logout.status === RequestStatus.SUCCESS || state.requests.users.logout.status === RequestStatus.FAILURE) && !purging) { purging = true; @@ -211,6 +219,7 @@ export default function configureAppStore(initialState) { mattermostBucket.removePreference('cert', Config.AppGroupId); mattermostBucket.removePreference('emm', Config.AppGroupId); mattermostBucket.removeFile('entities', Config.AppGroupId); + setSiteUrl(null); setTimeout(() => { purging = false; diff --git a/app/utils/image_cache_manager.js b/app/utils/image_cache_manager.js index 98a086ffa..e911d3ba6 100644 --- a/app/utils/image_cache_manager.js +++ b/app/utils/image_cache_manager.js @@ -6,12 +6,15 @@ import {Platform} from 'react-native'; import RNFetchBlob from 'rn-fetch-blob'; +import {Client4} from 'mattermost-redux/client'; + import {DeviceTypes} from 'app/constants'; import mattermostBucket from 'app/mattermost_bucket'; import LocalConfig from 'assets/config'; const {IMAGES_PATH} = DeviceTypes; +let siteUrl; export default class ImageCacheManager { static listeners = {}; @@ -41,7 +44,13 @@ export default class ImageCacheManager { certificate, }; - this.downloadTask = await RNFetchBlob.config(options).fetch('GET', uri); + const headers = {}; + if (uri.includes(Client4.getUrl()) || uri.includes(siteUrl)) { + headers.Authorization = `Bearer ${Client4.getToken()}`; + headers['X-Requested-With'] = 'XMLHttpRequest'; + } + + this.downloadTask = await RNFetchBlob.config(options).fetch('GET', uri, headers); if (this.downloadTask.respInfo.respType === 'text') { throw new Error(); } @@ -79,6 +88,14 @@ export const getCacheFile = async (name, uri) => { return {exists, path}; }; +export const getSiteUrl = () => { + return siteUrl; +}; + +export const setSiteUrl = (url) => { + siteUrl = url; +}; + const isDownloading = (uri) => Boolean(ImageCacheManager.listeners[uri]); const addListener = (uri, listener) => {