Files
wekan/tests/windowsSso.test.cjs
T
Lauri Ojansivu a594a58117 Login: Windows single sign-on with Kerberos or NTLM.
#5758. On a WeKan running on Windows in a domain, members already signed
in to Windows can sign in with "Sign in with Windows", without a
password: /sso/windows runs the browser's Negotiate handshake with
node-expose-sspi, WeKan finds or creates the member, and the browser
comes back to /sign-in with a one-time token (random, kept as its
SHA-256, one minute, used once) that the page signs in with through a
login handler - so every login check applies, deactivated accounts and
SQRL's sqrlonly included.

- WINDOWS_SSO_ENABLED=true turns it on, on Windows only.
- WINDOWS_SSO_DOMAINS lists the domains allowed; empty means nobody, and
  machine accounts never.
- WINDOWS_SSO_ACTIVE_DIRECTORY=true takes name and e-mail from AD.
- WINDOWS_SSO_MERGE_EXISTING_USERS=true signs in to the account with the
  same username or e-mail; otherwise a new account is made beside a
  local one of the same name, since a local alice is not necessarily the
  domain's alice. A Windows account signs in to the account it linked to
  before.

node-expose-sspi is Windows-only and not a dependency every platform
downloads: the admin installs it into the bundle, and WeKan looks for it
there (docs/Features/Login/WindowsSSO.md; start-wekan.bat has the
variables).

Built without a Windows domain to test against, as decided: the
decisions - names, domains, profile, which member - are tested in plain
Node in tests/windowsSso.test.cjs, with negatives for every malformed or
foreign name, an empty allowlist and a same-named local account; the
handshake and the server wiring are pinned from source. New strings
translated in all locales.

Thanks to nikalasmd and xet7 !
2026-10-14 21:46:17 +03:00

113 lines
7.9 KiB
JavaScript

