diff --git a/meshuser.js b/meshuser.js index 7fd28435..21d8bae1 100644 --- a/meshuser.js +++ b/meshuser.js @@ -5292,7 +5292,45 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use break; } case 'webpush': { - //console.log(command); + // Check if web push is enabled + if (parent.parent.webpush == null) break; + + // Adds a web push session to the user. Start by sanitizing the input. + if ((typeof command.sub != 'object') && (typeof command.sub.keys != 'object') && (typeof command.sub.endpoint != 'string')) break; + if (common.validateString(command.sub.endpoint, 1, 1024) == false) break; // Check endpoint + if (common.validateString(command.sub.keys.auth, 1, 64) == false) break; // Check key auth + if (common.validateString(command.sub.keys.p256dh, 1, 256) == false) break; // Check key dh + var newWebPush = { endpoint: command.sub.endpoint, keys: { auth: command.sub.keys.auth, p256dh: command.sub.keys.p256dh } } + + // See if we need to add this session + var changed = false; + if (user.webpush == null) { + changed = true; + user.webpush = [newWebPush]; + } else { + var found = false; + for (var i in user.webpush) { + if ((user.webpush[i].endpoint == newWebPush.endpoint) && (user.webpush[i].keys.auth == newWebPush.keys.auth) && (user.webpush[i].keys.p256dh == newWebPush.keys.p256dh)) { found = true; } + } + if (found == true) break; + changed = true; + user.webpush.push(newWebPush); + while (user.webpush.length > 5) { user.webpush.shift(); } + } + + // If we added the session, update the user + if (changed == true) { + // Update the database + parent.db.SetUser(user); + + // Event the change + var message = { etype: 'user', userid: user._id, username: user.name, account: parent.CloneSafeUser(user), action: 'accountchange', domain: domain.id, nolog: 1 }; + if (db.changeStream) { message.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come. + var targets = ['*', 'server-users', user._id]; + if (user.groups) { for (var i in user.groups) { targets.push('server-users:' + i); } } + parent.parent.DispatchEvent(targets, obj, message); + } + break; } case 'print': { diff --git a/views/default.handlebars b/views/default.handlebars index bd36368f..3022e486 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -13842,6 +13842,7 @@ // function setupServiceWorker() { + if ((features2 & 8) == 0) return; // Web push not enabled on this server if ((typeof serverinfo.vapidpublickey != 'string') || (Notification == null) || (Notification.permission != 'granted')) return; // Register the service worker if ('serviceWorker' in navigator) { diff --git a/webserver.js b/webserver.js index 1bfdfcd9..aec02d5a 100644 --- a/webserver.js +++ b/webserver.js @@ -2457,6 +2457,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) { if (obj.parent.amtManager != null) { features2 += 0x00000001; } // Indicates that the Intel AMT manager is active if (obj.parent.firebase != null) { features2 += 0x00000002; } // Indicates the server supports Firebase push messaging if ((obj.parent.firebase != null) && (obj.parent.firebase.pushOnly != true)) { features2 += 0x00000004; } // Indicates the server supports Firebase two-way push messaging + if (obj.parent.webpush != null) { features2 += 0x00000008; } // Indicates web push is enabled // Create a authentication cookie const authCookie = obj.parent.encodeCookie({ userid: dbGetFunc.user._id, domainid: domain.id, ip: req.clientIp }, obj.parent.loginCookieEncryptionKey); @@ -6340,6 +6341,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) { if ((typeof user2.otpsecret == 'string') && (user2.otpsecret != null)) { user2.otpsecret = 1; } // Indicates a time secret is present. if ((typeof user2.otpkeys == 'object') && (user2.otpkeys != null)) { user2.otpkeys = 0; if (user.otpkeys != null) { for (var i = 0; i < user.otpkeys.keys.length; i++) { if (user.otpkeys.keys[i].u == true) { user2.otpkeys = 1; } } } } // Indicates the number of one time backup codes that are active. if ((typeof user2.otphkeys == 'object') && (user2.otphkeys != null)) { user2.otphkeys = user2.otphkeys.length; } // Indicates the number of hardware keys setup + if ((typeof user2.webpush == 'object') && (user2.webpush != null)) { user2.webpush = user2.webpush.length; } // Indicates the number of web push sessions we have return user2; }