2015-11-20 13:14:00 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-20 13:14:00 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-05-30 17:09:57 +03:00
|
|
|
import React from 'react';
|
2017-08-26 10:18:07 +03:00
|
|
|
import ReactDOM from 'react-dom';
|
2017-11-13 22:19:33 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-05-30 17:09:57 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const DIV_ID = 'mx_recaptcha';
|
2015-11-20 13:14:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A pure UI component which displays a captcha form.
|
|
|
|
*/
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'CaptchaForm',
|
|
|
|
|
|
|
|
propTypes: {
|
2016-10-11 20:00:47 +03:00
|
|
|
sitePublicKey: React.PropTypes.string,
|
|
|
|
|
|
|
|
// called with the captcha response
|
|
|
|
onCaptchaResponse: React.PropTypes.func,
|
2015-11-20 13:14:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2016-10-11 20:00:47 +03:00
|
|
|
onCaptchaResponse: () => {},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
errorText: null,
|
2015-11-20 13:14:00 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-06-16 12:25:51 +03:00
|
|
|
componentWillMount: function() {
|
|
|
|
this._captchaWidgetId = null;
|
|
|
|
},
|
|
|
|
|
2015-11-20 13:14:00 +03:00
|
|
|
componentDidMount: function() {
|
|
|
|
// Just putting a script tag into the returned jsx doesn't work, annoyingly,
|
|
|
|
// so we do this instead.
|
2016-10-11 20:00:47 +03:00
|
|
|
if (global.grecaptcha) {
|
|
|
|
// already loaded
|
|
|
|
this._onCaptchaLoaded();
|
|
|
|
} else {
|
2015-11-20 13:14:00 +03:00
|
|
|
console.log("Loading recaptcha script...");
|
2017-01-20 17:22:27 +03:00
|
|
|
window.mx_on_recaptcha_loaded = () => {this._onCaptchaLoaded();};
|
2017-10-11 19:56:17 +03:00
|
|
|
const protocol = global.location.protocol;
|
2016-12-24 06:15:30 +03:00
|
|
|
if (protocol === "file:") {
|
2017-10-11 19:56:17 +03:00
|
|
|
const warning = document.createElement('div');
|
2016-12-24 06:15:30 +03:00
|
|
|
// XXX: fix hardcoded app URL. Better solutions include:
|
|
|
|
// * jumping straight to a hosted captcha page (but we don't support that yet)
|
|
|
|
// * embedding the captcha in an iframe (if that works)
|
|
|
|
// * using a better captcha lib
|
2017-11-13 22:19:33 +03:00
|
|
|
ReactDOM.render(_t(
|
2017-08-26 10:18:07 +03:00
|
|
|
"Robot check is currently unavailable on desktop - please use a <a>web browser</a>",
|
2017-11-13 22:19:33 +03:00
|
|
|
{},
|
|
|
|
{ 'a': (sub) => { return <a href='https://riot.im/app'>{ sub }</a>; }}), warning);
|
2016-12-24 06:15:30 +03:00
|
|
|
this.refs.recaptchaContainer.appendChild(warning);
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
|
|
|
const scriptTag = document.createElement('script');
|
2016-12-24 06:15:30 +03:00
|
|
|
scriptTag.setAttribute(
|
2017-10-11 19:56:17 +03:00
|
|
|
'src', protocol+"//www.google.com/recaptcha/api.js?onload=mx_on_recaptcha_loaded&render=explicit",
|
2016-12-24 06:15:30 +03:00
|
|
|
);
|
|
|
|
this.refs.recaptchaContainer.appendChild(scriptTag);
|
|
|
|
}
|
2015-11-20 13:14:00 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-06-16 12:25:51 +03:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
this._resetRecaptcha();
|
|
|
|
},
|
|
|
|
|
2016-10-11 20:00:47 +03:00
|
|
|
_renderRecaptcha: function(divId) {
|
|
|
|
if (!global.grecaptcha) {
|
|
|
|
console.error("grecaptcha not loaded!");
|
|
|
|
throw new Error("Recaptcha did not load successfully");
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const publicKey = this.props.sitePublicKey;
|
2016-10-11 20:00:47 +03:00
|
|
|
if (!publicKey) {
|
|
|
|
console.error("No public key for recaptcha!");
|
|
|
|
throw new Error(
|
|
|
|
"This server has not supplied enough information for Recaptcha "
|
|
|
|
+ "authentication");
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Rendering to %s", divId);
|
2017-06-16 12:25:51 +03:00
|
|
|
this._captchaWidgetId = global.grecaptcha.render(divId, {
|
2016-10-11 20:00:47 +03:00
|
|
|
sitekey: publicKey,
|
|
|
|
callback: this.props.onCaptchaResponse,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-06-16 12:25:51 +03:00
|
|
|
_resetRecaptcha: function() {
|
|
|
|
if (this._captchaWidgetId !== null) {
|
|
|
|
global.grecaptcha.reset(this._captchaWidgetId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-11 20:00:47 +03:00
|
|
|
_onCaptchaLoaded: function() {
|
|
|
|
console.log("Loaded recaptcha script.");
|
|
|
|
try {
|
|
|
|
this._renderRecaptcha(DIV_ID);
|
|
|
|
} catch (e) {
|
|
|
|
this.setState({
|
|
|
|
errorText: e.toString(),
|
2017-01-20 17:22:27 +03:00
|
|
|
});
|
2016-10-11 20:00:47 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-20 13:14:00 +03:00
|
|
|
render: function() {
|
2016-10-11 20:00:47 +03:00
|
|
|
let error = null;
|
|
|
|
if (this.state.errorText) {
|
|
|
|
error = (
|
|
|
|
<div className="error">
|
2017-10-11 19:56:17 +03:00
|
|
|
{ this.state.errorText }
|
2016-10-11 20:00:47 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:14:00 +03:00
|
|
|
return (
|
|
|
|
<div ref="recaptchaContainer">
|
2017-10-11 19:56:17 +03:00
|
|
|
{ _t("This Home Server would like to make sure you are not a robot") }
|
|
|
|
<br />
|
2015-11-20 13:14:00 +03:00
|
|
|
<div id={DIV_ID}></div>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ error }
|
2015-11-20 13:14:00 +03:00
|
|
|
</div>
|
|
|
|
);
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-10-11 20:00:47 +03:00
|
|
|
});
|