Files
wekan/models/lib/oauthProviders.js
T
Lauri Ojansivu ad51a03731 Log in by full-page redirect by default with OAuth2/OIDC, Google & co, SAML and CAS.
Every login that chooses between a popup and a redirect opened a popup by
default. Google's sign-in pages, and other identity providers', can send
Cross-Origin-Opener-Policy, which makes Meteor's popup look closed at
once: the login was tried before the provider answered, and the user
was back on the sign-in page with no error. Popups are also blocked in
iframes and on some phones. The full-page redirect has no popup to lose.

- OAUTH2_LOGIN_STYLE (OAuth2/OIDC): redirect unless it says popup, in the
  server, the Admin Panel catalog and the samples.
- OAUTH_PROVIDERS_LOGIN_STYLE (Google, GitHub, Facebook, X, Meteor
  Developer, Weibo, Meetup): redirect by default, in the code, Dockerfile,
  snap, Sandstorm package and samples.
- SAML: both identity-provider profiles log in by redirect;
  SAML_LOGIN_FLOW=popup still chooses the popup, and the client no longer
  falls back to a popup while the configuration loads.
- CAS: redirects unless "popup": true. Its redirect could never finish:
  nothing called Meteor.initCas, which the package now does at startup.
  The sign-in page also passed username and password as loginWithCas's
  options and callback, so its callback never ran, and the CAS redirect
  page echoed the request address into HTML unescaped.

A redirect login's error now reaches the reloaded sign-in page, from
accounts-oauth's page-load hook and the SAML and CAS events, instead of
the page coming back silently.

Verified against a freshly built and prepared bundle with the local
identity-provider suite and no login style set: OIDC, SAML, CAS and all
seven Meteor providers log in by redirect, and each refusal is shown on
the sign-in page (31 of 31 with Chromium). Its LDAP wrong-password case
expected wording that changed in 679a8b3499 and is corrected.

Thanks to xet7 !
2026-10-08 15:18:59 +03:00

350 lines
12 KiB
JavaScript

