Files
wekan/server/accountWebhook.js
T
Lauri Ojansivu ea256dd772 Admin Panel: a login and registration webhook, off by default.
Admin Panel / Settings / Global Webhooks gets one address WeKan tells
when somebody logs in, when a new account is made, or both - nothing is
sent until an event is ticked. Each message is a JSON POST with the
event, the user name and the time; the email, the login's IP address,
the full name and the sign-in method are added only when ticked. A token
goes in X-Wekan-Token, as for board webhooks.

It is sent after the login, through webhookFetch - the SSRF guard,
Internal destinations and, on Sandstorm, the Powerbox - and a receiver
that is down only leaves a log line. The setting is read and saved
through site-admin methods and never published.

models/lib/accountWebhook.js cleans the setting and builds the message;
tests/accountWebhook.test.cjs checks the default carries name and time
only, each extra field only when ticked, the events separately, bad
addresses refused, and that no hash, services or user id is sent.
Documented in docs/Features/Webhooks/Login-and-Registration-Webhook.md.
The new strings are English placeholders until the next translation pass.

Thanks to KrunalDholakiya and xet7 !

Fixes #2290.
2026-10-14 14:25:19 +03:00

72 lines
2.8 KiB
JavaScript

// #2290: the login and registration webhook (models/lib/accountWebhook.js).
// Read and saved through site-admin methods, never published: the address
// and its token are the administrator's.
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { check } from 'meteor/check';
import Settings from '/models/settings';
import Users from '/models/users';
const { cleanAccountWebhook, sends, accountWebhookPayload } = require('/models/lib/accountWebhook');
async function requireSiteAdmin(userId) {
const user = userId && await Meteor.users.findOneAsync(userId, { fields: { isAdmin: 1 } });
if (!user || user.isAdmin !== true) throw new Meteor.Error('not-authorized');
}
// Every login asks, so the setting is cached briefly.
let cached = { at: 0, config: null };
async function accountWebhookConfig() {
if (Date.now() - cached.at < 10000) return cached.config;
const setting = await Settings.findOneAsync({}, { fields: { accountWebhook: 1 } });
cached = { at: Date.now(), config: cleanAccountWebhook((setting && setting.accountWebhook) || {}).value };
return cached.config;
}
// Fire and forget: a login must never wait for, or fail because of, the
// address it is reported to.
function send(event, user, ip) {
Meteor.defer(async () => {
try {
const config = await accountWebhookConfig();
if (!sends(config, event)) return;
const headers = { 'Content-Type': 'application/json' };
if (config.token) headers['X-Wekan-Token'] = config.token;
const { webhookFetch } = require('/server/sandstormEgress');
await webhookFetch(config.url, { method: 'POST', headers,
body: JSON.stringify(accountWebhookPayload(event, user, { config, ip })), timeoutMs: 10000 });
} catch (error) {
console.warn(`Login and registration webhook: ${event} not delivered: ${error && error.message}`);
}
});
}
if (Meteor.isServer) {
Accounts.onLogin((info) => {
if (!info || !info.user) return;
send('login', info.user, info.connection && info.connection.clientAddress);
});
Users.after.insert((userId, doc) => {
if (doc) send('register', doc);
});
}
Meteor.methods({
async getAccountWebhook() {
await requireSiteAdmin(this.userId);
const setting = await Settings.findOneAsync({}, { fields: { accountWebhook: 1 } });
return cleanAccountWebhook((setting && setting.accountWebhook) || {}).value;
},
async saveAccountWebhook(input) {
check(input, Object);
await requireSiteAdmin(this.userId);
const { value, error } = cleanAccountWebhook(input);
if (error) throw new Meteor.Error(error);
const setting = await Settings.findOneAsync({});
if (!setting) throw new Meteor.Error('settings-not-found');
await Settings.updateAsync(setting._id, { $set: { accountWebhook: value } });
cached = { at: 0, config: null };
return value;
},
});