Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions apps/meteor/app/gitlab/server/lib.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { OauthConfig } from '@rocket.chat/core-typings';
import type { OAuthConfiguration } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';
import passport from 'passport';
import _ from 'underscore';

import { CustomOAuth } from '../../custom-oauth/server/custom_oauth_server';
import { addPassportCustomOAuth } from '../../../server/lib/oauth/addPassportCustomOAuth';
import { settings } from '../../settings/server';

const config: OauthConfig = {
const config: Partial<OAuthConfiguration> = {
serverURL: 'https://gitlab.com',
identityPath: '/api/v4/user',
scope: 'read_user',
Expand All @@ -17,15 +18,39 @@ const config: OauthConfig = {
accessTokenParam: 'access_token',
};

const Gitlab = new CustomOAuth('gitlab', config);
const configureGitlabOAuth = () => {
passport.unuse('gitlab');

const enabled = settings.get<boolean>('Accounts_OAuth_Gitlab');
if (!enabled) {
return;
}

const clientId = settings.get<string>('Accounts_OAuth_Gitlab_id');
const clientSecret = settings.get<string>('Accounts_OAuth_Gitlab_secret');
const serverURL = settings.get<string>('API_Gitlab_URL').trim().replace(/\/*$/, '') || config.serverURL;
Comment thread
yash-rajpal marked this conversation as resolved.
const identityPath = settings.get<string>('Accounts_OAuth_Gitlab_identity_path') || config.identityPath;
const mergeUsers = Boolean(settings.get<boolean>('Accounts_OAuth_Gitlab_merge_users'));

if (!clientId || !clientSecret) {
return;
}

addPassportCustomOAuth('gitlab', { ...config, clientId, clientSecret, serverURL, identityPath, mergeUsers });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the settings are changed, the addPassportCustomOAuth re adds the endpoint routes. Will it have impact on memory (duplications) or are the routes just overwritten?

Rest everything LGTM.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was checking this, I assumed it will overwrite but sadly no, it adds middlewares to the chain.

For our implementation it shouldn't create any impact as we send a response, so it never goes to stacked middleware.

I'll open a task to improve this, for now it should increase a little memory consumption if routes are over-written many times.

};

Meteor.startup(() => {
const updateConfig = _.debounce(() => {
config.serverURL = settings.get<string>('API_Gitlab_URL').trim().replace(/\/*$/, '') || config.serverURL;
config.identityPath = settings.get('Accounts_OAuth_Gitlab_identity_path') || config.identityPath;
config.mergeUsers = Boolean(settings.get<boolean>('Accounts_OAuth_Gitlab_merge_users'));
Gitlab.configure(config);
}, 300);

settings.watchMultiple(['API_Gitlab_URL', 'Accounts_OAuth_Gitlab_identity_path', 'Accounts_OAuth_Gitlab_merge_users'], updateConfig);
const updateConfig = _.debounce(configureGitlabOAuth, 300);

settings.watchMultiple(
[
'Accounts_OAuth_Gitlab',
'API_Gitlab_URL',
'Accounts_OAuth_Gitlab_id',
'Accounts_OAuth_Gitlab_secret',
'Accounts_OAuth_Gitlab_identity_path',
'Accounts_OAuth_Gitlab_merge_users',
],
updateConfig,
);
});
Loading