-
Notifications
You must be signed in to change notification settings - Fork 0
/
awtsmoosPush.js
54 lines (44 loc) · 1.28 KB
/
awtsmoosPush.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//B"H
const crypto = require('crypto');
class AwtsmoosPush {
constructor() {
}
setVapidDetails(
website,
VAPID_PUBLIC_KEY,
VAPID_PRIVATE_KEY
) {
this.website = website;
this.VAPID_PUBLIC_KEY = VAPID_PUBLIC_KEY;
this.VAPID_PRIVATE_KEY = VAPID_PRIVATE_KEY;
}
async sendNotification(subscription, payload, options) {
}
async makeJWS(subscription) {
var {endpoint, keys} = subscription;
var {p256dh} = keys;
var header = {
"typ": "JWT",
"alg": "ES256"
}
var payload = {
"aud": (new URL(endpoint)).origin,
"exp": Math.floor(Date.now() / 1000) + 12 * 60 * 60,
"sub": this.website
}
var urlSafeH = this.toUrlSafeBase64(header);
var urlSafePayload = this.toUrlSafeBase64(payload);
var unsigned = `${urlSafeH}.${urlSafePayload}`
}
toUrlSafeBase64(inputString) {
// Create a buffer from the input string
const base64Encoded = Buffer.from(inputString).toString('base64');
// Replace the characters for URL safety
const urlSafeBase64 = base64Encoded
.replace(/\+/g, '-') // Replace '+' with '-'
.replace(/\//g, '_') // Replace '/' with '_'
.replace(/=+$/, ''); // Remove trailing '=' padding
return urlSafeBase64;
}
}
module.exports = AwtsmoosPush;