2018-07-10 13:40:32 +00:00
|
|
|
import React from 'react';
|
2021-10-20 18:00:35 +00:00
|
|
|
import { WebView, WebViewNavigation } 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';
|
2021-10-20 18:00:35 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { WebViewMessage } from 'react-native-webview/lib/WebViewTypes';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { RouteProp } from '@react-navigation/core';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2021-12-03 19:27:57 +00:00
|
|
|
import { OutsideModalParamList } from '../stacks/types';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../containers/ActivityIndicator';
|
2023-08-18 17:48:33 +00:00
|
|
|
import { TSupportedThemes, withTheme } from '../theme';
|
2023-05-08 13:45:46 +00:00
|
|
|
import { userAgent } from '../lib/constants';
|
|
|
|
import { debounce } from '../lib/methods/helpers';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2022-05-11 16:59:57 +00:00
|
|
|
import { IApplicationState, ICredentials } from '../definitions';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2020-06-17 20:12:21 +00:00
|
|
|
// iframe uses a postMessage to send the token to the client
|
|
|
|
// We'll handle this sending the token to the hash of the window.location
|
|
|
|
// https://docs.rocket.chat/guides/developer-guides/iframe-integration/authentication#iframe-url
|
|
|
|
// https://github.com/react-native-community/react-native-webview/issues/24#issuecomment-540130141
|
|
|
|
const injectedJavaScript = `
|
|
|
|
window.addEventListener('message', ({ data }) => {
|
|
|
|
if (typeof data === 'object') {
|
|
|
|
window.location.hash = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
function wrap(fn) {
|
|
|
|
return function wrapper() {
|
|
|
|
var res = fn.apply(this, arguments);
|
|
|
|
window.ReactNativeWebView.postMessage(window.location.href);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
history.pushState = wrap(history.pushState);
|
|
|
|
history.replaceState = wrap(history.replaceState);
|
|
|
|
window.addEventListener('popstate', function() {
|
|
|
|
window.ReactNativeWebView.postMessage(window.location.href);
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2021-10-20 18:00:35 +00:00
|
|
|
interface INavigationOption {
|
2021-12-03 19:27:57 +00:00
|
|
|
navigation: StackNavigationProp<OutsideModalParamList, 'AuthenticationWebView'>;
|
|
|
|
route: RouteProp<OutsideModalParamList, 'AuthenticationWebView'>;
|
2021-10-20 18:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IAuthenticationWebView extends INavigationOption {
|
|
|
|
server: string;
|
|
|
|
Accounts_Iframe_api_url: string;
|
|
|
|
Accounts_Iframe_api_method: string;
|
2023-08-18 17:48:33 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-10-20 18:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
logging: boolean;
|
|
|
|
loading: boolean;
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2021-10-20 18:00:35 +00:00
|
|
|
class AuthenticationWebView extends React.PureComponent<IAuthenticationWebView, IState> {
|
|
|
|
private oauthRedirectRegex: RegExp;
|
|
|
|
private iframeRedirectRegex: RegExp;
|
|
|
|
|
|
|
|
static navigationOptions = ({ route, navigation }: INavigationOption) => {
|
|
|
|
const { authType } = route.params;
|
|
|
|
return {
|
|
|
|
headerLeft: () => <HeaderButton.CloseModal navigation={navigation} />,
|
|
|
|
title: ['saml', 'cas', 'iframe'].includes(authType) ? 'SSO' : 'OAuth'
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: IAuthenticationWebView) {
|
2018-07-10 13:40:32 +00:00
|
|
|
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
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
this.oauthRedirectRegex = new RegExp(`(?=.*(${props.server}))(?=.*(credentialToken))(?=.*(credentialSecret))`, 'g');
|
|
|
|
this.iframeRedirectRegex = new RegExp(`(?=.*(${props.server}))(?=.*(event|loginToken|token))`, 'g');
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2022-05-11 16:59:57 +00:00
|
|
|
login = (params: ICredentials) => {
|
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 {
|
2022-04-28 20:37:25 +00:00
|
|
|
Services.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();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2021-04-14 14:24:13 +00:00
|
|
|
// Force 3s delay so the server has time to evaluate the token
|
2022-05-11 16:59:57 +00:00
|
|
|
debouncedLogin = debounce((params: ICredentials) => this.login(params), 3000);
|
2020-01-07 17:44:34 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
tryLogin = debounce(
|
|
|
|
async () => {
|
|
|
|
const { Accounts_Iframe_api_url, Accounts_Iframe_api_method } = this.props;
|
|
|
|
const data = await fetch(Accounts_Iframe_api_url, { method: Accounts_Iframe_api_method }).then(response => response.json());
|
|
|
|
const resume = data?.login || data?.loginToken;
|
|
|
|
if (resume) {
|
|
|
|
this.login({ resume });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
3000,
|
|
|
|
true
|
|
|
|
);
|
2020-06-17 20:12:21 +00:00
|
|
|
|
2021-10-20 18:00:35 +00:00
|
|
|
onNavigationStateChange = (webViewState: WebViewNavigation | WebViewMessage) => {
|
2020-01-28 13:21:50 +00:00
|
|
|
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) {
|
2022-05-11 16:59:57 +00:00
|
|
|
let payload: ICredentials;
|
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-06-17 20:12:21 +00:00
|
|
|
if (this.oauthRedirectRegex.test(url)) {
|
2020-01-28 13:21:50 +00:00
|
|
|
const parts = url.split('#');
|
|
|
|
const credentials = JSON.parse(parts[1]);
|
2021-04-01 13:29:21 +00:00
|
|
|
this.debouncedLogin({ oauth: { ...credentials } });
|
2019-08-09 17:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-17 20:12:21 +00:00
|
|
|
|
|
|
|
if (authType === 'iframe') {
|
|
|
|
if (this.iframeRedirectRegex.test(url)) {
|
|
|
|
const parts = url.split('#');
|
|
|
|
const credentials = JSON.parse(parts[1]);
|
|
|
|
switch (credentials.event) {
|
|
|
|
case 'try-iframe-login':
|
|
|
|
this.tryLogin();
|
|
|
|
break;
|
|
|
|
case 'login-with-token':
|
2021-04-01 13:29:21 +00:00
|
|
|
this.debouncedLogin({ resume: credentials.token || credentials.loginToken });
|
2020-06-17 20:12:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-09-13 20:41:05 +00:00
|
|
|
// Do nothing
|
2020-06-17 20:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
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;
|
2022-03-18 02:37:10 +00:00
|
|
|
const { route } = this.props;
|
2020-06-17 20:12:21 +00:00
|
|
|
const { url, authType } = route.params;
|
|
|
|
const isIframe = authType === 'iframe';
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
return (
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2020-10-30 13:59:44 +00:00
|
|
|
<StatusBar />
|
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}
|
2020-06-17 20:12:21 +00:00
|
|
|
// https://github.com/react-native-community/react-native-webview/issues/24#issuecomment-540130141
|
|
|
|
onMessage={({ nativeEvent }) => this.onNavigationStateChange(nativeEvent)}
|
2019-08-09 17:28:46 +00:00
|
|
|
onNavigationStateChange={this.onNavigationStateChange}
|
2020-06-17 20:12:21 +00:00
|
|
|
injectedJavaScript={isIframe ? injectedJavaScript : undefined}
|
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
|
|
|
/>
|
2022-03-18 02:37:10 +00:00
|
|
|
{loading ? <ActivityIndicator size='large' 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
|
|
|
|
2022-05-11 16:59:57 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2023-08-18 17:48:33 +00:00
|
|
|
server: state.server.server,
|
2022-05-11 16:59:57 +00:00
|
|
|
Accounts_Iframe_api_url: state.settings.Accounts_Iframe_api_url as string,
|
|
|
|
Accounts_Iframe_api_method: state.settings.Accounts_Iframe_api_method as string
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2023-08-18 17:48:33 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(AuthenticationWebView));
|