Files
upstream-rocketchat/apps/meteor/client/views/root/hooks/useIframeCommands.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

138 lines
4.6 KiB
TypeScript

import type { UserStatus } from '@rocket.chat/core-typings';
import { escapeRegExp } from '@rocket.chat/tools';
import { type LocationPathname, UserContext, useLoginWithCustomOauth, useLoginWithToken, useSetting } from '@rocket.chat/ui-contexts';
import { useContext, useEffect } from 'react';
import { capitalize, ltrim, rtrim } from '../../../../lib/utils/stringUtils';
import { AccountBox } from '../../../lib/AccountBox';
import { baseURI } from '../../../lib/baseURI';
import { loginServices } from '../../../lib/loginServices';
import { getRootUrlPathPrefix } from '../../../lib/meteorRuntimeConfig';
import { settings } from '../../../lib/settings';
import { router } from '../../../providers/RouterProvider';
export const useIframeCommands = () => {
const iframeReceiveEnabled = useSetting('Iframe_Integration_receive_enable');
const iframeReceiveOrigin = useSetting('Iframe_Integration_receive_origin', '*');
const loginWithToken = useLoginWithToken();
const loginWithCustomOauth = useLoginWithCustomOauth();
const { logout } = useContext(UserContext);
const enableModernOAuthFlow = useSetting('Accounts_OAuth_Use_Modern_Flow', true);
useEffect(() => {
if (!iframeReceiveEnabled) {
return;
}
const commands = {
'go'(data: { path: string }) {
if (typeof data.path !== 'string' || data.path.trim().length === 0) {
return console.error('`path` not defined');
}
const newUrl = new URL(`${rtrim(baseURI, '/')}/${ltrim(data.path, '/')}`);
const newParams = Array.from(newUrl.searchParams.entries()).reduce(
(ret, [key, value]) => {
ret[key] = value;
return ret;
},
{} as Record<string, string>,
);
const newPath = newUrl.pathname.replace(new RegExp(`^${escapeRegExp(getRootUrlPathPrefix())}`), '') as LocationPathname;
router.navigate({
pathname: newPath,
search: { ...router.getSearchParameters(), ...newParams },
});
},
'set-user-status'(data: { status: UserStatus }) {
AccountBox.setStatus(data.status);
},
'call-custom-oauth-login'(data: { service: string; redirectUrl?: string | null }, event: MessageEvent) {
if (enableModernOAuthFlow) {
const url = new URL(window.location.href);
const queryParams = url.searchParams;
const loginClient = queryParams.get('loginClient');
const redirectUrl = new URL(`/oauth/${data.service}`, window.location.origin);
if (loginClient) {
redirectUrl.searchParams.set('loginClient', loginClient);
}
window.location.href = redirectUrl.toString();
return;
}
const customOAuthCallback = (response: unknown) => {
event.source?.postMessage(
{
event: 'custom-oauth-callback',
response,
},
{ targetOrigin: event.origin },
);
};
const siteUrl = `${settings.peek('Site_Url') ?? ''}/`;
if (typeof data.redirectUrl !== 'string' || !data.redirectUrl.startsWith(siteUrl)) {
data.redirectUrl = null;
}
if (window.ServiceConfiguration) {
const customOauth = loginServices.getLoginService(data.service);
if (customOauth) {
const customRedirectUri = data.redirectUrl || siteUrl;
loginWithCustomOauth(capitalize(customOauth.service, true), { redirectUrl: customRedirectUri }, customOAuthCallback);
}
}
},
'login-with-token'(data: { token: string }) {
if (typeof data.token === 'string') {
void loginWithToken(data.token, () => {
console.log('Iframe command [login-with-token]: result', data);
});
}
},
'logout'() {
void logout();
router.navigate('/home');
},
} as const;
type CommandMessage<TCommandName extends keyof typeof commands = keyof typeof commands> = {
externalCommand: TCommandName;
} & Parameters<(typeof commands)[TCommandName]>[0];
const messageListener = (event: MessageEvent<CommandMessage>) => {
if (typeof event.data !== 'object' || typeof event.data.externalCommand !== 'string') {
return;
}
if (iframeReceiveOrigin !== '*' && iframeReceiveOrigin.split(',').indexOf(event.origin) === -1) {
console.error('Origin not allowed', event.origin);
return;
}
if (!(event.data.externalCommand in commands)) {
console.error('Command not allowed', event.data.externalCommand);
return;
}
const command: (data: MessageEvent['data'], event: MessageEvent) => void = commands[event.data.externalCommand];
command(event.data, event);
};
window.addEventListener('message', messageListener);
return () => {
window.removeEventListener('message', messageListener);
};
}, [iframeReceiveEnabled, iframeReceiveOrigin, loginWithToken, loginWithCustomOauth, logout, enableModernOAuthFlow]);
};