mirror of
https://github.com/directus/directus.git
synced 2026-01-31 15:07:56 -05:00
Start on Grant / oAUTH flow
This commit is contained in:
24
src/utils/get-email-from-profile.ts
Normal file
24
src/utils/get-email-from-profile.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { get } from 'lodash';
|
||||
|
||||
// The path in JSON to fetch the email address from the profile.
|
||||
const profileMap = {
|
||||
github: 'email',
|
||||
};
|
||||
|
||||
/**
|
||||
* Extract the email address from a given user profile coming from a providers API
|
||||
*
|
||||
* Falls back to OAUTH_<PROVIDER>_PROFILE_EMAIL if we don't have it preconfigured yet
|
||||
*
|
||||
* This is used in the SSO flow to extract the users
|
||||
*/
|
||||
export default function getEmailFromProfile(provider: string, profile: Record<string, any>) {
|
||||
const path =
|
||||
profileMap[provider] || process.env[`OAUTH_${provider.toUpperCase()}_PROFILE_EMAIL`];
|
||||
|
||||
if (!path) {
|
||||
throw new Error('Path to email in profile object is unknown.');
|
||||
}
|
||||
|
||||
return get(profile, path);
|
||||
}
|
||||
38
src/utils/get-grant-config.ts
Normal file
38
src/utils/get-grant-config.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Reads the environment variables to construct the configuration object required by Grant
|
||||
*/
|
||||
export default function getGrantConfig() {
|
||||
const enabledProviders = process.env.OAUTH_PROVIDERS.split(',').map((provider) =>
|
||||
provider.trim()
|
||||
);
|
||||
|
||||
const config: any = {
|
||||
defaults: {
|
||||
origin: process.env.PUBLIC_URL,
|
||||
transport: 'session',
|
||||
prefix: '/auth/sso',
|
||||
response: ['tokens', 'profile'],
|
||||
},
|
||||
};
|
||||
|
||||
for (const [key, value] of Object.entries(process.env)) {
|
||||
if (key.startsWith('OAUTH') === false) continue;
|
||||
|
||||
const parts = key.split('_');
|
||||
const provider = parts[1].toLowerCase();
|
||||
|
||||
if (enabledProviders.includes(provider) === false) continue;
|
||||
|
||||
// OAUTH <PROVIDER> SETTING = VALUE
|
||||
parts.splice(0, 2);
|
||||
|
||||
const configKey = parts.join('_').toLowerCase();
|
||||
|
||||
config[provider] = {
|
||||
...(config[provider] || {}),
|
||||
[configKey]: value,
|
||||
};
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
Reference in New Issue
Block a user