vn-verdnaturachat/app/views/AuthenticationWebView.js

138 lines
4.0 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { WebView } from 'react-native-webview';
import { connect } from 'react-redux';
import parse from 'url-parse';
2019-12-04 16:39:53 +00:00
import RocketChat from '../lib/rocketchat';
2019-03-12 16:23:06 +00:00
import { isIOS } from '../utils/deviceInfo';
import { CloseModalButton } from '../containers/HeaderButton';
import StatusBar from '../containers/StatusBar';
2019-12-04 16:39:53 +00:00
import ActivityIndicator from '../containers/ActivityIndicator';
import { withTheme } from '../theme';
import { themedHeader } from '../utils/navigation';
import log from '../utils/log';
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';
class AuthenticationWebView extends React.PureComponent {
2019-12-04 16:39:53 +00:00
static navigationOptions = ({ navigation, screenProps }) => {
const authType = navigation.getParam('authType', 'oauth');
return {
2019-12-04 16:39:53 +00:00
...themedHeader(screenProps.theme),
headerLeft: <CloseModalButton navigation={navigation} />,
2019-08-12 18:37:42 +00:00
title: authType === 'saml' || authType === 'cas' ? 'SSO' : 'OAuth'
};
}
static propTypes = {
2019-03-12 16:23:06 +00:00
navigation: PropTypes.object,
2019-12-04 16:39:53 +00:00
server: PropTypes.string,
theme: PropTypes.string
}
constructor(props) {
super(props);
2018-12-05 20:52:08 +00:00
this.state = {
logging: false,
loading: false
2018-12-05 20:52:08 +00:00
};
this.authType = props.navigation.getParam('authType', 'oauth');
this.redirectRegex = new RegExp(`(?=.*(${ props.server }))(?=.*(credentialToken))(?=.*(credentialSecret))`, 'g');
}
dismiss = () => {
2019-03-12 16:23:06 +00:00
const { navigation } = this.props;
navigation.pop();
}
login = async(params) => {
2018-12-05 20:52:08 +00:00
const { logging } = this.state;
if (logging) {
return;
}
this.setState({ logging: true });
try {
await RocketChat.loginOAuthOrSso(params);
} catch (e) {
console.warn(e);
}
2018-12-05 20:52:08 +00:00
this.setState({ logging: false });
this.dismiss();
}
onNavigationStateChange = (webViewState) => {
try {
const url = decodeURIComponent(webViewState.url);
if (this.authType === 'cas') {
const { navigation } = this.props;
const ssoToken = navigation.getParam('ssoToken');
if (url.includes('ticket') || url.includes('validate')) {
2020-01-13 12:23:14 +00:00
const payload = { cas: { credentialToken: ssoToken } };
// We need to set a timeout when the login is done with SSO in order to make it work on our side.
// It is actually due to the SSO server processing the response.
setTimeout(() => {
this.login(payload);
}, 3000);
}
}
if (this.authType === 'saml') {
const parsedUrl = parse(url, true);
if (parsedUrl.query && parsedUrl.query.saml_idp_credentialToken) {
const payload = { credentialToken: parsedUrl.query.saml_idp_credentialToken, saml: true };
// We need to set a timeout when the login is done with SSO in order to make it work on our side.
// It is actually due to the SSO server processing the response.
setTimeout(() => {
this.login(payload);
}, 3000);
2019-08-12 18:37:42 +00:00
}
}
if (this.authType === 'oauth') {
if (this.redirectRegex.test(url)) {
const parts = url.split('#');
const credentials = JSON.parse(parts[1]);
this.login({ oauth: { ...credentials } });
}
}
} catch (e) {
log(e);
}
}
render() {
const { loading } = this.state;
2019-12-04 16:39:53 +00:00
const { navigation, theme } = this.props;
const uri = navigation.getParam('url');
return (
<>
2019-12-04 16:39:53 +00:00
<StatusBar theme={theme} />
2019-03-12 16:23:06 +00:00
<WebView
source={{ uri }}
2019-03-12 16:23:06 +00:00
userAgent={userAgent}
onNavigationStateChange={this.onNavigationStateChange}
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 }
</>
);
}
}
const mapStateToProps = state => ({
server: state.server.server
});
2019-12-04 16:39:53 +00:00
export default connect(mapStateToProps)(withTheme(AuthenticationWebView));