import type { OAuthConfiguration } from '@rocket.chat/core-typings'; import { Meteor } from 'meteor/meteor'; import passport from 'passport'; import _ from 'underscore'; import { addPassportCustomOAuth } from '../oauth/addPassportCustomOAuth'; import { CustomOAuth } from './custom-oauth/custom_oauth_server'; import { settings } from '../../settings'; const config: Partial = { serverURL: '', identityPath: '/oauth2/UserInfo', authorizePath: '/oauth2/authorize', tokenPath: '/oauth2/token', scope: 'openid email profile offline_access', tokenSentVia: 'payload', usernameField: 'preferred_username', mergeUsers: true, addAutopublishFields: { forLoggedInUser: ['services.drupal'], forOtherUsers: ['services.drupal.name'], }, accessTokenParam: 'access_token', }; const Drupal = new CustomOAuth('drupal', config); const configureDrupalOAuth = () => { passport.unuse('drupal'); const enabled = settings.get('Accounts_OAuth_Drupal'); if (!enabled) { return; } const serverURL = settings.get('API_Drupal_URL').trim().replace(/\/*$/, ''); const clientId = settings.get('Accounts_OAuth_Drupal_id'); const clientSecret = settings.get('Accounts_OAuth_Drupal_secret'); if (!clientId || !clientSecret || !serverURL) { return; } const completeConfig = { ...config, serverURL, clientId, clientSecret }; if (settings.get('Accounts_OAuth_Use_Modern_Flow')) { addPassportCustomOAuth('drupal', completeConfig); return; } Drupal.configure(completeConfig); }; Meteor.startup(() => { const updateConfig = _.debounce(configureDrupalOAuth, 300); settings.watchMultiple( [ 'Accounts_OAuth_Drupal', 'API_Drupal_URL', 'Accounts_OAuth_Drupal_id', 'Accounts_OAuth_Drupal_secret', 'Accounts_OAuth_Use_Modern_Flow', ], updateConfig, ); });