From 23162c86253cc5aca18f2a9a954ab56736055991 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 9 Oct 2017 18:50:08 -0600 Subject: [PATCH 1/3] Support third party integration managers in AppPermission Alternative integration managers may wish to also wrap widgets to supply a better user experience. With the previous code, it was not possible to use the integrations_widgets_urls configuration option (described in AppTile). AppPermission should use the same logic to determine if a widget is being wrapped, so it can display a helpful URL for the user (instead of the wrapper URL). Signed-off-by: Travis Ralston --- .../views/elements/AppPermission.js | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/components/views/elements/AppPermission.js b/src/components/views/elements/AppPermission.js index c45006be3a..0ce7c2a45e 100644 --- a/src/components/views/elements/AppPermission.js +++ b/src/components/views/elements/AppPermission.js @@ -2,6 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import url from 'url'; import { _t } from '../../../languageHandler'; +import SdkConfig from '../../../SdkConfig'; export default class AppPermission extends React.Component { constructor(props) { @@ -34,14 +35,21 @@ export default class AppPermission extends React.Component { } isScalarWurl(wurl) { - if (wurl && wurl.hostname && ( - wurl.hostname === 'scalar.vector.im' || - wurl.hostname === 'scalar-staging.riot.im' || - wurl.hostname === 'scalar-develop.riot.im' || - wurl.hostname === 'demo.riot.im' || - wurl.hostname === 'localhost' - )) { - return true; + // Exit early if we've been given bad data + if (!wurl) { + return false; + } + + let scalarUrls = SdkConfig.get().integrations_widgets_urls; + if (!scalarUrls || scalarUrls.length == 0) { + scalarUrls = [SdkConfig.get().integrations_rest_url]; + } + + const url = wurl.format(); + for (const scalarUrl of scalarUrls) { + if (url.startsWith(scalarUrl)) { + return true; + } } return false; } From 98613748b63a338d21b800d41004f070562a0ffb Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Sun, 27 May 2018 11:12:55 -0600 Subject: [PATCH 2/3] Move Scalar Widget URL check to WidgetUtils Signed-off-by: Travis Ralston --- src/WidgetUtils.js | 35 +++++++++++++++++++ .../views/elements/AppPermission.js | 23 ++---------- src/components/views/elements/AppTile.js | 35 +------------------ 3 files changed, 38 insertions(+), 55 deletions(-) diff --git a/src/WidgetUtils.js b/src/WidgetUtils.js index 5f45a8c58c..10cd473904 100644 --- a/src/WidgetUtils.js +++ b/src/WidgetUtils.js @@ -15,6 +15,8 @@ limitations under the License. */ import MatrixClientPeg from './MatrixClientPeg'; +import SdkConfig from "./SdkConfig"; +import * as url from "url"; export default class WidgetUtils { /* Returns true if user is able to send state events to modify widgets in this room @@ -55,4 +57,37 @@ export default class WidgetUtils { return room.currentState.maySendStateEvent('im.vector.modular.widgets', me); } + + /** + * Returns true if specified url is a scalar URL, typically https://scalar.vector.im/api + * @param {[type]} testUrlString URL to check + * @return {Boolean} True if specified URL is a scalar URL + */ + static isScalarUrl(testUrlString) { + if (!testUrlString) { + console.error('Scalar URL check failed. No URL specified'); + return false; + } + + const testUrl = url.parse(testUrlString); + + let scalarUrls = SdkConfig.get().integrations_widgets_urls; + if (!scalarUrls || scalarUrls.length === 0) { + scalarUrls = [SdkConfig.get().integrations_rest_url]; + } + + for (let i = 0; i < scalarUrls.length; i++) { + const scalarUrl = url.parse(scalarUrls[i]); + if (testUrl && scalarUrl) { + if ( + testUrl.protocol === scalarUrl.protocol && + testUrl.host === scalarUrl.host && + testUrl.pathname.startsWith(scalarUrl.pathname) + ) { + return true; + } + } + } + return false; + } } diff --git a/src/components/views/elements/AppPermission.js b/src/components/views/elements/AppPermission.js index 0ce7c2a45e..6af3148e21 100644 --- a/src/components/views/elements/AppPermission.js +++ b/src/components/views/elements/AppPermission.js @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'; import url from 'url'; import { _t } from '../../../languageHandler'; import SdkConfig from '../../../SdkConfig'; +import WidgetUtils from "../../../WidgetUtils"; export default class AppPermission extends React.Component { constructor(props) { @@ -20,7 +21,7 @@ export default class AppPermission extends React.Component { const searchParams = new URLSearchParams(wurl.search); - if (this.isScalarWurl(wurl) && searchParams && searchParams.get('url')) { + if (WidgetUtils.isScalarUrl(wurl) && searchParams && searchParams.get('url')) { curl = url.parse(searchParams.get('url')); if (curl) { curl.search = curl.query = ""; @@ -34,26 +35,6 @@ export default class AppPermission extends React.Component { return curlString; } - isScalarWurl(wurl) { - // Exit early if we've been given bad data - if (!wurl) { - return false; - } - - let scalarUrls = SdkConfig.get().integrations_widgets_urls; - if (!scalarUrls || scalarUrls.length == 0) { - scalarUrls = [SdkConfig.get().integrations_rest_url]; - } - - const url = wurl.format(); - for (const scalarUrl of scalarUrls) { - if (url.startsWith(scalarUrl)) { - return true; - } - } - return false; - } - render() { let e2eWarningText; if (this.props.isRoomEncrypted) { diff --git a/src/components/views/elements/AppTile.js b/src/components/views/elements/AppTile.js index 5f3ed6eba1..0ec754e0c0 100644 --- a/src/components/views/elements/AppTile.js +++ b/src/components/views/elements/AppTile.js @@ -121,39 +121,6 @@ export default class AppTile extends React.Component { return u.format(); } - /** - * Returns true if specified url is a scalar URL, typically https://scalar.vector.im/api - * @param {[type]} testUrlString URL to check - * @return {Boolean} True if specified URL is a scalar URL - */ - isScalarUrl(testUrlString) { - if (!testUrlString) { - console.error('Scalar URL check failed. No URL specified'); - return false; - } - - const testUrl = url.parse(testUrlString); - - let scalarUrls = SdkConfig.get().integrations_widgets_urls; - if (!scalarUrls || scalarUrls.length == 0) { - scalarUrls = [SdkConfig.get().integrations_rest_url]; - } - - for (let i = 0; i < scalarUrls.length; i++) { - const scalarUrl = url.parse(scalarUrls[i]); - if (testUrl && scalarUrl) { - if ( - testUrl.protocol === scalarUrl.protocol && - testUrl.host === scalarUrl.host && - testUrl.pathname.startsWith(scalarUrl.pathname) - ) { - return true; - } - } - } - return false; - } - isMixedContent() { const parentContentProtocol = window.location.protocol; const u = url.parse(this.props.url); @@ -209,7 +176,7 @@ export default class AppTile extends React.Component { setScalarToken() { this.setState({initialising: true}); - if (!this.isScalarUrl(this.props.url)) { + if (!WidgetUtils.isScalarUrl(this.props.url)) { console.warn('Non-scalar widget, not setting scalar token!', url); this.setState({ error: null, From 254e8c358aaf5cd7c20d7c005dccd805c1697ee8 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 28 May 2018 11:38:09 -0600 Subject: [PATCH 3/3] Remove unused imports Signed-off-by: Travis Ralston --- src/components/views/elements/AppPermission.js | 1 - src/components/views/elements/AppTile.js | 1 - 2 files changed, 2 deletions(-) diff --git a/src/components/views/elements/AppPermission.js b/src/components/views/elements/AppPermission.js index 6af3148e21..231ed52364 100644 --- a/src/components/views/elements/AppPermission.js +++ b/src/components/views/elements/AppPermission.js @@ -2,7 +2,6 @@ import React from 'react'; import PropTypes from 'prop-types'; import url from 'url'; import { _t } from '../../../languageHandler'; -import SdkConfig from '../../../SdkConfig'; import WidgetUtils from "../../../WidgetUtils"; export default class AppPermission extends React.Component { diff --git a/src/components/views/elements/AppTile.js b/src/components/views/elements/AppTile.js index 0ec754e0c0..429b5941b9 100644 --- a/src/components/views/elements/AppTile.js +++ b/src/components/views/elements/AppTile.js @@ -25,7 +25,6 @@ import PlatformPeg from '../../../PlatformPeg'; import ScalarAuthClient from '../../../ScalarAuthClient'; import WidgetMessaging from '../../../WidgetMessaging'; import TintableSvgButton from './TintableSvgButton'; -import SdkConfig from '../../../SdkConfig'; import Modal from '../../../Modal'; import { _t, _td } from '../../../languageHandler'; import sdk from '../../../index';