diff --git a/apps/meteor/app/custom-oauth/server/customOAuth.ts b/apps/meteor/app/custom-oauth/server/customOAuth.ts index 82d774ff94112..e9e5ed034fcf0 100644 --- a/apps/meteor/app/custom-oauth/server/customOAuth.ts +++ b/apps/meteor/app/custom-oauth/server/customOAuth.ts @@ -236,6 +236,8 @@ export class CustomOAuthStrategy extends Strategy { try { const result = JSON.parse(typeof body === 'string' ? body : body.toString()); const normalizedIdentity = this.normalizeIdentity(result); + //Nextcloud URL needed on addWebdavServer + normalizedIdentity.serverURL = this.serverURL; return done(null, normalizedIdentity); } catch (e) { return done(new Error(`Failed to parse identity from ${this.name} at ${this.identityPath}. ${e}`)); diff --git a/apps/meteor/app/nextcloud/server/lib.ts b/apps/meteor/app/nextcloud/server/lib.ts index 28cf52da57a17..fc2b592b7c1ad 100644 --- a/apps/meteor/app/nextcloud/server/lib.ts +++ b/apps/meteor/app/nextcloud/server/lib.ts @@ -1,14 +1,12 @@ -import type { OauthConfig } from '@rocket.chat/core-typings'; +import type { OAuthConfiguration } from '@rocket.chat/core-typings'; import { Meteor } from 'meteor/meteor'; -import _ from 'underscore'; -import { CustomOAuth } from '../../custom-oauth/server/custom_oauth_server'; -import { settings } from '../../settings/server'; +import { addPassportCustomOAuth } from '../../../server/lib/oauth/addPassportCustomOAuth'; +import { settings } from '../../settings/server/cached'; -const config: OauthConfig = { - serverURL: '', +const NEXTCLOUD_PATHS = { tokenPath: '/index.php/apps/oauth2/api/v1/token', - tokenSentVia: 'header', + tokenSentVia: 'header' as OAuthConfiguration['tokenSentVia'], authorizePath: '/index.php/apps/oauth2/authorize', identityPath: '/ocs/v2.php/cloud/user?format=json', scope: 'openid', @@ -18,20 +16,31 @@ const config: OauthConfig = { }, }; -const Nextcloud = new CustomOAuth('nextcloud', config); +function configureNextcloudOAuth(): void { + const enabled = settings.get('Accounts_OAuth_Nextcloud'); + if (!enabled) { + return; + } -const fillServerURL = _.debounce((): void => { - const nextcloudURL = settings.get('Accounts_OAuth_Nextcloud_URL'); - if (!nextcloudURL) { - if (nextcloudURL === undefined) { - return fillServerURL(); - } + const serverURL = settings.get('Accounts_OAuth_Nextcloud_URL')?.trim().replace(/\/*$/, ''); + const clientId = settings.get('Accounts_OAuth_Nextcloud_id'); + const clientSecret = settings.get('Accounts_OAuth_Nextcloud_secret'); + + if (!serverURL || !clientId || !clientSecret) { return; } - config.serverURL = nextcloudURL.trim().replace(/\/*$/, ''); - return Nextcloud.configure(config); -}, 1000); + + addPassportCustomOAuth('nextcloud', { + ...NEXTCLOUD_PATHS, + serverURL, + clientId, + clientSecret, + }); +} Meteor.startup(() => { - settings.watch('Accounts_OAuth_Nextcloud_URL', () => fillServerURL()); + settings.watchMultiple( + ['Accounts_OAuth_Nextcloud', 'Accounts_OAuth_Nextcloud_URL', 'Accounts_OAuth_Nextcloud_id', 'Accounts_OAuth_Nextcloud_secret'], + configureNextcloudOAuth, + ); });