'use strict';
// The catalog of the OAuth login providers that Meteor's own accounts packages
// offer (accounts-google, accounts-github, accounts-facebook, accounts-twitter,
// accounts-meteor-developer, accounts-weibo, accounts-meetup), and the pure
// decisions built on it: which environment variables configure each one, how a
// `<NAME>_FILE` secret is read, whether a provider is enabled, and whether a
// provider login may take over an existing account.
//
// This file is pure and dependency-free (no Meteor, no database) so it is unit
// tested with plain Node (tests/oauthProviders.test.cjs) and shared by:
// - server/lib/oauthProviders.js, which writes ServiceConfiguration from it;
// - server/models/settings.js, which reports the enabled providers to the
// login form;
// - the Admin Panel, which renders one override block per provider.
//
// SECURITY: nothing here returns a secret to a caller on its own. The secret is
// resolved by resolveProviderConfig() for the SERVER's ServiceConfiguration
// upsert only; the client-facing helpers (enabledProviderKeys and the
// Admin Panel status) go through isProviderEnabled(), which reports only
// whether a secret exists.
const OAUTH_PROVIDERS = [
{
key: 'google',
service: 'google',
envPrefix: 'OAUTH_GOOGLE',
idVar: 'OAUTH_GOOGLE_CLIENT_ID',
secretVar: 'OAUTH_GOOGLE_SECRET',
idField: 'clientId',
loginMethod: 'loginWithGoogle',
icon: 'fa-google',
labelKey: 'oauth-provider-google',
},
{
key: 'github',
service: 'github',
envPrefix: 'OAUTH_GITHUB',
idVar: 'OAUTH_GITHUB_CLIENT_ID',
secretVar: 'OAUTH_GITHUB_SECRET',
idField: 'clientId',
loginMethod: 'loginWithGithub',
icon: 'fa-github',
labelKey: 'oauth-provider-github',
},
{
key: 'facebook',
service: 'facebook',
envPrefix: 'OAUTH_FACEBOOK',
idVar: 'OAUTH_FACEBOOK_APP_ID',
secretVar: 'OAUTH_FACEBOOK_SECRET',
idField: 'appId',
loginMethod: 'loginWithFacebook',
icon: 'fa-facebook',
labelKey: 'oauth-provider-facebook',
},
{
key: 'twitter',
service: 'twitter',
envPrefix: 'OAUTH_TWITTER',
idVar: 'OAUTH_TWITTER_CONSUMER_KEY',
secretVar: 'OAUTH_TWITTER_SECRET',
idField: 'consumerKey',
loginMethod: 'loginWithTwitter',
icon: 'fa-twitter',
labelKey: 'oauth-provider-twitter',
},
{
key: 'meteor-developer',
service: 'meteor-developer',
envPrefix: 'OAUTH_METEOR_DEVELOPER',
idVar: 'OAUTH_METEOR_DEVELOPER_CLIENT_ID',
secretVar: 'OAUTH_METEOR_DEVELOPER_SECRET',
idField: 'clientId',
loginMethod: 'loginWithMeteorDeveloperAccount',
icon: 'fa-rocket',
labelKey: 'oauth-provider-meteor-developer',
},
{
key: 'weibo',
service: 'weibo',
envPrefix: 'OAUTH_WEIBO',
idVar: 'OAUTH_WEIBO_CLIENT_ID',
secretVar: 'OAUTH_WEIBO_SECRET',
idField: 'clientId',
loginMethod: 'loginWithWeibo',
icon: 'fa-weibo',
labelKey: 'oauth-provider-weibo',
},
{
key: 'meetup',
service: 'meetup',
envPrefix: 'OAUTH_MEETUP',
idVar: 'OAUTH_MEETUP_CLIENT_ID',
secretVar: 'OAUTH_MEETUP_SECRET',
idField: 'clientId',
loginMethod: 'loginWithMeetup',
icon: 'fa-meetup',
labelKey: 'oauth-provider-meetup',
},
];
// The two settings shared by every provider, and the passwordless switch.
const OAUTH_PROVIDERS_LOGIN_STYLE_VAR = 'OAUTH_PROVIDERS_LOGIN_STYLE';
const OAUTH_PROVIDERS_MERGE_EXISTING_USERS_VAR = 'OAUTH_PROVIDERS_MERGE_EXISTING_USERS';
// #1904: only these email domains may sign in with a provider, like
// OAUTH2_ALLOWED_EMAIL_DOMAINS for OAuth2/OIDC. Comma separated; empty allows all.
const OAUTH_PROVIDERS_ALLOWED_EMAIL_DOMAINS_VAR = 'OAUTH_PROVIDERS_ALLOWED_EMAIL_DOMAINS';
const PASSWORDLESS_ENABLED_VAR = 'PASSWORDLESS_ENABLED';
const PROVIDER_KEYS = OAUTH_PROVIDERS.map(p => p.key);
function providerByKey(key) {
return OAUTH_PROVIDERS.find(p => p.key === key);
}
function providerByService(service) {
return OAUTH_PROVIDERS.find(p => p.service === service);
}
function isTrue(value) {
return value === true || value === 'true';
}
function isEmpty(value) {
return value === undefined || value === null || value === '';
}
/**
* Read one environment variable, honouring its `<NAME>_FILE` variant the way
* OAUTH2_SECRET_FILE / MONGO_URL_FILE are documented: when `<NAME>` is
* empty and `<NAME>_FILE` names a readable file, the trimmed file content is
* the value (Docker/Kubernetes secrets mounted as files). `env` and `readFile`
* are injectable so the decision is testable without touching the real
* environment or disk.
*
* @param {string} name
* @param {object} [env=process.env]
* @param {function(string): string} [readFile] - returns the file's content or throws
* @returns {string|undefined}
*/
function providerEnvValue(name, env = process.env, readFile) {
const direct = env[name];
if (!isEmpty(direct)) return String(direct);
const file = env[`${name}_FILE`];
if (isEmpty(file)) return undefined;
const reader = readFile || defaultReadFile;
try {
const content = reader(String(file));
if (isEmpty(content)) return undefined;
return String(content).trim() || undefined;
} catch (e) {
return undefined;
}
}
function defaultReadFile(file) {
// Required lazily so a browser bundle that pulls in the catalog never sees
// `fs`; only the server ever reaches this line.
// eslint-disable-next-line global-require
return require('fs').readFileSync(file, 'utf8');
}
/**
* Admin-panel override first, environment second, like
* models/lib/configResolver.js resolveConfigValue(). Kept inline so this file
* stays dependency-free for the unit test.
*/
function pick(adminValue, envValue) {
if (!isEmpty(adminValue)) return { value: adminValue, source: 'admin' };
if (!isEmpty(envValue)) return { value: envValue, source: 'env' };
return { value: undefined, source: 'default' };
}
/**
* Resolve the full configuration of one provider. `admin` is the provider's
* Admin Panel sub-document ({ enabled, id, secret, loginStyle,
* mergeExistingUsers }, all optional, possibly absent), `env` the environment.
*
* The result CONTAINS THE SECRET. It is for the server-side
* ServiceConfiguration upsert only; never hand it to a method result or a
* publication. Use isProviderEnabled() for anything client-facing.
*
* @returns {{ key, service, enabled: boolean, id, idField, secret, loginStyle,
* sources: { enabled, id, secret, loginStyle } }}
*/
function resolveProviderConfig(provider, admin, env = process.env, readFile) {
const a = admin || {};
const enabledPick = pick(
a.enabled === undefined ? undefined : a.enabled,
providerEnvValue(`${provider.envPrefix}_ENABLED`, env, readFile),
);
const idPick = pick(a.id, providerEnvValue(provider.idVar, env, readFile));
const secretPick = pick(a.secret, providerEnvValue(provider.secretVar, env, readFile));
const stylePick = pick(
a.loginStyle,
providerEnvValue(OAUTH_PROVIDERS_LOGIN_STYLE_VAR, env, readFile),
);
const enabledFlag = isTrue(enabledPick.value);
const enabled = enabledFlag && !isEmpty(idPick.value) && !isEmpty(secretPick.value);
return {
key: provider.key,
service: provider.service,
idField: provider.idField,
enabled,
enabledFlag,
id: isEmpty(idPick.value) ? undefined : String(idPick.value),
secret: isEmpty(secretPick.value) ? undefined : String(secretPick.value),
loginStyle: normalizeLoginStyle(stylePick.value),
sources: {
enabled: enabledPick.source,
id: idPick.source,
secret: secretPick.source,
loginStyle: stylePick.source,
},
};
}
// redirect is the default, like OAUTH2_LOGIN_STYLE; popup only when asked
// for. A provider's sign-in page (Google's among them) can send
// Cross-Origin-Opener-Policy, which makes Meteor's popup look closed at once,
// so the login is tried before the provider answered and fails silently.
function normalizeLoginStyle(value) {
return String(value || '').trim().toLowerCase() === 'popup' ? 'popup' : 'redirect';
}
/**
* Whether the provider is enabled AND has both credentials - the same
* decision resolveProviderConfig() makes, without returning the secret. Safe
* for client-facing callers.
*/
function isProviderEnabled(provider, admin, env = process.env, readFile) {
return resolveProviderConfig(provider, admin, env, readFile).enabled;
}
/**
* The keys of every provider currently enabled. `adminProviders` is the
* Settings document's `oauthProviders` sub-document ({ [key]: {...} }), which
* may be absent.
*/
function enabledProviderKeys(adminProviders, env = process.env, readFile) {
const admins = adminProviders || {};
return OAUTH_PROVIDERS.filter(p => isProviderEnabled(p, admins[p.key], env, readFile)).map(
p => p.key,
);
}
/**
* Whether a provider login whose email matches an existing account may take
* that account over. OFF by default, like OAUTH2_MERGE_EXISTING_USERS,
* SAML_MERGE_EXISTING_USERS and CAS_MERGE_EXISTING_USERS; the Admin Panel's
* per-provider `mergeExistingUsers` overrides the shared env var.
*/
function isMergeExistingUsersAllowed(admin, env = process.env) {
const a = admin || {};
const picked = pick(
a.mergeExistingUsers === undefined ? undefined : a.mergeExistingUsers,
env[OAUTH_PROVIDERS_MERGE_EXISTING_USERS_VAR],
);
return isTrue(picked.value);
}
/**
* The email domains provider logins are restricted to (#1904): the Admin
* Panel value when one is stored, otherwise OAUTH_PROVIDERS_ALLOWED_EMAIL_DOMAINS.
* Empty means every domain, as OAUTH2_ALLOWED_EMAIL_DOMAINS does for OAuth2.
*/
function allowedProviderEmailDomains(adminValue, env = process.env) {
return pick(adminValue, env[OAUTH_PROVIDERS_ALLOWED_EMAIL_DOMAINS_VAR]).value || '';
}
/**
* The pure account-conflict decision, made when a provider login arrives:
* - no existing account with that email -> create a new one ('create');
* - the existing account was made by the SAME provider -> it is that user's
* own account, log them in ('link');
* - otherwise the account belongs to another login method. Merge only when
* allowed AND the provider asserts the email is verified; else 'conflict'.
*
* @param {object} args
* @param {object|undefined} args.existingUser - { services?, authenticationMethod? }
* @param {string} args.providerKey - catalog key ('google', ...)
* @param {boolean} args.mergeAllowed
* @param {boolean} args.emailVerified
* @returns {'create'|'link'|'merge'|'conflict'}
*/
function decideAccountConflict({ existingUser, providerKey, mergeAllowed, emailVerified }) {
if (!existingUser) return 'create';
const provider = providerByKey(providerKey);
const service = provider ? provider.service : providerKey;
const ownedByProvider =
(existingUser.services && existingUser.services[service]) ||
existingUser.authenticationMethod === providerKey;
if (ownedByProvider) return 'link';
if (mergeAllowed && emailVerified === true) return 'merge';
return 'conflict';
}
/**
* Passwordless (accounts-passwordless, e-mailed one-time code) is a login
* method of its own, switched by PASSWORDLESS_ENABLED or the Admin Panel.
*/
function isPasswordlessEnabled(adminValue, env = process.env) {
const picked = pick(
adminValue === undefined ? undefined : adminValue,
env[PASSWORDLESS_ENABLED_VAR],
);
return isTrue(picked.value);
}
/**
* Derive a username the way wekan-oidc does: the provider's username, else
* the local part of the email, else the display name compacted, else the
* provider's id.
*/
function deriveUsername(profile) {
const p = profile || {};
const candidates = [
p.username,
p.screenName,
p.login,
typeof p.email === 'string' ? p.email.split('@')[0] : undefined,
typeof p.name === 'string' ? p.name.replace(/\s+/g, '').toLowerCase() : undefined,
p.id !== undefined && p.id !== null ? String(p.id) : undefined,
];
const found = candidates.find(c => typeof c === 'string' && c.trim().length > 0);
return found ? found.trim() : undefined;
}
module.exports = {
OAUTH_PROVIDERS,
PROVIDER_KEYS,
OAUTH_PROVIDERS_LOGIN_STYLE_VAR,
OAUTH_PROVIDERS_MERGE_EXISTING_USERS_VAR,
OAUTH_PROVIDERS_ALLOWED_EMAIL_DOMAINS_VAR,
allowedProviderEmailDomains,
PASSWORDLESS_ENABLED_VAR,
providerByKey,
providerByService,
providerEnvValue,
resolveProviderConfig,
normalizeLoginStyle,
isProviderEnabled,
enabledProviderKeys,
isMergeExistingUsersAllowed,
decideAccountConflict,
isPasswordlessEnabled,
deriveUsername,
};