Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const webPush = require('web-push');
const args = require('minimist')(process.argv.slice(2));
if (!args.subscription || !args.payload) {
throw new Error('Bad Input - Sorry. Include --subscription and --payload');
}
// Just makes final output a little cleaner
console.log('');
console.log('');
const subscription = JSON.parse(args.subscription);
const payload = args.payload;
webPush.setGCMAPIKey('AIzaSyBBh4ddPa96rQQNxqiq_qQj7sq1JdsNQUQ');
return webPush.sendNotification(subscription.endpoint, {
userPublicKey: subscription.keys.p256dh,
userAuth: subscription.keys.auth,
payload: payload
})
.then(result => {
console.log('Result: ', result);
})
.catch(err => {
console.log(err);
})
.then(() => {
// Just makes final output a little cleaner
console.log('');
console.log('');
subscriptions.forEach((subscription) => {
webPush.sendNotification(subscription, payload, options)
.then(() => {})
.catch((err) => {
console.log(`Error when trying to deliver message for ${subscription.endpoint}`, err);
// This is probably an old subscription, remove it
deleteSubscription(null, { endpoint: subscription.endpoint }, { user });
});
});
};
webpush.setVapidDetails(
SERVER_SUBJECT,
pushKeys.pushVapidKeys.publicKey,
pushKeys.pushVapidKeys.privateKey,
);
// This is the same output of calling JSON.stringify on a PushSubscription
const pushSubscription = {
endpoint: endpoint,
keys: {
auth: keys.auth,
p256dh: keys.p256dh,
},
};
webpush.sendNotification(pushSubscription, payload)
.then((webPushResult) => {
return resolve(webPushResult);
}).catch((webPushError) => {
console.log(`push failed to ${endpoint}\n ${webPushError}`);
return reject(webPushError);
});
});
}
// Use the web-push library to hide the implementation details of the communication
// between the application server and the push service.
// For details, see https://tools.ietf.org/html/draft-ietf-webpush-protocol and
// https://tools.ietf.org/html/draft-ietf-webpush-encryption.
const webPush = require('web-push');
if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
"environment variables. You can use the following ones:");
console.log(webPush.generateVAPIDKeys());
return;
}
// Set the keys used for encrypting the push messages.
webPush.setVapidDetails(
'https://serviceworke.rs/',
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
const payloads = {};
module.exports = function(app, route) {
app.get(route + 'vapidPublicKey', function(req, res) {
res.send(process.env.VAPID_PUBLIC_KEY);
});
app.post(route + 'register', function(req, res) {
// A real world application would store the subscription info.
res.sendStatus(201);
});
})
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
app.use(function (req, res, next) {
res.locals.sessionFlash = req.session.sessionFlash;
delete req.session.sessionFlash;
next();
});
//set up webpush to send push notifications for the notifier
webpush = require('web-push');
if (!auth.vapidPrivateKey || !auth.vapidPublicKey) {
vapidKeys = webpush.generateVAPIDKeys();
webpush.setVapidDetails(
'mailto:support@sweet.sh',
vapidKeys.publicKey,
vapidKeys.privateKey
);
} else {
webpush.setVapidDetails(
'mailto:support@sweet.sh',
auth.vapidPublicKey,
auth.vapidPrivateKey
);
}
//kill the process when the sigint code is recieved, generally generated by pressing ctrl-c in the console
app.on('SIGINT', function () {
db.stop(function (err) {
process.exit(err ? 1 : 0);
var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
app.options('*', cors());
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var PORT = process.env.PORT || 8090;
var webPush = require('web-push');
// The GCM API key is AIzaSyDNlm9R_w_0FDGjSM1fzyx5I5JnJBXACqU
webPush.setVapidDetails(
'mailto:salnikov@gmail.com',
'BHe82datFpiOOT0k3D4pieGt1GU-xx8brPjBj0b22gvmwl-HLD1vBOP1AxlDKtwYUQiS9S-SDVGYe_TdZrYJLw8',
's-zBxZ1Kl_Y1Ac8_uBjwIjtLtG6qlJKOX5trtbanAhc'
);
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use("/push", function(req, res, next) {
//console.log(res.body);
if (req.body.action === 'subscribe') {
var endpoint = req.body.subscription;
console.log(req);
let privateKey = process.env.VAPID_PRIVATE_KEY
let publicKey = process.env.VAPID_PUBLIC_KEY
const linkingNotifyEndpoint = process.env.LINKING_NOTIFY_ENDPOINT
const linkingNotifyToken = process.env.LINKING_NOTIFY_TOKEN
const dappOfferUrl = process.env.DAPP_OFFER_URL
if (!privateKey || !publicKey) {
console.log(
'Warning: VAPID public or private key not defined, generating one'
)
const vapidKeys = webpush.generateVAPIDKeys()
publicKey = vapidKeys.publicKey
privateKey = vapidKeys.privateKey
}
webpush.setVapidDetails(`mailto:${emailAddress}`, publicKey, privateKey)
// should be tightened up for security
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
)
next()
})
// limit request to one per minute
const rateLimiterOptions = {
points: 1,
duration: 60
// Use the web-push library to hide the implementation details of the communication
// between the application server and the push service.
// For details, see https://tools.ietf.org/html/draft-ietf-webpush-protocol and
// https://tools.ietf.org/html/draft-ietf-webpush-encryption.
const webPush = require('web-push');
if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
"environment variables. You can use the following ones:");
console.log(webPush.generateVAPIDKeys());
return;
}
// Set the keys used for encrypting the push messages.
webPush.setVapidDetails(
'https://serviceworke.rs/',
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
module.exports = function(app, route) {
app.get(route + 'vapidPublicKey', function(req, res) {
res.send(process.env.VAPID_PUBLIC_KEY);
});
app.post(route + 'register', function(req, res) {
// A real world application would store the subscription info.
// Use the web-push library to hide the implementation details of the communication
// between the application server and the push service.
// For details, see https://tools.ietf.org/html/draft-ietf-webpush-protocol and
// https://tools.ietf.org/html/draft-ietf-webpush-encryption.
const webPush = require('web-push');
if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
"environment variables. You can use the following ones:");
console.log(webPush.generateVAPIDKeys());
return;
}
// Set the keys used for encrypting the push messages.
webPush.setVapidDetails(
'https://serviceworke.rs/',
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
module.exports = function(app, route) {
app.get(route + 'vapidPublicKey', function(req, res) {
res.send(process.env.VAPID_PUBLIC_KEY);
});
app.post(route + 'register', function(req, res) {
// A real world application would store the subscription info.
// Use the web-push library to hide the implementation details of the communication
// between the application server and the push service.
// For details, see https://tools.ietf.org/html/draft-ietf-webpush-protocol and
// https://tools.ietf.org/html/draft-ietf-webpush-encryption.
const webPush = require('web-push');
if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
"environment variables. You can use the following ones:");
console.log(webPush.generateVAPIDKeys());
return;
}
// Set the keys used for encrypting the push messages.
webPush.setVapidDetails(
'https://serviceworke.rs/',
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
module.exports = function(app, route) {
app.get(route + 'vapidPublicKey', function(req, res) {
res.send(process.env.VAPID_PUBLIC_KEY);
});
app.post(route + 'register', function(req, res) {
// A real world application would store the subscription info.