diff --git a/apps/meteor/.meteor/packages b/apps/meteor/.meteor/packages index 4242b98f6bed3..000e897aa4ce5 100644 --- a/apps/meteor/.meteor/packages +++ b/apps/meteor/.meteor/packages @@ -10,7 +10,6 @@ rocketchat:version accounts-base@3.1.2 accounts-facebook@1.3.4 -accounts-github@1.5.1 accounts-google@1.4.1 accounts-meteor-developer@1.5.1 accounts-oauth@1.4.6 diff --git a/apps/meteor/.meteor/versions b/apps/meteor/.meteor/versions index 04fcb4d920bdd..29f3113919760 100644 --- a/apps/meteor/.meteor/versions +++ b/apps/meteor/.meteor/versions @@ -1,6 +1,5 @@ accounts-base@3.1.2 accounts-facebook@1.3.4 -accounts-github@1.5.1 accounts-google@1.4.1 accounts-meteor-developer@1.5.1 accounts-oauth@1.4.6 @@ -35,7 +34,6 @@ facebook-oauth@1.11.6 facts-base@1.0.2 fetch@0.1.6 geojson-utils@1.0.12 -github-oauth@1.4.2 google-oauth@1.4.5 hot-code-push@1.0.5 http@3.0.0 diff --git a/apps/meteor/app/github/server/index.ts b/apps/meteor/app/github/server/index.ts new file mode 100644 index 0000000000000..cf327e4971bb2 --- /dev/null +++ b/apps/meteor/app/github/server/index.ts @@ -0,0 +1 @@ +import './lib'; diff --git a/apps/meteor/app/github/server/lib.ts b/apps/meteor/app/github/server/lib.ts new file mode 100644 index 0000000000000..abdc87419956a --- /dev/null +++ b/apps/meteor/app/github/server/lib.ts @@ -0,0 +1,19 @@ +import type { OauthConfig } from '@rocket.chat/core-typings'; + +import { CustomOAuth } from '../../custom-oauth/server/custom_oauth_server'; + +const config: OauthConfig = { + serverURL: 'https://github.com', + identityPath: 'https://api.github.com/user', + tokenPath: 'https://github.com/login/oauth/access_token', + scope: 'user:email', + mergeUsers: false, + addAutopublishFields: { + forLoggedInUser: ['services.github'], + forOtherUsers: ['services.github.username'], + }, + accessTokenParam: 'access_token', + identityTokenSentVia: 'header', +}; + +export const Github = new CustomOAuth('github', config); diff --git a/apps/meteor/client/meteor/login/github.ts b/apps/meteor/client/meteor/login/github.ts deleted file mode 100644 index 98c8fb8fea76d..0000000000000 --- a/apps/meteor/client/meteor/login/github.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Random } from '@rocket.chat/random'; -import { Accounts } from 'meteor/accounts-base'; -// eslint-disable-next-line import/no-duplicates -import { Github } from 'meteor/github-oauth'; -import { Meteor } from 'meteor/meteor'; -// eslint-disable-next-line import/no-duplicates -import { OAuth } from 'meteor/oauth'; - -import { createOAuthTotpLoginMethod } from './oauth'; -import { overrideLoginMethod } from '../../lib/2fa/overrideLoginMethod'; -import { wrapRequestCredentialFn } from '../../lib/wrapRequestCredentialFn'; - -const { loginWithGithub } = Meteor; -const loginWithGithubAndTOTP = createOAuthTotpLoginMethod(Github); -Meteor.loginWithGithub = (options, callback) => { - overrideLoginMethod(loginWithGithub, [options], callback, loginWithGithubAndTOTP); -}; - -Github.requestCredential = wrapRequestCredentialFn('github', ({ config, loginStyle, options, credentialRequestCompleteCallback }) => { - const credentialToken = Random.secret(); - const scope = options?.requestPermissions || ['user:email']; - const flatScope = scope.map(encodeURIComponent).join('+'); - - let allowSignup = ''; - if (Accounts._options?.forbidClientAccountCreation) { - allowSignup = '&allow_signup=false'; - } - - const loginUrl = - `https://github.com/login/oauth/authorize` + - `?client_id=${config.clientId}` + - `&scope=${flatScope}` + - `&redirect_uri=${OAuth._redirectUri('github', config)}` + - `&state=${OAuth._stateParam(loginStyle, credentialToken, options.redirectUrl)}${allowSignup}`; - - OAuth.launchLogin({ - loginService: 'github', - loginStyle, - loginUrl, - credentialRequestCompleteCallback, - credentialToken, - popupOptions: { width: 900, height: 450 }, - }); -}); diff --git a/apps/meteor/client/meteor/login/index.ts b/apps/meteor/client/meteor/login/index.ts index cef3570085f43..d49f3653de683 100644 --- a/apps/meteor/client/meteor/login/index.ts +++ b/apps/meteor/client/meteor/login/index.ts @@ -1,7 +1,6 @@ import './cas'; import './crowd'; import './facebook'; -import './github'; import './google'; import './ldap'; import './meteorDeveloperAccount'; diff --git a/apps/meteor/client/views/root/AppLayout.tsx b/apps/meteor/client/views/root/AppLayout.tsx index 17c8561ca489b..7a62fff7b9c5b 100644 --- a/apps/meteor/client/views/root/AppLayout.tsx +++ b/apps/meteor/client/views/root/AppLayout.tsx @@ -8,6 +8,7 @@ import { useDolphinOAuth } from './hooks/customOAuth/useDolphinOAuth'; import { useDrupalOAuth } from './hooks/customOAuth/useDrupalOAuth'; import { useGitHubEnterpriseOAuth } from './hooks/customOAuth/useGitHubEnterpriseOAuth'; import { useGitLabOAuth } from './hooks/customOAuth/useGitLabOAuth'; +import { useGithubOAuth } from './hooks/customOAuth/useGithubOAuth'; import { useNextcloudOAuth } from './hooks/customOAuth/useNextcloudOAuth'; import { useWordPressOAuth } from './hooks/customOAuth/useWordPressOAuth'; import { useAnalytics } from './hooks/useAnalytics'; @@ -56,6 +57,7 @@ const AppLayout = () => { useLivechatEnterprise(); useNextcloudOAuth(); useGitLabOAuth(); + useGithubOAuth(); useGitHubEnterpriseOAuth(); useDrupalOAuth(); useDolphinOAuth(); diff --git a/apps/meteor/client/views/root/hooks/customOAuth/useGithubOAuth.ts b/apps/meteor/client/views/root/hooks/customOAuth/useGithubOAuth.ts new file mode 100644 index 0000000000000..acf799e27344f --- /dev/null +++ b/apps/meteor/client/views/root/hooks/customOAuth/useGithubOAuth.ts @@ -0,0 +1,33 @@ +import type { OauthConfig } from '@rocket.chat/core-typings'; +import { useSetting } from '@rocket.chat/ui-contexts'; +import { useEffect } from 'react'; + +import { CustomOAuth } from '../../../../lib/customOAuth/CustomOAuth'; + +const config: OauthConfig = { + authorizePath: 'https://github.com/login/oauth/authorize', + serverURL: 'https://github.com', + identityPath: 'https://api.github.com/user', + tokenPath: 'https://github.com/login/oauth/access_token', + scope: 'user:email', + mergeUsers: false, + addAutopublishFields: { + forLoggedInUser: ['services.github'], + forOtherUsers: ['services.github.username'], + }, + accessTokenParam: 'access_token', +} as const satisfies OauthConfig; + +const Github = CustomOAuth.configureOAuthService('github', config); + +export const useGithubOAuth = () => { + const enabled = useSetting('Accounts_OAuth_Github'); + + useEffect(() => { + if (enabled) { + Github.configure({ + ...config, + }); + } + }, [enabled]); +}; diff --git a/apps/meteor/definition/externals/meteor/github-oauth.d.ts b/apps/meteor/definition/externals/meteor/github-oauth.d.ts deleted file mode 100644 index ac67405bd4e4c..0000000000000 --- a/apps/meteor/definition/externals/meteor/github-oauth.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -declare module 'meteor/github-oauth' { - export const Github: any; -} diff --git a/apps/meteor/server/importPackages.ts b/apps/meteor/server/importPackages.ts index 61cb32f15958d..49af9b1ca237a 100644 --- a/apps/meteor/server/importPackages.ts +++ b/apps/meteor/server/importPackages.ts @@ -79,3 +79,4 @@ import '../app/ui-utils/server'; import '../app/reactions/server'; import '../app/livechat/server'; import '../app/authentication/server'; +import '../app/github/server';