diff --git a/INSTALL.md b/INSTALL.md index f19025d..919743f 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -187,10 +187,10 @@ sudo npm install -g web-push web-push generate-vapid-keys ``` -The public Key needs to be copied into `web/main.js` (first line), while the -private key is put into the config option `push_private_key`. +The keys then need to be copied into the config options `push_public_key` and +`push_private_key` respectively so the backend can handle the rest. -Then, to be VAPID compliant you have to announce an contact address claim to +Next, to be VAPID compliant you have to announce an contact address claim to the push services so they can contact you if anything is going wrong with your notifications. Do this by entering your email address as a `mailto:` link in the `push_sender_info` option, like `mailto:it@fasttube.de`. diff --git a/config.ini b/config.ini index 76bc5fb..3eee4db 100644 --- a/config.ini +++ b/config.ini @@ -26,8 +26,10 @@ guideline_url = https://fasttube.de/wp-content/uploads/2020/12/Cororna-Regeln-St json_indent = 4 # VAPID credentials for push notifications +# private key: base64url encoded public part of an EC-Prime256v1 keypair. See INSTALL.md # private key: base64url encoded private part of an EC-Prime256v1 keypair. See INSTALL.md # sender info: usually mailto link to responsible party to contact about issues +push_public_key = BBwBPYxhogHLU3B1FpxfQNzO3q7qZpmD1n1KaaL8WJbcVmJSHhi1uB-VmvsVjjUHWYCeqKyLT7w-1LBfpIcbbcg push_private_key = abcdefghijklm_NOPQRSTUVWXYZ-0123456789 push_sender_info = mailto:it@fasttube.de # when to notify users, in hours after arrival diff --git a/ftracker/core.py b/ftracker/core.py index 8c79207..9a61240 100644 --- a/ftracker/core.py +++ b/ftracker/core.py @@ -171,6 +171,25 @@ def get_data(): return json.dumps(r, indent=SPACES), 200 +@app.route('/pushinfo') +def get_pushinfo(): + + if config['notify_after_hrs']: + + r = { + 'enabled': True, + 'publickey': config['push_public_key'] + } + + else: + + r = { + 'enabled': False, + 'publickey': None + } + + return json.dumps(r, indent=SPACES), 200 + @app.route('/pushsubscribe', methods=['POST']) def post_pushsub(): diff --git a/res/config.deploy.ini b/res/config.deploy.ini index 4afb438..07ba13e 100644 --- a/res/config.deploy.ini +++ b/res/config.deploy.ini @@ -26,8 +26,10 @@ guideline_url = https://youtu.be/oHg5SJYRHA0 json_indent = 4 # VAPID credentials for push notifications +# private key: base64url encoded public part of an EC-Prime256v1 keypair. See INSTALL.md # private key: base64url encoded private part of an EC-Prime256v1 keypair. See INSTALL.md # sender info: usually mailto link to responsible party to contact about issues +push_public_key = abcdefghijklm_NOPQRSTUVWXYZ-0123456789abcdefghijklm_NOPQRSTUVWXYZ-0123456789abcdefghijklm_NOPQRSTUVWXYZ-0123456789 push_private_key = abcdefghijklm_NOPQRSTUVWXYZ-0123456789 push_sender_info = mailto:admin@example.com # when to notify users, in hours after arrival diff --git a/res/docker-entrypoint.sh b/res/docker-entrypoint.sh index b1e6713..209b90f 100644 --- a/res/docker-entrypoint.sh +++ b/res/docker-entrypoint.sh @@ -10,11 +10,10 @@ then web-push generate-vapid-keys --json > $VAPID_CREDS_FILE - echo "Patching public key into frontend ..." + echo "Patching keypair into config ..." PUB_KEY=`cat $VAPID_CREDS_FILE | jq -r .publicKey` - sed -i "s/pushServerPublicKey = '[a-zA-Z0-9_\-]*'/pushServerPublicKey = '${PUB_KEY}'/" /var/www/html/ftracker/main.js + echo "pushServerPublicKey = ${PUB_KEY}" >> /var/www/html/ftracker/main.js - echo "Patching private key into backend config ..." PRIV_KEY=`cat $VAPID_CREDS_FILE | jq -r .privateKey` echo "push_private_key = ${PRIV_KEY}" >> /etc/ftracker/config.ini diff --git a/web/main.js b/web/main.js index 0ee42c9..d97172f 100644 --- a/web/main.js +++ b/web/main.js @@ -1,5 +1,3 @@ -var pushServerPublicKey = 'BBwBPYxhogHLU3B1FpxfQNzO3q7qZpmD1n1KaaL8WJbcVmJSHhi1uB-VmvsVjjUHWYCeqKyLT7w-1LBfpIcbbcg' - var spage = document.getElementById('startpage') var mform = document.getElementById('mainform') @@ -226,6 +224,18 @@ function initPush(name) { return } + fetch('/pushinfo').then(function(res) { + if (res.ok) + res.json().then(function(push) { + if (push.enabled) + registerPush(name, push.publickey); + }); + }); + +} + +function registerPush(name, pushServerPublicKey) { + // Register service worker navigator.serviceWorker.register("/sw.js").then(function(swRegistration) { console.log("ServiceWorker registered:", swRegistration)