2023-10-18 17:13:42 +00:00
|
|
|
import { RouteProp } from '@react-navigation/core';
|
|
|
|
import { useNavigation, useRoute } from '@react-navigation/native';
|
2021-10-20 18:00:35 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
2023-10-18 17:13:42 +00:00
|
|
|
import React, { useLayoutEffect, useState } from 'react';
|
|
|
|
import { WebView, WebViewNavigation } from 'react-native-webview';
|
2021-10-20 18:00:35 +00:00
|
|
|
import { WebViewMessage } from 'react-native-webview/lib/WebViewTypes';
|
2023-10-18 17:13:42 +00:00
|
|
|
import parse from 'url-parse';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
import ActivityIndicator from '../containers/ActivityIndicator';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2023-10-18 17:13:42 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import { ICredentials } from '../definitions';
|
|
|
|
import { userAgent } from '../lib/constants';
|
|
|
|
import { useAppSelector } from '../lib/hooks';
|
|
|
|
import { useDebounce } from '../lib/methods/helpers';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2023-10-18 17:13:42 +00:00
|
|
|
import { OutsideModalParamList } from '../stacks/types';
|
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);
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
const AuthenticationWebView = () => {
|
|
|
|
const [logging, setLogging] = useState(false);
|
|
|
|
const [loading, setLoading] = useState(false);
|
2021-10-20 18:00:35 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
const navigation = useNavigation<StackNavigationProp<OutsideModalParamList, 'AuthenticationWebView'>>();
|
|
|
|
const {
|
|
|
|
params: { authType, url, ssoToken }
|
|
|
|
} = useRoute<RouteProp<OutsideModalParamList, 'AuthenticationWebView'>>();
|
2021-10-20 18:00:35 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
const { Accounts_Iframe_api_method, Accounts_Iframe_api_url, server } = useAppSelector(state => ({
|
|
|
|
server: state.server.server,
|
|
|
|
Accounts_Iframe_api_url: state.settings.Accounts_Iframe_api_url as string,
|
|
|
|
Accounts_Iframe_api_method: state.settings.Accounts_Iframe_api_method as string
|
|
|
|
}));
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
const oauthRedirectRegex = new RegExp(`(?=.*(${server}))(?=.*(credentialToken))(?=.*(credentialSecret))`, 'g');
|
|
|
|
const iframeRedirectRegex = new RegExp(`(?=.*(${server}))(?=.*(event|loginToken|token))`, 'g');
|
2020-01-28 13:21:50 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
// Force 3s delay so the server has time to evaluate the token
|
|
|
|
const debouncedLogin = useDebounce((params: ICredentials) => login(params), 3000);
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
const login = (params: ICredentials) => {
|
2018-12-05 20:52:08 +00:00
|
|
|
if (logging) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-18 17:13:42 +00:00
|
|
|
setLogging(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);
|
|
|
|
}
|
2023-10-18 17:13:42 +00:00
|
|
|
setLogging(false);
|
|
|
|
navigation.pop();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
const tryLogin = useDebounce(
|
2021-09-13 20:41:05 +00:00
|
|
|
async () => {
|
|
|
|
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) {
|
2023-10-18 17:13:42 +00:00
|
|
|
login({ resume });
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
3000,
|
2023-10-18 17:13:42 +00:00
|
|
|
{ leading: true }
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
2020-06-17 20:12:21 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
const 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
|
|
|
if (authType === 'saml' || authType === 'cas') {
|
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
|
|
|
}
|
2023-10-18 17:13:42 +00:00
|
|
|
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') {
|
2023-10-18 17:13:42 +00:00
|
|
|
if (oauthRedirectRegex.test(url)) {
|
2020-01-28 13:21:50 +00:00
|
|
|
const parts = url.split('#');
|
|
|
|
const credentials = JSON.parse(parts[1]);
|
2023-10-18 17:13:42 +00:00
|
|
|
debouncedLogin({ oauth: { ...credentials } });
|
2019-08-09 17:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-17 20:12:21 +00:00
|
|
|
|
|
|
|
if (authType === 'iframe') {
|
2023-10-18 17:13:42 +00:00
|
|
|
if (iframeRedirectRegex.test(url)) {
|
2020-06-17 20:12:21 +00:00
|
|
|
const parts = url.split('#');
|
|
|
|
const credentials = JSON.parse(parts[1]);
|
|
|
|
switch (credentials.event) {
|
|
|
|
case 'try-iframe-login':
|
2023-10-18 17:13:42 +00:00
|
|
|
tryLogin();
|
2020-06-17 20:12:21 +00:00
|
|
|
break;
|
|
|
|
case 'login-with-token':
|
2023-10-18 17:13:42 +00:00
|
|
|
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
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
const isIframe = authType === 'iframe';
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
headerLeft: () => <HeaderButton.CloseModal />,
|
|
|
|
title: ['saml', 'cas', 'iframe'].includes(authType) ? 'SSO' : 'OAuth'
|
|
|
|
});
|
|
|
|
}, [authType, navigation]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<StatusBar />
|
|
|
|
<WebView
|
|
|
|
source={{ uri: url }}
|
|
|
|
userAgent={userAgent}
|
|
|
|
// https://github.com/react-native-community/react-native-webview/issues/24#issuecomment-540130141
|
|
|
|
onMessage={({ nativeEvent }) => onNavigationStateChange(nativeEvent)}
|
|
|
|
onNavigationStateChange={onNavigationStateChange}
|
|
|
|
injectedJavaScript={isIframe ? injectedJavaScript : undefined}
|
|
|
|
onLoadStart={() => setLoading(true)}
|
|
|
|
onLoadEnd={() => setLoading(false)}
|
|
|
|
/>
|
|
|
|
{loading ? <ActivityIndicator size='large' absolute /> : null}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2023-10-18 17:13:42 +00:00
|
|
|
export default AuthenticationWebView;
|