Skip to content

Commit d46e7d7

Browse files
authored
Merge pull request #5 from bartbutenaers/master
Button to generate VAPID keypair
2 parents 11a57dd + 516fb5f commit d46e7d7

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
"web-push-notification": "web-push-notification.js"
2929
}
3030
},
31-
"version": "0.0.1"
31+
"version": "0.0.2"
3232
}

vapid-configuration.html

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@
1010
},
1111
label: function () {
1212
return this.name || this.subject;
13+
},
14+
oneditprepare: function () {
15+
var node = this;
16+
17+
$("#node-input-generateKeyPair").click(function () {
18+
if ($("#node-config-input-publicKey").val() || $("#node-config-input-privateKey").val()) {
19+
if (!confirm("The current keypair will be overwritten! Are you sure to continue?")) {
20+
// The user has clicked the 'Cancel' button
21+
return;
22+
}
23+
}
24+
25+
// Ask the server side flow to generate a new key pair
26+
$.getJSON('vapid_configuration/generate_key_pair', function(jsonData) {
27+
// Show the new keys on the config screen
28+
$("#node-config-input-publicKey").val(jsonData.publicKey);
29+
$("#node-config-input-privateKey").val(jsonData.privateKey);
30+
31+
// Make sure the validators are being triggerd, otherwise the red border will remain around the input fields
32+
$("#node-config-input-publicKey").change();
33+
$("#node-config-input-privateKey").change();
34+
}).error(function() {
35+
RED.notify("Cannot create VAPID key pair. See Node-RED log for more details...", "error");
36+
});
37+
});
1338
}
1439
});
1540

@@ -20,6 +45,10 @@
2045
<label for="node-config-input-subject"><i class="icon-tag"></i> Subject</label>
2146
<input type="text" id="node-config-input-subject" placeholder="This must be either a URL or a 'mailto:' address.">
2247
</div>
48+
<div class="form-row">
49+
<label>&nbsp;</label>
50+
<button id="node-input-generateKeyPair"><i class="fa fa-exchange"></i> Generate VAPID keypair</button>
51+
</div>
2352
<div class="form-row">
2453
<label for="node-config-input-publicKey"><i class="icon-tag"></i> Public Key</label>
2554
<input type="text" id="node-config-input-publicKey" placeholder="The public VAPID key">
@@ -39,6 +68,9 @@
3968
</script>
4069

4170
<script type="text/x-red" data-help-name="vapid-configuration">
42-
<p>Configuration for VAPID. You can generate the key pair using <code>generateVAPIDKeys()</code> method of <a href="https://www.npmjs.com/package/web-push" target="_blank">web-push</a> library or online here: <a href="https://web-push-codelab.glitch.me/" target="_blank">https://web-push-codelab.glitch.me</a>.</p>
71+
<p>Configuration for VAPID. You can generate the key pair using the <i>"Generate VAPID keypair"</i> button or online here: <a href="https://web-push-codelab.glitch.me/" target="_blank">https://web-push-codelab.glitch.me</a>.</p>
72+
<p>Read more about the VAPID specification here: <a href="https://tools.ietf.org/html/rfc8292" target="_blank">https://tools.ietf.org/html/rfc8292</a>.</p>
4373
<p>For Chrome prior to version 52 and some old browsers, you're also still required to include a <code>gcm_sender_id</code> in your web app's manifest.json.</p>
44-
</script>
74+
</script>
75+
76+

vapid-configuration.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,33 @@ module.exports = function (RED) {
88
}
99
RED.nodes.registerType('vapid-configuration', VapidConfigurationNode)
1010
}
11+
module.exports = function (RED) {
12+
const webpush = require('web-push');
13+
14+
function VapidConfigurationNode (config) {
15+
RED.nodes.createNode(this, config)
16+
this.subject = config.subject
17+
this.publicKey = config.publicKey
18+
this.privateKey = config.privateKey
19+
this.gcmApiKey = config.gcmApiKey
20+
}
21+
RED.nodes.registerType('vapid-configuration', VapidConfigurationNode)
22+
23+
// Make the key pair generation available to the config screen (in the flow editor)
24+
RED.httpAdmin.get('/vapid_configuration/generate_key_pair', RED.auth.needsPermission('vapid-configuration.write'), async function(req, res){
25+
try {
26+
// Generate a VAPID keypair
27+
const vapidKeys = webpush.generateVAPIDKeys();
28+
29+
// Return public key and private key to the config screen (since they need to be stored in the node's credentials)
30+
res.json({
31+
publicKey: vapidKeys.publicKey,
32+
privateKey: vapidKeys.privateKey
33+
})
34+
}
35+
catch (err) {
36+
console.log("Error while generating VAPID keypair: " + err)
37+
res.status(500).json({error: 'Error while generating VAPID keypair'})
38+
}
39+
});
40+
}

0 commit comments

Comments
 (0)