Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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
* Not necessary to be JavaScript server, choose your favorite language ;)
*/
const http = require("http"),
https = require("https"),
fs = require("fs");
// A library for handling VAPID and encrypting of Web-Push-Protocol.
// See https://github.com/web-push-libs/web-push for more information.
const webpush = require("web-push");
// "./application-server-keys.json" should be automatically generated by "postinstall"
const keys = require("./application-server-keys.json");
// Initialize library with **YOUR** project keys and settings.
webpush.setVapidDetails(
process.env.SENDER_EMAIL || "mailto:otiai10@gmail.com",
keys.publicKey,
keys.privateKey
);
const subscribers = [
// Here would be any subscribers endpoints,
// therefore this is gonna be a list of, so called, "target list".
// You should sotre this list to your database to make it persistent.
];
const controllers = {
/**
* ============================
* == THIS IS NOT IMPORTANT! ==
* ============================
const sendPushNotification = (root, args, context) => {
const { user } = context;
Users.utils.checkLoggedInAndVerified(user);
// Set web-push keys
webPush.setGCMAPIKey(gcmPrivateKey);
webPush.setVapidDetails(vapidSubject, vapidPublicKey, vapidPrivateKey);
const payload = JSON.stringify({
title: 'Welcome',
body: 'Thank you for enabling push notifications',
icon: '/android-chrome-192x192.png',
});
const options = {
TTL: 60, // time to live in seconds
};
// Gather all subscriptions from all subscribed users
const selector = { subscriptions: { $exists: true, $ne: [] } };
const projection = { fields: { _id: true, subscriptions: true } };
const users = Users.collection.find(selector, projection).fetch();
const subscriptions = flatten(map(users, 'subscriptions'));
function init(cb) {
web_push_1.setVapidDetails('mailto:bryan@nebri.us', util_1.getEnvironmentVariable('PUSH_PUBLIC_KEY'), util_1.getEnvironmentVariable('PUSH_PRIVATE_KEY'));
console.log('Notifications initialied');
setImmediate(cb);
}
exports.init = init;
public setup(httpServer: HttpServer): void {
webpush.setVapidDetails(
'mailto:example@yourdomain.org',
this.vapidKeys.public,
this.vapidKeys.private
);
httpServer.post('/push/register', this._register.bind(this));
httpServer.post('/push/notifyAll', this._notifyAll.bind(this));
httpServer.get('/push/clear', this._clearSubscriptions.bind(this));
}
.then(function(){
webpush.setVapidDetails('mailto:monkey030210@gmail.com',
vapidKeys.publicKey,
vapidKeys.privateKey
);
return admin.database().ref('subscriptions').once('value');
})
.then(function(subs){