@certd/acme-client 0.1.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/api.js CHANGED
@@ -1,243 +1,243 @@
1
- /**
2
- * ACME API client
3
- */
4
-
5
- const util = require('./util');
6
-
7
-
8
- /**
9
- * AcmeApi
10
- *
11
- * @class
12
- * @param {HttpClient} httpClient
13
- */
14
-
15
- class AcmeApi {
16
- constructor(httpClient, accountUrl = null) {
17
- this.http = httpClient;
18
- this.accountUrl = accountUrl;
19
- }
20
-
21
-
22
- /**
23
- * Get account URL
24
- *
25
- * @private
26
- * @returns {string} Account URL
27
- */
28
-
29
- getAccountUrl() {
30
- if (!this.accountUrl) {
31
- throw new Error('No account URL found, register account first');
32
- }
33
-
34
- return this.accountUrl;
35
- }
36
-
37
-
38
- /**
39
- * ACME API request
40
- *
41
- * @private
42
- * @param {string} url Request URL
43
- * @param {object} [payload] Request payload, default: `null`
44
- * @param {array} [validStatusCodes] Array of valid HTTP response status codes, default: `[]`
45
- * @param {boolean} [jwsKid] Use KID in JWS header, default: `true`
46
- * @returns {Promise<object>} HTTP response
47
- */
48
-
49
- async apiRequest(url, payload = null, validStatusCodes = [], jwsKid = true) {
50
- const kid = jwsKid ? this.getAccountUrl() : null;
51
- const resp = await this.http.signedRequest(url, payload, kid);
52
-
53
- if (validStatusCodes.length && (validStatusCodes.indexOf(resp.status) === -1)) {
54
- throw new Error(util.formatResponseError(resp));
55
- }
56
-
57
- return resp;
58
- }
59
-
60
-
61
- /**
62
- * ACME API request by resource name helper
63
- *
64
- * @private
65
- * @param {string} resource Request resource name
66
- * @param {object} [payload] Request payload, default: `null`
67
- * @param {array} [validStatusCodes] Array of valid HTTP response status codes, default: `[]`
68
- * @param {boolean} [jwsKid] Use KID in JWS header, default: `true`
69
- * @returns {Promise<object>} HTTP response
70
- */
71
-
72
- async apiResourceRequest(resource, payload = null, validStatusCodes = [], jwsKid = true) {
73
- const resourceUrl = await this.http.getResourceUrl(resource);
74
- return this.apiRequest(resourceUrl, payload, validStatusCodes, jwsKid);
75
- }
76
-
77
-
78
- /**
79
- * Get Terms of Service URL if available
80
- *
81
- * https://tools.ietf.org/html/rfc8555#section-7.1.1
82
- *
83
- * @returns {Promise<string|null>} ToS URL
84
- */
85
-
86
- async getTermsOfServiceUrl() {
87
- return this.http.getMetaField('termsOfService');
88
- }
89
-
90
-
91
- /**
92
- * Create new account
93
- *
94
- * https://tools.ietf.org/html/rfc8555#section-7.3
95
- *
96
- * @param {object} data Request payload
97
- * @returns {Promise<object>} HTTP response
98
- */
99
-
100
- async createAccount(data) {
101
- const resp = await this.apiResourceRequest('newAccount', data, [200, 201], false);
102
-
103
- /* Set account URL */
104
- if (resp.headers.location) {
105
- this.accountUrl = resp.headers.location;
106
- }
107
-
108
- return resp;
109
- }
110
-
111
-
112
- /**
113
- * Update account
114
- *
115
- * https://tools.ietf.org/html/rfc8555#section-7.3.2
116
- *
117
- * @param {object} data Request payload
118
- * @returns {Promise<object>} HTTP response
119
- */
120
-
121
- updateAccount(data) {
122
- return this.apiRequest(this.getAccountUrl(), data, [200, 202]);
123
- }
124
-
125
-
126
- /**
127
- * Update account key
128
- *
129
- * https://tools.ietf.org/html/rfc8555#section-7.3.5
130
- *
131
- * @param {object} data Request payload
132
- * @returns {Promise<object>} HTTP response
133
- */
134
-
135
- updateAccountKey(data) {
136
- return this.apiResourceRequest('keyChange', data, [200]);
137
- }
138
-
139
-
140
- /**
141
- * Create new order
142
- *
143
- * https://tools.ietf.org/html/rfc8555#section-7.4
144
- *
145
- * @param {object} data Request payload
146
- * @returns {Promise<object>} HTTP response
147
- */
148
-
149
- createOrder(data) {
150
- return this.apiResourceRequest('newOrder', data, [201]);
151
- }
152
-
153
-
154
- /**
155
- * Get order
156
- *
157
- * https://tools.ietf.org/html/rfc8555#section-7.4
158
- *
159
- * @param {string} url Order URL
160
- * @returns {Promise<object>} HTTP response
161
- */
162
-
163
- getOrder(url) {
164
- return this.apiRequest(url, null, [200]);
165
- }
166
-
167
-
168
- /**
169
- * Finalize order
170
- *
171
- * https://tools.ietf.org/html/rfc8555#section-7.4
172
- *
173
- * @param {string} url Finalization URL
174
- * @param {object} data Request payload
175
- * @returns {Promise<object>} HTTP response
176
- */
177
-
178
- finalizeOrder(url, data) {
179
- return this.apiRequest(url, data, [200]);
180
- }
181
-
182
-
183
- /**
184
- * Get identifier authorization
185
- *
186
- * https://tools.ietf.org/html/rfc8555#section-7.5
187
- *
188
- * @param {string} url Authorization URL
189
- * @returns {Promise<object>} HTTP response
190
- */
191
-
192
- getAuthorization(url) {
193
- return this.apiRequest(url, null, [200]);
194
- }
195
-
196
-
197
- /**
198
- * Update identifier authorization
199
- *
200
- * https://tools.ietf.org/html/rfc8555#section-7.5.2
201
- *
202
- * @param {string} url Authorization URL
203
- * @param {object} data Request payload
204
- * @returns {Promise<object>} HTTP response
205
- */
206
-
207
- updateAuthorization(url, data) {
208
- return this.apiRequest(url, data, [200]);
209
- }
210
-
211
-
212
- /**
213
- * Complete challenge
214
- *
215
- * https://tools.ietf.org/html/rfc8555#section-7.5.1
216
- *
217
- * @param {string} url Challenge URL
218
- * @param {object} data Request payload
219
- * @returns {Promise<object>} HTTP response
220
- */
221
-
222
- completeChallenge(url, data) {
223
- return this.apiRequest(url, data, [200]);
224
- }
225
-
226
-
227
- /**
228
- * Revoke certificate
229
- *
230
- * https://tools.ietf.org/html/rfc8555#section-7.6
231
- *
232
- * @param {object} data Request payload
233
- * @returns {Promise<object>} HTTP response
234
- */
235
-
236
- revokeCert(data) {
237
- return this.apiResourceRequest('revokeCert', data, [200]);
238
- }
239
- }
240
-
241
-
242
- /* Export API */
243
- module.exports = AcmeApi;
1
+ /**
2
+ * ACME API client
3
+ */
4
+
5
+ const util = require('./util');
6
+
7
+
8
+ /**
9
+ * AcmeApi
10
+ *
11
+ * @class
12
+ * @param {HttpClient} httpClient
13
+ */
14
+
15
+ class AcmeApi {
16
+ constructor(httpClient, accountUrl = null) {
17
+ this.http = httpClient;
18
+ this.accountUrl = accountUrl;
19
+ }
20
+
21
+
22
+ /**
23
+ * Get account URL
24
+ *
25
+ * @private
26
+ * @returns {string} Account URL
27
+ */
28
+
29
+ getAccountUrl() {
30
+ if (!this.accountUrl) {
31
+ throw new Error('No account URL found, register account first');
32
+ }
33
+
34
+ return this.accountUrl;
35
+ }
36
+
37
+
38
+ /**
39
+ * ACME API request
40
+ *
41
+ * @private
42
+ * @param {string} url Request URL
43
+ * @param {object} [payload] Request payload, default: `null`
44
+ * @param {array} [validStatusCodes] Array of valid HTTP response status codes, default: `[]`
45
+ * @param {boolean} [jwsKid] Use KID in JWS header, default: `true`
46
+ * @returns {Promise<object>} HTTP response
47
+ */
48
+
49
+ async apiRequest(url, payload = null, validStatusCodes = [], jwsKid = true) {
50
+ const kid = jwsKid ? this.getAccountUrl() : null;
51
+ const resp = await this.http.signedRequest(url, payload, kid);
52
+
53
+ if (validStatusCodes.length && (validStatusCodes.indexOf(resp.status) === -1)) {
54
+ throw new Error(util.formatResponseError(resp));
55
+ }
56
+
57
+ return resp;
58
+ }
59
+
60
+
61
+ /**
62
+ * ACME API request by resource name helper
63
+ *
64
+ * @private
65
+ * @param {string} resource Request resource name
66
+ * @param {object} [payload] Request payload, default: `null`
67
+ * @param {array} [validStatusCodes] Array of valid HTTP response status codes, default: `[]`
68
+ * @param {boolean} [jwsKid] Use KID in JWS header, default: `true`
69
+ * @returns {Promise<object>} HTTP response
70
+ */
71
+
72
+ async apiResourceRequest(resource, payload = null, validStatusCodes = [], jwsKid = true) {
73
+ const resourceUrl = await this.http.getResourceUrl(resource);
74
+ return this.apiRequest(resourceUrl, payload, validStatusCodes, jwsKid);
75
+ }
76
+
77
+
78
+ /**
79
+ * Get Terms of Service URL if available
80
+ *
81
+ * https://tools.ietf.org/html/rfc8555#section-7.1.1
82
+ *
83
+ * @returns {Promise<string|null>} ToS URL
84
+ */
85
+
86
+ async getTermsOfServiceUrl() {
87
+ return this.http.getMetaField('termsOfService');
88
+ }
89
+
90
+
91
+ /**
92
+ * Create new account
93
+ *
94
+ * https://tools.ietf.org/html/rfc8555#section-7.3
95
+ *
96
+ * @param {object} data Request payload
97
+ * @returns {Promise<object>} HTTP response
98
+ */
99
+
100
+ async createAccount(data) {
101
+ const resp = await this.apiResourceRequest('newAccount', data, [200, 201], false);
102
+
103
+ /* Set account URL */
104
+ if (resp.headers.location) {
105
+ this.accountUrl = resp.headers.location;
106
+ }
107
+
108
+ return resp;
109
+ }
110
+
111
+
112
+ /**
113
+ * Update account
114
+ *
115
+ * https://tools.ietf.org/html/rfc8555#section-7.3.2
116
+ *
117
+ * @param {object} data Request payload
118
+ * @returns {Promise<object>} HTTP response
119
+ */
120
+
121
+ updateAccount(data) {
122
+ return this.apiRequest(this.getAccountUrl(), data, [200, 202]);
123
+ }
124
+
125
+
126
+ /**
127
+ * Update account key
128
+ *
129
+ * https://tools.ietf.org/html/rfc8555#section-7.3.5
130
+ *
131
+ * @param {object} data Request payload
132
+ * @returns {Promise<object>} HTTP response
133
+ */
134
+
135
+ updateAccountKey(data) {
136
+ return this.apiResourceRequest('keyChange', data, [200]);
137
+ }
138
+
139
+
140
+ /**
141
+ * Create new order
142
+ *
143
+ * https://tools.ietf.org/html/rfc8555#section-7.4
144
+ *
145
+ * @param {object} data Request payload
146
+ * @returns {Promise<object>} HTTP response
147
+ */
148
+
149
+ createOrder(data) {
150
+ return this.apiResourceRequest('newOrder', data, [201]);
151
+ }
152
+
153
+
154
+ /**
155
+ * Get order
156
+ *
157
+ * https://tools.ietf.org/html/rfc8555#section-7.4
158
+ *
159
+ * @param {string} url Order URL
160
+ * @returns {Promise<object>} HTTP response
161
+ */
162
+
163
+ getOrder(url) {
164
+ return this.apiRequest(url, null, [200]);
165
+ }
166
+
167
+
168
+ /**
169
+ * Finalize order
170
+ *
171
+ * https://tools.ietf.org/html/rfc8555#section-7.4
172
+ *
173
+ * @param {string} url Finalization URL
174
+ * @param {object} data Request payload
175
+ * @returns {Promise<object>} HTTP response
176
+ */
177
+
178
+ finalizeOrder(url, data) {
179
+ return this.apiRequest(url, data, [200]);
180
+ }
181
+
182
+
183
+ /**
184
+ * Get identifier authorization
185
+ *
186
+ * https://tools.ietf.org/html/rfc8555#section-7.5
187
+ *
188
+ * @param {string} url Authorization URL
189
+ * @returns {Promise<object>} HTTP response
190
+ */
191
+
192
+ getAuthorization(url) {
193
+ return this.apiRequest(url, null, [200]);
194
+ }
195
+
196
+
197
+ /**
198
+ * Update identifier authorization
199
+ *
200
+ * https://tools.ietf.org/html/rfc8555#section-7.5.2
201
+ *
202
+ * @param {string} url Authorization URL
203
+ * @param {object} data Request payload
204
+ * @returns {Promise<object>} HTTP response
205
+ */
206
+
207
+ updateAuthorization(url, data) {
208
+ return this.apiRequest(url, data, [200]);
209
+ }
210
+
211
+
212
+ /**
213
+ * Complete challenge
214
+ *
215
+ * https://tools.ietf.org/html/rfc8555#section-7.5.1
216
+ *
217
+ * @param {string} url Challenge URL
218
+ * @param {object} data Request payload
219
+ * @returns {Promise<object>} HTTP response
220
+ */
221
+
222
+ completeChallenge(url, data) {
223
+ return this.apiRequest(url, data, [200]);
224
+ }
225
+
226
+
227
+ /**
228
+ * Revoke certificate
229
+ *
230
+ * https://tools.ietf.org/html/rfc8555#section-7.6
231
+ *
232
+ * @param {object} data Request payload
233
+ * @returns {Promise<object>} HTTP response
234
+ */
235
+
236
+ revokeCert(data) {
237
+ return this.apiResourceRequest('revokeCert', data, [200]);
238
+ }
239
+ }
240
+
241
+
242
+ /* Export API */
243
+ module.exports = AcmeApi;