-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvapid-configuration.js
36 lines (32 loc) · 1.34 KB
/
vapid-configuration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module.exports = function (RED) {
const webpush = require('web-push');
function VapidConfigurationNode (config) {
RED.nodes.createNode(this, config)
this.subject = config.subject
this.publicKey = config.publicKey
this.privateKey = config.privateKey
this.gcmApiKey = config.gcmApiKey
this.timeout = config.timeout
// Older nodes (version 0.0.3 and below) have no timeout option, so set it to 0 (= no custom timeout)
if (this.timeout == undefined) {
this.timeout = 0;
}
}
RED.nodes.registerType('vapid-configuration', VapidConfigurationNode)
// Make the key pair generation available to the config screen (in the flow editor)
RED.httpAdmin.get('/vapid_configuration/generate_key_pair', RED.auth.needsPermission('vapid-configuration.write'), async function(req, res){
try {
// Generate a VAPID keypair
const vapidKeys = webpush.generateVAPIDKeys();
// Return public key and private key to the config screen (since they need to be stored in the node's credentials)
res.json({
publicKey: vapidKeys.publicKey,
privateKey: vapidKeys.privateKey
})
}
catch (err) {
console.log("Error while generating VAPID keypair: " + err)
res.status(500).json({error: 'Error while generating VAPID keypair'})
}
});
}