mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
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.
61 lines
2.3 KiB
JavaScript
61 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
// #2290: Admin Panel / Settings / Global Webhooks / Login and registration
|
|
// webhook - one instance-wide address told when somebody logs in or a new
|
|
// account is made. Off by default, and by default it carries only what
|
|
// happened, the user name and the time; the email, the address the login came
|
|
// from, the full name and the sign-in method are each sent only when ticked.
|
|
//
|
|
// Pure: tests/accountWebhook.test.cjs. Sent by server/accountWebhook.js,
|
|
// through the same SSRF guard as every outgoing webhook.
|
|
|
|
const EXTRA_FIELDS = ['email', 'ip', 'fullname', 'authMethod'];
|
|
|
|
function cleanAccountWebhook(input) {
|
|
const i = input && typeof input === 'object' ? input : {};
|
|
const url = typeof i.url === 'string' ? i.url.trim().slice(0, 2000) : '';
|
|
let error;
|
|
if (url) {
|
|
let parsed;
|
|
try { parsed = new URL(url); } catch (e) { error = 'invalid-url'; }
|
|
if (parsed && !['http:', 'https:'].includes(parsed.protocol)) error = 'invalid-url';
|
|
}
|
|
return {
|
|
value: {
|
|
url: error ? '' : url,
|
|
token: typeof i.token === 'string' ? i.token.trim().slice(0, 500) : '',
|
|
onLogin: i.onLogin === true,
|
|
onRegister: i.onRegister === true,
|
|
fields: Array.isArray(i.fields) ? EXTRA_FIELDS.filter(f => i.fields.includes(f)) : [],
|
|
},
|
|
error,
|
|
};
|
|
}
|
|
|
|
// Whether `event` ('login' | 'register') is sent at all.
|
|
function sends(config, event) {
|
|
if (!config || !config.url) return false;
|
|
return event === 'login' ? config.onLogin === true : event === 'register' ? config.onRegister === true : false;
|
|
}
|
|
|
|
function accountWebhookPayload(event, user, { config, ip, now } = {}) {
|
|
const u = user || {};
|
|
const profile = u.profile || {};
|
|
const fields = (config && Array.isArray(config.fields)) ? config.fields : [];
|
|
const body = {
|
|
event,
|
|
username: u.username || '',
|
|
at: new Date(now || Date.now()).toISOString(),
|
|
};
|
|
if (fields.includes('email')) {
|
|
const email = Array.isArray(u.emails) && u.emails[0] && u.emails[0].address;
|
|
if (email) body.email = email;
|
|
}
|
|
if (fields.includes('ip') && ip) body.ip = String(ip);
|
|
if (fields.includes('fullname') && profile.fullname) body.fullname = profile.fullname;
|
|
if (fields.includes('authMethod') && u.authenticationMethod) body.authMethod = u.authenticationMethod;
|
|
return body;
|
|
}
|
|
|
|
module.exports = { EXTRA_FIELDS, cleanAccountWebhook, sends, accountWebhookPayload };
|