Files
wekan/models/lib/windowsSso.js
T
Lauri Ojansivu b7589737f5 File docs/Features the way the WeKan menus are.
A feature's guide now sits under the menu it is used from, read from the
templates rather than from where somebody once put it: Header-Bar,
All-Boards, Pages, Board-Header (search and every Board View), Board-Sidebar
(Board Settings, Filter, Members, Labels, Archive), Rules, Swimlanes, Lists,
Cards, Member-Settings, Admin-Panel and Login-Page. Topics used from several
menus (editor, history, theme, translations, email, troubleshooting) are in
General.

- Each area has a README mapping its menu entries to their guides and to the
  template or server module that implements them. Board-Header/README.md
  lists all 40 Board View entries with the template boardBody.jade renders;
  five views (Table, Calendar Mode, Time, Timeline, Map) have no guide of
  their own yet and say so.
- Board-View.md listed 25 views while BOARD_VIEWS has 40; it now lists all
  of them with their English labels.
- Pages keep their file names. Where a released changelog links an old path,
  a "# Moved" page stays there and points to the new one; test-menu.sh skips
  folders holding only those.
- Undo was filed under Login; it is General/Undo now.
- Every link in docs, code comments, scripts and tests follows the move;
  docsLinksResolve checks them all.

Tests that read a doc follow it to its new place. boardViewSettings now fails
when Board-View.md or Board-Header/README.md differs from BOARD_VIEWS, or
names a template boardBody.jade does not render.

Thanks to xet7 !
2026-10-16 23:45:35 +03:00

110 lines
5.0 KiB
JavaScript

'use strict';
// #5758: Windows single sign-on (Kerberos / NTLM, "Integrated Windows
// Authentication") with node-expose-sspi. server/windowsSso.js runs the
// browser's Negotiate handshake; this module is the decision part, plain
// Node so tests/windowsSso.test.cjs can run it without Windows:
//
// who the account the handshake proved: DOMAIN\user (NTLM, and what
// SSPI reports) or user@REALM (a Kerberos principal);
// allowed only the domains listed in WINDOWS_SSO_DOMAINS - an empty list
// lets nobody in, because a trusted domain's accounts would
// otherwise all be welcome;
// member the WeKan account: the one this Windows account signed in as
// before, else (WINDOWS_SSO_MERGE_EXISTING_USERS=true) the one
// with the same username or Active Directory e-mail, else a new one.
const NAME_CHARS = /^[A-Za-z0-9._$-]{1,64}$/;
const DOMAIN_CHARS = /^[A-Za-z0-9.-]{1,255}$/;
// "CONTOSO\alice" or "alice@CONTOSO.COM" -> { domain: 'contoso', user: 'alice' }
// (the first label of a realm is its NetBIOS-style short name). null for
// anything else, including a machine account (name ending in $).
function parseWindowsName(raw) {
const text = typeof raw === 'string' ? raw.trim() : '';
let domain = '', user = '';
const slash = text.indexOf('\\');
if (slash > 0 && text.indexOf('\\', slash + 1) === -1) {
domain = text.slice(0, slash); user = text.slice(slash + 1);
} else if (slash === -1 && text.indexOf('@') > 0 && text.indexOf('@') === text.lastIndexOf('@')) {
[user, domain] = text.split('@');
domain = domain.split('.')[0];
} else return null;
if (!DOMAIN_CHARS.test(domain) || !NAME_CHARS.test(user) || user.endsWith('$')) return null;
return { domain: domain.toLowerCase(), user: user.toLowerCase() };
}
// WINDOWS_SSO_DOMAINS: comma-separated short domain names (CONTOSO) or DNS
// names (contoso.com, compared by their first label).
function allowedDomains(value) {
return String(value || '').split(',').map(part => part.trim().toLowerCase().split('.')[0]).filter(part => DOMAIN_CHARS.test(part));
}
function isDomainAllowed(parsed, domains) {
return !!parsed && Array.isArray(domains) && domains.includes(parsed.domain);
}
// What the WeKan account gets from the handshake and, when
// WINDOWS_SSO_ACTIVE_DIRECTORY=true, from Active Directory (adUser's
// attributes come as arrays of strings).
function first(value) {
const one = Array.isArray(value) ? value[0] : value;
return typeof one === 'string' ? one.trim() : '';
}
function windowsProfile(parsed, adUser) {
const ad = adUser && typeof adUser === 'object' ? adUser : {};
const mail = first(ad.mail).toLowerCase();
const email = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(mail) && mail.length <= 254 ? mail : '';
const fullname = (first(ad.displayName) || `${first(ad.givenName)} ${first(ad.sn)}`.trim() || parsed.user).slice(0, 200);
return { id: `${parsed.domain}\\${parsed.user}`, username: parsed.user, fullname, email };
}
// Which existing member this sign-in is: { userId } or { create: username }.
// `find(selector)` returns a user or null. The Windows account's own link
// always wins; matching by name or e-mail is only on request, because a local
// "alice" is not necessarily the domain's alice.
async function resolveMember(profile, { find, merge = false } = {}) {
const linked = await find({ 'services.windowsSso.id': profile.id });
if (linked) return { userId: linked._id };
if (merge) {
const byName = await find({ username: profile.username });
if (byName) return { userId: byName._id, link: true };
if (profile.email) {
const byMail = await find({ 'emails.address': profile.email });
if (byMail) return { userId: byMail._id, link: true };
}
}
// A new account, under a free username: alice, alice-2, alice-3, ...
for (let n = 1; n <= 50; n += 1) {
const username = n === 1 ? profile.username : `${profile.username}-${n}`;
if (!await find({ username })) return { create: username };
}
return { refused: 'windows-sso-username-taken' };
}
// Where node-expose-sspi is looked for: it is Windows-only and installed by
// the admin into the bundle (docs/Features/Login-Page/WindowsSSO.md), never a
// dependency every platform downloads.
function moduleSearchBases({ cwd, mainScript }) {
const bases = [];
const add = dir => { if (dir && !bases.includes(dir)) bases.push(dir); };
const join = (...parts) => parts.join('/').replace(/\\/g, '/').replace(/\/+/g, '/');
if (mainScript) {
const dir = String(mainScript).replace(/\\/g, '/').replace(/\/[^/]*$/, '');
add(join(dir, 'programs/server'));
add(dir);
}
if (cwd) {
add(join(cwd, 'programs/server'));
add(String(cwd).replace(/\\/g, '/'));
}
return bases;
}
// Enabled only on Windows, and only when asked.
function windowsSsoEnabled({ platform, env }) {
return platform === 'win32' && String((env && env.WINDOWS_SSO_ENABLED) || '').trim() === 'true';
}
module.exports = { parseWindowsName, allowedDomains, isDomainAllowed, windowsProfile, resolveMember, moduleSearchBases, windowsSsoEnabled };