Skip to content

Commit 3f93c0b

Browse files
authored
add documentation for setVapidDetails method (#787)
* add documentation for setVapidDetails method document the setVapidDetails method add warning about Safari and localhost URIs as VAPID subject * add check for https://localhost to VAPID subject validator emit sensible warning to user when localhost URI is detected
1 parent 13606eb commit 3f93c0b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ that may either be a string URI of the proxy server (eg. http://< hostname >:< p
193193
or an "options" object with more specific properties.
194194
- **agent** is the [HTTPS Agent instance](https://nodejs.org/dist/latest/docs/api/https.html#https_class_https_agent) which will be used in the `https.request` method. If the `proxy` options defined, `agent` will be ignored!
195195

196+
> **Note:** As of this writing, if a push notification request contains a VAPID `subject` referencing an `https://localhost` URI (set either using the `options` argument or via the global `setVapidDetails()` method), Safari's push notification endpoint rejects the request with a `BadJwtToken` error.
197+
196198
### Returns
197199

198200
A promise that resolves if the notification was sent successfully
@@ -250,6 +252,32 @@ None.
250252

251253
<hr />
252254

255+
## setVapidDetails(subject, publicKey, privateKey)
256+
257+
```javascript
258+
webpush.setVapidDetails(
259+
260+
process.env.VAPID_PUBLIC_KEY,
261+
process.env.VAPID_PRIVATE_KEY
262+
);
263+
```
264+
265+
Globally sets the application's VAPID subject, public key, and private key, to be used in subsequent calls to `sendNotification()` and `generateRequestDetails()` that don't specifically override them in their `options` argument.
266+
267+
### Input
268+
269+
The `setVapidDetails` method expects the following input:
270+
271+
- *subject*: the VAPID server contact information, as either an `https:` or `mailto:` URI ([as per the VAPID spec](https://datatracker.ietf.org/doc/html/draft-thomson-webpush-vapid#section-2.1)).
272+
- *publicKey*: the VAPID public key.
273+
- *privateKey*: the VAPID private key.
274+
275+
### Returns
276+
277+
None.
278+
279+
<hr />
280+
253281
## encrypt(userPublicKey, userAuth, payload, contentEncoding)
254282

255283
```javascript

src/vapid-helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ function validateSubject(subject) {
7979
const subjectParseResult = url.parse(subject);
8080
if (!subjectParseResult.hostname) {
8181
throw new Error('Vapid subject is not a url or mailto url. ' + subject);
82+
} else if (subjectParseResult.hostname === 'localhost' && subjectParseResult.protocol === 'https:') {
83+
console.warn('VAPID subject points to a localhost web URI, which is unsupported by Apple\'s push notification server and will result in a BadJwtToken error when sending notifications.');
8284
}
8385
}
8486
}

0 commit comments

Comments
 (0)