Files
wekan/server/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

125 lines
5.6 KiB
JavaScript

// #5758: Windows single sign-on - Kerberos or NTLM, the browser's own
// "Integrated Windows Authentication" - with node-expose-sspi
// (models/lib/windowsSso.js holds the decisions). Off unless WeKan runs on
// Windows with WINDOWS_SSO_ENABLED=true and node-expose-sspi installed in the
// bundle (docs/Features/Login-Page/WindowsSSO.md).
//
// The sign-in page links to /sso/windows. There the browser proves who is
// signed in to Windows; WeKan finds or creates that member and sends the
// browser back to /sign-in?windowsSso=<one-time token>, which the page signs
// in with, through the same login checks as every other way of signing in.
import crypto from 'node:crypto';
import path from 'node:path';
import { createRequire } from 'node:module';
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { WebApp } from 'meteor/webapp';
import { Accounts } from 'meteor/accounts-base';
import { check } from 'meteor/check';
import { Random } from 'meteor/random';
import { authEnv } from '/server/lib/authConfig';
import { ensureIndex } from '/server/lib/mongoStartup';
const {
parseWindowsName, allowedDomains, isDomainAllowed, windowsProfile, resolveMember, moduleSearchBases, windowsSsoEnabled,
} = require('/models/lib/windowsSso');
// { _id: sha256(token), userId, expiresAt } - one minute, used once.
export const WindowsSsoTokens = new Mongo.Collection('windowsSsoTokens');
WindowsSsoTokens.deny({ insert: () => true, update: () => true, remove: () => true });
const TOKEN_MS = 60000;
const hash = value => crypto.createHash('sha256').update(String(value)).digest('hex');
const enabled = () => windowsSsoEnabled({ platform: process.platform, env: { WINDOWS_SSO_ENABLED: authEnv('WINDOWS_SSO_ENABLED') } });
// node-expose-sspi from the bundle, or null with the reason logged once.
let sspi;
function loadSspi() {
if (sspi !== undefined) return sspi;
sspi = null;
for (const base of moduleSearchBases({ cwd: process.cwd(), mainScript: process.argv[1] })) {
try {
sspi = createRequire(path.join(base, 'noop.js'))('node-expose-sspi');
break;
} catch (e) { /* the next place */ }
}
if (!sspi) console.warn('Windows SSO: WINDOWS_SSO_ENABLED=true, but node-expose-sspi is not installed (docs/Features/Login-Page/WindowsSSO.md).');
return sspi;
}
let middleware = null;
function negotiate() {
if (!middleware) {
middleware = loadSspi().sso.auth({
useActiveDirectory: authEnv('WINDOWS_SSO_ACTIVE_DIRECTORY') === 'true',
useGroups: false,
});
}
return middleware;
}
const signInUrl = query => `${(process.env.ROOT_URL || Meteor.absoluteUrl()).replace(/\/+$/, '')}/sign-in?${query}`;
function redirect(res, query) {
res.writeHead(302, { Location: signInUrl(query), 'Cache-Control': 'no-store' });
res.end();
}
async function memberFor(user) {
const parsed = parseWindowsName(user && user.name);
if (!isDomainAllowed(parsed, allowedDomains(authEnv('WINDOWS_SSO_DOMAINS')))) return { refused: 'windows-sso-domain' };
const profile = windowsProfile(parsed, user.adUser);
const found = await resolveMember(profile, {
find: selector => Meteor.users.findOneAsync(selector, { fields: { _id: 1 } }),
merge: authEnv('WINDOWS_SSO_MERGE_EXISTING_USERS') === 'true',
});
if (found.refused) return found;
if (found.create) {
const userDoc = { username: found.create, authenticationMethod: 'windows', profile: { fullname: profile.fullname },
services: { windowsSso: { id: profile.id } } };
if (profile.email && !(await Meteor.users.findOneAsync({ 'emails.address': profile.email }, { fields: { _id: 1 } }))) {
userDoc.emails = [{ address: profile.email, verified: true }];
}
return { userId: await Accounts.insertUserDoc({}, userDoc) };
}
if (found.link) await Meteor.users.updateAsync(found.userId, { $set: { 'services.windowsSso.id': profile.id } });
return { userId: found.userId };
}
Meteor.startup(async () => {
Meteor.settings.public.windowsSsoEnabled = enabled() && !!loadSspi();
await ensureIndex(WindowsSsoTokens, { expiresAt: 1 }, { expireAfterSeconds: 0 });
});
WebApp.handlers.get('/sso/windows', (req, res) => {
if (!enabled() || !loadSspi() || Meteor.settings?.public?.sandstorm) {
res.writeHead(404); res.end(); return;
}
// The Negotiate handshake answers 401 with a challenge until the browser
// has proved the account; then it calls on with req.sso.user.
negotiate()(req, res, async error => {
try {
if (error || !req.sso || !req.sso.user) return redirect(res, 'windowsSso=failed');
const member = await memberFor(req.sso.user);
if (!member.userId) return redirect(res, 'windowsSso=failed');
const token = Random.secret(43);
await WindowsSsoTokens.insertAsync({ _id: hash(token), userId: member.userId, expiresAt: new Date(Date.now() + TOKEN_MS) });
return redirect(res, `windowsSso=${encodeURIComponent(token)}`);
} catch (e) {
if (process.env.DEBUG === 'true') console.warn('Windows SSO failed:', e);
return redirect(res, 'windowsSso=failed');
}
});
});
// Accounts.callLoginMethod({ methodArguments: [{ windowsSso: token }] }).
Accounts.registerLoginHandler('windowsSso', async options => {
if (!options || options.windowsSso === undefined) return undefined;
check(options.windowsSso, String);
const invalid = { error: new Meteor.Error(403, 'windows-sso-failed') };
if (!enabled()) return invalid;
const result = await WindowsSsoTokens.rawCollection().findOneAndDelete({ _id: hash(options.windowsSso) });
const doc = result && 'ok' in result && 'value' in result ? result.value : result;
if (!doc || doc.expiresAt <= new Date()) return invalid;
return { userId: doc.userId };
});