@@ -32,38 +32,12 @@ function generateVAPIDKeys() {
32
32
curve . generateKeys ( ) ;
33
33
34
34
return {
35
- publicKey : curve . getPublicKey ( ) ,
36
- privateKey : curve . getPrivateKey ( )
35
+ publicKey : urlBase64 . encode ( curve . getPublicKey ( ) ) ,
36
+ privateKey : urlBase64 . encode ( curve . getPrivateKey ( ) )
37
37
} ;
38
38
}
39
39
40
- /**
41
- * This method takes the required VAPID parameters and returns the required
42
- * header to be added to a Web Push Protocol Request.
43
- * @param {string } audience This must be the origin of the push service.
44
- * @param {string } subject This should be a URL or a 'mailto:' email
45
- * address.
46
- * @param {Buffer } publicKey The VAPID public key.
47
- * @param {Buffer } privateKey The VAPID private key.
48
- * @param {integer } [expiration] The expiration of the VAPID JWT.
49
- * @return {Object } Returns an Object with the Authorization and
50
- * 'Crypto-Key' values to be used as headers.
51
- */
52
- function getVapidHeaders ( audience , subject , publicKey , privateKey , expiration ) {
53
- if ( ! audience ) {
54
- throw new Error ( 'No audience set in vapid.audience.' ) ;
55
- }
56
-
57
- if ( typeof audience !== 'string' || audience . length === 0 ) {
58
- throw new Error ( 'The audience value must be a string containing the ' +
59
- 'origin of a push service. ' + audience ) ;
60
- }
61
-
62
- const audienceParseResult = url . parse ( audience ) ;
63
- if ( ! audienceParseResult . hostname ) {
64
- throw new Error ( 'VAPID audience is not a url. ' + audience ) ;
65
- }
66
-
40
+ function validateSubject ( subject ) {
67
41
if ( ! subject ) {
68
42
throw new Error ( 'No subject set in vapid.subject.' ) ;
69
43
}
@@ -79,30 +53,76 @@ function getVapidHeaders(audience, subject, publicKey, privateKey, expiration) {
79
53
throw new Error ( 'Vapid subject is not a url or mailto url. ' + subject ) ;
80
54
}
81
55
}
56
+ }
82
57
58
+ function validatePublicKey ( publicKey ) {
83
59
if ( ! publicKey ) {
84
60
throw new Error ( 'No key set vapid.publicKey' ) ;
85
61
}
86
62
87
- if ( ! ( publicKey instanceof Buffer ) ) {
88
- throw new Error ( 'Vapid public key is not a buffer.' ) ;
63
+ if ( typeof publicKey !== 'string' ) {
64
+ throw new Error ( 'Vapid public key is must be a URL safe Base 64 ' +
65
+ 'encoded string.' ) ;
89
66
}
90
67
68
+ publicKey = urlBase64 . decode ( publicKey ) ;
69
+
91
70
if ( publicKey . length !== 65 ) {
92
- throw new Error ( 'Vapid public key should be 65 bytes long' ) ;
71
+ throw new Error ( 'Vapid public key should be 65 bytes long when decoded. ' ) ;
93
72
}
73
+ }
94
74
75
+ function validatePrivateKey ( privateKey ) {
95
76
if ( ! privateKey ) {
96
77
throw new Error ( 'No key set in vapid.privateKey' ) ;
97
78
}
98
79
99
- if ( ! ( privateKey instanceof Buffer ) ) {
100
- throw new Error ( 'Vapid private key is not a buffer' ) ;
80
+ if ( typeof privateKey !== 'string' ) {
81
+ throw new Error ( 'Vapid private key must be a URL safe Base 64 ' +
82
+ 'encoded string.' ) ;
101
83
}
102
84
85
+ privateKey = urlBase64 . decode ( privateKey ) ;
86
+
103
87
if ( privateKey . length !== 32 ) {
104
- throw new Error ( 'Vapid private key should be 32 bytes long' ) ;
88
+ throw new Error ( 'Vapid private key should be 32 bytes long when decoded. ' ) ;
105
89
}
90
+ }
91
+
92
+ /**
93
+ * This method takes the required VAPID parameters and returns the required
94
+ * header to be added to a Web Push Protocol Request.
95
+ * @param {string } audience This must be the origin of the push service.
96
+ * @param {string } subject This should be a URL or a 'mailto:' email
97
+ * address.
98
+ * @param {Buffer } publicKey The VAPID public key.
99
+ * @param {Buffer } privateKey The VAPID private key.
100
+ * @param {integer } [expiration] The expiration of the VAPID JWT.
101
+ * @return {Object } Returns an Object with the Authorization and
102
+ * 'Crypto-Key' values to be used as headers.
103
+ */
104
+ function getVapidHeaders ( audience , subject , publicKey , privateKey , expiration ) {
105
+ if ( ! audience ) {
106
+ throw new Error ( 'No audience set in vapid.audience.' ) ;
107
+ }
108
+
109
+ if ( typeof audience !== 'string' || audience . length === 0 ) {
110
+ throw new Error ( 'The audience value must be a string containing the ' +
111
+ 'origin of a push service. ' + audience ) ;
112
+ }
113
+
114
+ const audienceParseResult = url . parse ( audience ) ;
115
+ if ( ! audienceParseResult . hostname ) {
116
+ throw new Error ( 'VAPID audience is not a url. ' + audience ) ;
117
+ }
118
+
119
+ validateSubject ( subject ) ;
120
+ validatePublicKey ( publicKey ) ;
121
+ validatePrivateKey ( privateKey ) ;
122
+
123
+ publicKey = urlBase64 . decode ( publicKey ) ;
124
+ privateKey = urlBase64 . decode ( privateKey ) ;
125
+
106
126
107
127
if ( expiration ) {
108
128
// TODO: Check if expiration is valid and use it in place of the hard coded
@@ -134,5 +154,8 @@ function getVapidHeaders(audience, subject, publicKey, privateKey, expiration) {
134
154
135
155
module . exports = {
136
156
generateVAPIDKeys : generateVAPIDKeys ,
137
- getVapidHeaders : getVapidHeaders
157
+ getVapidHeaders : getVapidHeaders ,
158
+ validateSubject : validateSubject ,
159
+ validatePublicKey : validatePublicKey ,
160
+ validatePrivateKey : validatePrivateKey
138
161
} ;
0 commit comments