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
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
109 lines
3.0 KiB
TypeScript
109 lines
3.0 KiB
TypeScript
import crypto from 'crypto';
|
|
|
|
import type { IUser } from '@rocket.chat/core-typings';
|
|
import { Logger } from '@rocket.chat/logger';
|
|
import { LoginCodes } from '@rocket.chat/models';
|
|
import type { Request, Response } from 'express';
|
|
import { Accounts } from 'meteor/accounts-base';
|
|
|
|
import { doesUserRequire2FA } from './twoFactorAuth';
|
|
|
|
const logger = new Logger('OAuth');
|
|
|
|
type LaunchStyle = 'popup' | 'redirect' | undefined;
|
|
|
|
const createLoginToken = async (userId: string) => {
|
|
const stampedToken = Accounts._generateStampedLoginToken();
|
|
await Accounts._insertLoginToken(userId, stampedToken);
|
|
|
|
return stampedToken.token;
|
|
};
|
|
|
|
const sendPopupMessage = (res: Response, payload: unknown) => {
|
|
const nonce = crypto.randomBytes(16).toString('base64');
|
|
res.setHeader('Content-Security-Policy', `script-src 'nonce-${nonce}'`);
|
|
|
|
res.send(`
|
|
<script nonce="${nonce}">
|
|
window.opener.postMessage(
|
|
${JSON.stringify(payload)},
|
|
window.location.origin
|
|
);
|
|
window.close();
|
|
</script>
|
|
`);
|
|
};
|
|
|
|
const handlePopupStyleOAuth = async (res: Response, oAuthUser: IUser, loginClient?: string) => {
|
|
const secondFactorMethod = doesUserRequire2FA(oAuthUser);
|
|
|
|
if (secondFactorMethod) {
|
|
const challengeId = await secondFactorMethod.sendTwoFactorChallenge(oAuthUser);
|
|
sendPopupMessage(res, {
|
|
oauthPopupCommand: 'redirect',
|
|
path: `/2fa/${secondFactorMethod.method}/${challengeId}${loginClient ? `?loginClient=${loginClient}` : ''}`,
|
|
});
|
|
return;
|
|
}
|
|
|
|
const token = await createLoginToken(oAuthUser._id);
|
|
|
|
if (loginClient) {
|
|
sendPopupMessage(res, {
|
|
oauthPopupCommand: 'redirect',
|
|
path: `home?resumeToken=${token}&userId=${oAuthUser._id}&loginClient=${loginClient}`,
|
|
});
|
|
return;
|
|
}
|
|
|
|
sendPopupMessage(res, { oauthPopupCommand: 'oauth-login-with-token', token });
|
|
};
|
|
|
|
export const passportOAuthCallback =
|
|
(siteUrl: string, launchStyle: LaunchStyle = 'redirect') =>
|
|
async (req: Request, res: Response) => {
|
|
const oAuthUser = req.user as IUser;
|
|
|
|
if (!oAuthUser) {
|
|
return res.redirect('/login');
|
|
}
|
|
|
|
const { loginClient } = req.session;
|
|
|
|
if (launchStyle === 'popup') {
|
|
await handlePopupStyleOAuth(res, oAuthUser, loginClient);
|
|
return;
|
|
}
|
|
|
|
const secondFactorMethod = doesUserRequire2FA(oAuthUser);
|
|
|
|
if (secondFactorMethod) {
|
|
const challengeId = await secondFactorMethod.sendTwoFactorChallenge(oAuthUser);
|
|
const twoFARedirectUrl = new URL(`/2fa/${secondFactorMethod.method}/${challengeId}`, siteUrl);
|
|
|
|
if (loginClient) {
|
|
twoFARedirectUrl.searchParams.set('loginClient', loginClient);
|
|
}
|
|
|
|
return res.redirect(twoFARedirectUrl.toString());
|
|
}
|
|
|
|
const loginCode = await LoginCodes.createCode(oAuthUser._id);
|
|
|
|
const redirectUrl = new URL(`/home`, siteUrl);
|
|
|
|
redirectUrl.searchParams.set('loginCode', loginCode);
|
|
|
|
if (loginClient) {
|
|
redirectUrl.searchParams.set('loginClient', loginClient);
|
|
}
|
|
|
|
setImmediate(() => res.redirect(redirectUrl.toString()));
|
|
|
|
req.session.destroy((err) => {
|
|
if (err) {
|
|
logger.error({ msg: 'Error destroying OAuth session', err });
|
|
}
|
|
});
|
|
};
|