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
93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
import { Users } from '@rocket.chat/models';
|
|
import { Accounts } from 'meteor/accounts-base';
|
|
import passport from 'passport';
|
|
import type { Profile, DoneCallback } from 'passport';
|
|
|
|
import { allowPassportOAuthMiddleware } from './allowPassportOAuthMiddleware';
|
|
import type { OAuthServiceConfig } from './createOAuthServiceConfig';
|
|
import { passportOAuthCallback } from './passportOAuthCallback';
|
|
import { removeOAuthRoutes } from './removeOAuthRoutes';
|
|
import { oAuthRouter } from '../../configuration/configurePassport';
|
|
import type { ICachedSettings } from '../../settings/CachedSettings';
|
|
|
|
export const configureOAuthServices = (oauthServiceConfig: OAuthServiceConfig[], settings: ICachedSettings) => {
|
|
oauthServiceConfig.forEach((config) => {
|
|
const Strategy = config.strategy;
|
|
const siteUrl = settings.get<string>('Site_Url').replace(/\/$/, '');
|
|
|
|
passport.unuse(config.provider);
|
|
removeOAuthRoutes(config.provider);
|
|
|
|
passport.use(
|
|
config.provider,
|
|
new Strategy(
|
|
{
|
|
...config,
|
|
clientID: config.clientId,
|
|
clientSecret: config.clientSecret,
|
|
consumerKey: config.clientId,
|
|
consumerSecret: config.clientSecret,
|
|
callbackURL: `${siteUrl}/_oauth/${config.provider}`,
|
|
state: true,
|
|
pkce: true,
|
|
profileFields: ['id', 'displayName', 'emails'],
|
|
},
|
|
async (accessToken: string, refreshToken: string, profile: Profile, done: DoneCallback) => {
|
|
const profileWithRaw = profile as Profile & { _json?: Record<string, unknown>; _raw?: string };
|
|
const { _json, _raw, ...restProfile } = profileWithRaw;
|
|
|
|
const user = await Accounts.updateOrCreateUserFromExternalService(
|
|
config.provider,
|
|
{
|
|
accessToken,
|
|
refreshToken,
|
|
name: profile.displayName,
|
|
...restProfile,
|
|
..._json,
|
|
email: profile?.emails?.[0]?.value,
|
|
},
|
|
{},
|
|
);
|
|
|
|
if (!user?.userId || typeof user?.userId !== 'string') {
|
|
return done(new Error('User not found'));
|
|
}
|
|
|
|
const userFromDB = await Users.findOneById(user.userId);
|
|
|
|
if (!userFromDB) {
|
|
return done(new Error('User not found'));
|
|
}
|
|
|
|
return done(null, userFromDB);
|
|
},
|
|
),
|
|
);
|
|
|
|
oAuthRouter.get(
|
|
`/oauth/${config.provider}`,
|
|
allowPassportOAuthMiddleware(config.provider),
|
|
(req, _res, next) => {
|
|
const { loginClient } = req.query;
|
|
if (loginClient === 'mobile' || loginClient === 'desktop') {
|
|
req.session.loginClient = loginClient;
|
|
req.session.save(() => {
|
|
next();
|
|
});
|
|
} else {
|
|
//delete stale value from previous sessions if any
|
|
delete req.session.loginClient;
|
|
next();
|
|
}
|
|
},
|
|
passport.authenticate(config.provider, { scope: config.scope, prompt: 'consent', failureRedirect: '/login', keepSessionInfo: true }),
|
|
);
|
|
oAuthRouter.get(
|
|
`/_oauth/${config.provider}`,
|
|
allowPassportOAuthMiddleware(config.provider),
|
|
passport.authenticate(config.provider, { failureRedirect: '/login', failWithError: true, keepSessionInfo: true }),
|
|
passportOAuthCallback(siteUrl),
|
|
);
|
|
});
|
|
};
|