'use strict';
// #5758: Windows single sign-on (Kerberos / NTLM through node-expose-sspi).
// The decisions run here in plain Node; the handshake itself needs Windows,
// a domain and node-expose-sspi, which this test environment does not have,
// so the server wiring is pinned by reading its source.
// Run: node tests/windowsSso.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const {
parseWindowsName, allowedDomains, isDomainAllowed, windowsProfile, resolveMember, moduleSearchBases, windowsSsoEnabled,
} = require('../models/lib/windowsSso.js');
const ROOT = path.join(__dirname, '..');
const read = f => fs.readFileSync(path.join(ROOT, f), 'utf8');
(async () => {
// Who the handshake proved.
assert.deepEqual(parseWindowsName('CONTOSO\\Alice'), { domain: 'contoso', user: 'alice' });
assert.deepEqual(parseWindowsName('alice@CONTOSO.EXAMPLE.COM'), { domain: 'contoso', user: 'alice' });
assert.deepEqual(parseWindowsName(' CONTOSO\\a.b-c_d '), { domain: 'contoso', user: 'a.b-c_d' });
// Negative: nothing that is not one account in one domain.
for (const bad of ['', 'alice', '\\alice', 'CONTOSO\\', 'A\\B\\c', 'alice@', '@CONTOSO', 'a@b@c', 'CONTOSO\\WS01$',
'CONTOSO\\al ice', 'CONTOSO\\../admin', 'CON TOSO\\alice', 'CONTOSO\\alice@x', null, undefined, 42, ['CONTOSO\\alice']]) {
assert.equal(parseWindowsName(bad), null, JSON.stringify(bad));
}
console.log(' ok - DOMAIN\\user and user@REALM, nothing else, no machine accounts');
assert.deepEqual(allowedDomains('CONTOSO, fabrikam.example.com,, '), ['contoso', 'fabrikam']);
assert.equal(isDomainAllowed(parseWindowsName('CONTOSO\\alice'), allowedDomains('contoso')), true);
assert.equal(isDomainAllowed(parseWindowsName('alice@FABRIKAM.EXAMPLE.COM'), allowedDomains('fabrikam.example.com')), true);
// Negative: an empty list lets nobody in; another domain is refused.
assert.equal(isDomainAllowed(parseWindowsName('CONTOSO\\alice'), allowedDomains('')), false);
assert.equal(isDomainAllowed(parseWindowsName('CONTOSO\\alice'), allowedDomains(undefined)), false);
assert.equal(isDomainAllowed(parseWindowsName('EVIL\\alice'), allowedDomains('contoso')), false);
assert.equal(isDomainAllowed(null, allowedDomains('contoso')), false);
console.log(' ok - only WINDOWS_SSO_DOMAINS; empty means nobody');
const alice = parseWindowsName('CONTOSO\\alice');
assert.deepEqual(windowsProfile(alice, { mail: ['Alice@Contoso.example'], displayName: ['Alice Example'] }),
{ id: 'contoso\\alice', username: 'alice', fullname: 'Alice Example', email: 'alice@contoso.example' });
assert.deepEqual(windowsProfile(alice, { givenName: ['Alice'], sn: ['Example'] }).fullname, 'Alice Example');
assert.deepEqual(windowsProfile(alice, undefined), { id: 'contoso\\alice', username: 'alice', fullname: 'alice', email: '' });
assert.equal(windowsProfile(alice, { mail: ['not an address'] }).email, '', 'a malformed mail is not used');
console.log(' ok - the profile from the handshake and Active Directory');
// Which member: the linked one first; by name or e-mail only on request.
const users = [
{ _id: 'linked', username: 'someone', services: { windowsSso: { id: 'contoso\\alice' } } },
{ _id: 'local-alice', username: 'alice' },
{ _id: 'mail-bob', username: 'robert', emails: [{ address: 'bob@contoso.example' }] },
];
const find = async selector => users.find(u => Object.entries(selector).every(([k, v]) =>
k === 'username' ? u.username === v
: k === 'services.windowsSso.id' ? u.services?.windowsSso?.id === v
: k === 'emails.address' ? (u.emails || []).some(e => e.address === v) : false)) || null;
const profile = windowsProfile(alice, { mail: ['alice@contoso.example'] });
assert.deepEqual(await resolveMember(profile, { find }), { userId: 'linked' });
assert.deepEqual(await resolveMember(profile, { find, merge: true }), { userId: 'linked' }, 'the link wins over a name');
const unlinked = { ...profile, id: 'contoso\\alice2' };
// Negative: without merge a local "alice" is NOT taken over - a new account is made beside it.
assert.deepEqual(await resolveMember(unlinked, { find }), { create: 'alice-2' });
assert.deepEqual(await resolveMember(unlinked, { find, merge: true }), { userId: 'local-alice', link: true });
const bob = windowsProfile(parseWindowsName('CONTOSO\\bob'), { mail: ['bob@contoso.example'] });
assert.deepEqual(await resolveMember(bob, { find }), { create: 'bob' });
assert.deepEqual(await resolveMember(bob, { find, merge: true }), { userId: 'mail-bob', link: true });
const crowded = async selector => (selector.username ? { _id: 'taken' } : null);
assert.deepEqual(await resolveMember(bob, { find: crowded }), { refused: 'windows-sso-username-taken' });
console.log(' ok - the linked member, merge only on request, a free username otherwise');
// Only on Windows, only when asked.
assert.equal(windowsSsoEnabled({ platform: 'win32', env: { WINDOWS_SSO_ENABLED: 'true' } }), true);
for (const [platform, value] of [['linux', 'true'], ['darwin', 'true'], ['win32', ''], ['win32', 'false'], ['win32', 'TRUE '], ['win32', undefined]]) {
assert.equal(windowsSsoEnabled({ platform, env: { WINDOWS_SSO_ENABLED: value } }), false, `${platform} ${value}`);
}
assert.deepEqual(moduleSearchBases({ cwd: 'C:\\wekan\\bundle', mainScript: 'C:\\wekan\\bundle\\main.js' }),
['C:/wekan/bundle/programs/server', 'C:/wekan/bundle']);
console.log(' ok - Windows only, opt-in, node-expose-sspi from the bundle');
// The server: a one-time token, used once, through the ordinary login checks.
const server = read('server/windowsSso.js');
assert.match(server, /if \(!enabled\(\) \|\| !loadSspi\(\) \|\| Meteor\.settings\?\.public\?\.sandstorm\) \{\s*res\.writeHead\(404\)/);
assert.match(server, /isDomainAllowed\(parsed, allowedDomains\(authEnv\('WINDOWS_SSO_DOMAINS'\)\)\)/);
assert.match(server, /merge: authEnv\('WINDOWS_SSO_MERGE_EXISTING_USERS'\) === 'true'/);
assert.match(server, /WindowsSsoTokens\.insertAsync\(\{ _id: hash\(token\), userId: member\.userId, expiresAt: new Date\(Date\.now\(\) \+ TOKEN_MS\) \}\)/);
assert.match(server, /findOneAndDelete\(\{ _id: hash\(options\.windowsSso\) \}\)/, 'single use');
assert.match(server, /if \(!doc \|\| doc\.expiresAt <= new Date\(\)\) return invalid;/);
assert.match(server, /Accounts\.registerLoginHandler\('windowsSso'/, 'a login handler, so every validateLoginAttempt rule applies');
assert.match(server, /ensureIndex\(WindowsSsoTokens, \{ expiresAt: 1 \}, \{ expireAfterSeconds: 0 \}\)/);
// Negative: never minted outside the login handler, never stored in clear,
// never a dependency every platform installs.
assert.doesNotMatch(server, /_generateStampedLoginToken|insertActiveLoginToken|Set-Cookie/);
assert.doesNotMatch(server, /insertAsync\(\{ _id: token/);
assert.doesNotMatch(read('package.json'), /node-expose-sspi/);
assert.match(read('server/imports.js'), /^import '\/server\/windowsSso';$/m);
console.log(' ok - the server: one-time hashed token, single use, ordinary login checks');
// The sign-in page.
assert.match(read('client/components/main/layouts.jade'), /if windowsSsoOffered\s*\n\s*a\.button\.primary\.js-windows-sso\(href="\/sso\/windows"\)/);
const layouts = read('client/components/main/layouts.js');
assert.match(layouts, /FlowRouter\.getQueryParam\('windowsSso'\)/);
assert.match(layouts, /methodArguments: \[\{ windowsSso: String\(windowsSsoToken\) \}\]/);
assert.match(layouts, /windowsSsoOffered\(\) \{ return Meteor\.settings\?\.public\?\.windowsSsoEnabled === true; \}/);
const en = JSON.parse(read('imports/i18n/data/en.i18n.json'));
for (const key of ['windows-sso-sign-in', 'windows-sso-failed']) assert.equal(typeof en[key], 'string', key);
assert.match(read('docs/Features/Login/WindowsSSO.md'), /WINDOWS_SSO_DOMAINS/);
console.log(' ok - the sign-in page and the docs');
console.log('\nwindowsSso: all tests passed');
})().catch(error => { console.error(error); process.exit(1); });