diff --git a/api/src/grant.ts b/api/src/grant.ts new file mode 100644 index 0000000000..ac65f4a3cc --- /dev/null +++ b/api/src/grant.ts @@ -0,0 +1,39 @@ +/** + * Grant is the oAuth library + */ + +import env from './env'; + +const enabledProviders = (env.OAUTH_PROVIDERS as string) + .split(',') + .map((provider) => provider.trim().toLowerCase()); + +const config: any = { + defaults: { + origin: env.PUBLIC_URL, + transport: 'session', + prefix: '/auth/oauth', + response: ['tokens', 'profile'], + }, +}; + +for (const [key, value] of Object.entries(env)) { + if (key.startsWith('OAUTH') === false) continue; + + const parts = key.split('_'); + const provider = parts[1].toLowerCase(); + + if (enabledProviders.includes(provider) === false) continue; + + // OAUTH SETTING = VALUE + parts.splice(0, 2); + + const configKey = parts.join('_').toLowerCase(); + + config[provider] = { + ...(config[provider] || {}), + [configKey]: value, + }; +} + +export default config; diff --git a/api/src/utils/get-grant-config.ts b/api/src/utils/get-grant-config.ts deleted file mode 100644 index 779fc8d63b..0000000000 --- a/api/src/utils/get-grant-config.ts +++ /dev/null @@ -1,40 +0,0 @@ -import env from '../env'; - -/** - * Reads the environment variables to construct the configuration object required by Grant - */ -export default function getGrantConfig() { - const enabledProviders = (env.OAUTH_PROVIDERS as string) - .split(',') - .map((provider) => provider.trim()); - - const config: any = { - defaults: { - origin: env.PUBLIC_URL, - transport: 'session', - prefix: '/auth/sso', - response: ['tokens', 'profile'], - }, - }; - - for (const [key, value] of Object.entries(env)) { - if (key.startsWith('OAUTH') === false) continue; - - const parts = key.split('_'); - const provider = parts[1].toLowerCase(); - - if (enabledProviders.includes(provider) === false) continue; - - // OAUTH SETTING = VALUE - parts.splice(0, 2); - - const configKey = parts.join('_').toLowerCase(); - - config[provider] = { - ...(config[provider] || {}), - [configKey]: value, - }; - } - - return config; -}