element-web/src/components/views/elements/AppPermission.js

97 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-07-26 13:28:43 +03:00
import React from 'react';
import PropTypes from 'prop-types';
2017-07-26 18:47:58 +03:00
import url from 'url';
2017-07-28 18:39:18 +03:00
import { _t } from '../../../languageHandler';
import SdkConfig from '../../../SdkConfig';
2017-07-26 13:28:43 +03:00
export default class AppPermission extends React.Component {
constructor(props) {
super(props);
const curlBase = this.getCurlBase();
2017-07-28 18:36:06 +03:00
this.state = { curlBase: curlBase};
2017-07-26 13:28:43 +03:00
}
// Return string representation of content URL without query parameters
getCurlBase() {
2017-07-26 18:47:58 +03:00
const wurl = url.parse(this.props.url);
let curl;
let curlString;
2017-07-26 18:47:58 +03:00
const searchParams = new URLSearchParams(wurl.search);
2017-11-16 16:19:36 +03:00
if (this.isScalarWurl(wurl) && searchParams && searchParams.get('url')) {
curl = url.parse(searchParams.get('url'));
2017-11-16 16:19:36 +03:00
if (curl) {
curl.search = curl.query = "";
curlString = curl.format();
}
2017-07-26 13:28:43 +03:00
}
if (!curl && wurl) {
wurl.search = wurl.query = "";
curlString = wurl.format();
}
return curlString;
2017-07-26 13:28:43 +03:00
}
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;
}
2017-07-26 13:28:43 +03:00
render() {
let e2eWarningText;
if (this.props.isRoomEncrypted) {
e2eWarningText =
<span className='mx_AppPermissionWarningTextLabel'>{ _t('NOTE: Apps are not end-to-end encrypted') }</span>;
}
const cookieWarning =
<span className='mx_AppPermissionWarningTextLabel'>
{ _t('Warning: This widget might use cookies.') }
</span>;
2017-07-26 13:28:43 +03:00
return (
2017-07-26 18:47:58 +03:00
<div className='mx_AppPermissionWarning'>
<div className='mx_AppPermissionWarningImage'>
<img src='img/warning.svg' alt={_t('Warning!')} />
2017-07-26 18:47:58 +03:00
</div>
<div className='mx_AppPermissionWarningText'>
<span className='mx_AppPermissionWarningTextLabel'>{ _t('Do you want to load widget from URL:') }</span> <span className='mx_AppPermissionWarningTextURL'>{ this.state.curlBase }</span>
{ e2eWarningText }
{ cookieWarning }
2017-07-26 18:47:58 +03:00
</div>
2017-07-26 13:28:43 +03:00
<input
2017-07-26 18:47:58 +03:00
className='mx_AppPermissionButton'
2017-07-26 13:28:43 +03:00
type='button'
2017-07-28 18:42:07 +03:00
value={_t('Allow')}
2017-07-26 13:28:43 +03:00
onClick={this.props.onPermissionGranted}
/>
</div>
);
}
}
AppPermission.propTypes = {
isRoomEncrypted: PropTypes.bool,
2017-07-26 13:28:43 +03:00
url: PropTypes.string.isRequired,
onPermissionGranted: PropTypes.func.isRequired,
};
2017-07-28 18:23:38 +03:00
AppPermission.defaultProps = {
isRoomEncrypted: false,
2017-07-26 13:28:43 +03:00
onPermissionGranted: function() {},
};