2018-07-10 13:40:32 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-05-18 19:31:33 +00:00
|
|
|
import { WebView } from 'react-native-webview';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2020-01-07 17:44:34 +00:00
|
|
|
import parse from 'url-parse';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { isIOS } from '../utils/deviceInfo';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../containers/ActivityIndicator';
|
|
|
|
import { withTheme } from '../theme';
|
2020-01-28 13:21:50 +00:00
|
|
|
import debounce from '../utils/debounce';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { CloseModalButton } from '../containers/HeaderButton';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
const userAgent = isIOS
|
|
|
|
? 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
|
|
|
|
: 'Mozilla/5.0 (Linux; Android 6.0.1; SM-G920V Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2019-08-09 17:28:46 +00:00
|
|
|
class AuthenticationWebView extends React.PureComponent {
|
2018-07-10 13:40:32 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2019-12-04 16:39:53 +00:00
|
|
|
server: PropTypes.string,
|
|
|
|
theme: PropTypes.string
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-12-05 20:52:08 +00:00
|
|
|
this.state = {
|
2019-06-20 19:02:16 +00:00
|
|
|
logging: false,
|
|
|
|
loading: false
|
2018-12-05 20:52:08 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
this.redirectRegex = new RegExp(`(?=.*(${ props.server }))(?=.*(credentialToken))(?=.*(credentialSecret))`, 'g');
|
|
|
|
}
|
|
|
|
|
2020-01-28 13:21:50 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.debouncedLogin && this.debouncedLogin.stop) {
|
|
|
|
this.debouncedLogin.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
dismiss = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.pop();
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
login = async(params) => {
|
2018-12-05 20:52:08 +00:00
|
|
|
const { logging } = this.state;
|
|
|
|
if (logging) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ logging: true });
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
try {
|
2019-08-09 17:28:46 +00:00
|
|
|
await RocketChat.loginOAuthOrSso(params);
|
2018-07-10 13:40:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
this.setState({ logging: false });
|
|
|
|
this.dismiss();
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 13:21:50 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
debouncedLogin = debounce(params => this.login(params), 3000);
|
2020-01-07 17:44:34 +00:00
|
|
|
|
2020-01-28 13:21:50 +00:00
|
|
|
onNavigationStateChange = (webViewState) => {
|
|
|
|
const url = decodeURIComponent(webViewState.url);
|
2020-06-15 14:00:46 +00:00
|
|
|
const { route } = this.props;
|
|
|
|
const { authType } = route.params;
|
|
|
|
if (authType === 'saml' || authType === 'cas') {
|
|
|
|
const { ssoToken } = route.params;
|
2020-04-06 19:15:13 +00:00
|
|
|
const parsedUrl = parse(url, true);
|
2020-04-13 13:11:51 +00:00
|
|
|
// ticket -> cas / validate & saml_idp_credentialToken -> saml
|
|
|
|
if (parsedUrl.pathname?.includes('validate') || parsedUrl.query?.ticket || parsedUrl.query?.saml_idp_credentialToken) {
|
2020-01-28 13:21:50 +00:00
|
|
|
let payload;
|
2020-06-15 14:00:46 +00:00
|
|
|
if (authType === 'saml') {
|
2020-04-06 19:15:13 +00:00
|
|
|
const token = parsedUrl.query?.saml_idp_credentialToken || ssoToken;
|
2020-01-28 13:21:50 +00:00
|
|
|
const credentialToken = { credentialToken: token };
|
|
|
|
payload = { ...credentialToken, saml: true };
|
|
|
|
} else {
|
|
|
|
payload = { cas: { credentialToken: ssoToken } };
|
2019-08-12 18:37:42 +00:00
|
|
|
}
|
2020-01-28 13:21:50 +00:00
|
|
|
this.debouncedLogin(payload);
|
2019-08-09 17:28:46 +00:00
|
|
|
}
|
2020-01-28 13:21:50 +00:00
|
|
|
}
|
2019-08-09 17:28:46 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
if (authType === 'oauth') {
|
2020-01-28 13:21:50 +00:00
|
|
|
if (this.redirectRegex.test(url)) {
|
|
|
|
const parts = url.split('#');
|
|
|
|
const credentials = JSON.parse(parts[1]);
|
|
|
|
this.login({ oauth: { ...credentials } });
|
2019-08-09 17:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
render() {
|
2019-06-20 19:02:16 +00:00
|
|
|
const { loading } = this.state;
|
2020-06-15 14:00:46 +00:00
|
|
|
const { route, theme } = this.props;
|
|
|
|
const { url } = route.params;
|
2018-07-10 13:40:32 +00:00
|
|
|
return (
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2019-12-04 16:39:53 +00:00
|
|
|
<StatusBar theme={theme} />
|
2019-03-12 16:23:06 +00:00
|
|
|
<WebView
|
2020-06-15 14:00:46 +00:00
|
|
|
source={{ uri: url }}
|
2019-03-12 16:23:06 +00:00
|
|
|
userAgent={userAgent}
|
2019-08-09 17:28:46 +00:00
|
|
|
onNavigationStateChange={this.onNavigationStateChange}
|
2019-06-20 19:02:16 +00:00
|
|
|
onLoadStart={() => {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
}}
|
|
|
|
onLoadEnd={() => {
|
|
|
|
this.setState({ loading: false });
|
|
|
|
}}
|
2019-03-12 16:23:06 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
{ loading ? <ActivityIndicator size='large' theme={theme} absolute /> : null }
|
2019-09-24 20:26:56 +00:00
|
|
|
</>
|
2018-07-10 13:40:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
server: state.server.server
|
|
|
|
});
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
AuthenticationWebView.navigationOptions = ({ route, navigation }) => {
|
|
|
|
const { authType } = route.params;
|
|
|
|
return {
|
|
|
|
headerLeft: () => <CloseModalButton navigation={navigation} />,
|
|
|
|
title: authType === 'saml' || authType === 'cas' ? 'SSO' : 'OAuth'
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(AuthenticationWebView));
|