@@ -12,8 +12,10 @@ import (
12
12
"time"
13
13
14
14
"github.com/go-chi/chi"
15
+
15
16
"github.com/smallstep/certificates/acme"
16
17
"github.com/smallstep/certificates/api"
18
+ "github.com/smallstep/certificates/api/render"
17
19
"github.com/smallstep/certificates/authority/provisioner"
18
20
)
19
21
@@ -181,11 +183,11 @@ func (h *Handler) GetDirectory(w http.ResponseWriter, r *http.Request) {
181
183
ctx := r .Context ()
182
184
acmeProv , err := acmeProvisionerFromContext (ctx )
183
185
if err != nil {
184
- api . WriteError (w , err )
186
+ render . Error (w , err )
185
187
return
186
188
}
187
189
188
- api .JSON (w , & Directory {
190
+ render .JSON (w , & Directory {
189
191
NewNonce : h .linker .GetLink (ctx , NewNonceLinkType ),
190
192
NewAccount : h .linker .GetLink (ctx , NewAccountLinkType ),
191
193
NewOrder : h .linker .GetLink (ctx , NewOrderLinkType ),
@@ -200,51 +202,51 @@ func (h *Handler) GetDirectory(w http.ResponseWriter, r *http.Request) {
200
202
// NotImplemented returns a 501 and is generally a placeholder for functionality which
201
203
// MAY be added at some point in the future but is not in any way a guarantee of such.
202
204
func (h * Handler ) NotImplemented (w http.ResponseWriter , r * http.Request ) {
203
- api . WriteError (w , acme .NewError (acme .ErrorNotImplementedType , "this API is not implemented" ))
205
+ render . Error (w , acme .NewError (acme .ErrorNotImplementedType , "this API is not implemented" ))
204
206
}
205
207
206
208
// GetAuthorization ACME api for retrieving an Authz.
207
209
func (h * Handler ) GetAuthorization (w http.ResponseWriter , r * http.Request ) {
208
210
ctx := r .Context ()
209
211
acc , err := accountFromContext (ctx )
210
212
if err != nil {
211
- api . WriteError (w , err )
213
+ render . Error (w , err )
212
214
return
213
215
}
214
216
az , err := h .db .GetAuthorization (ctx , chi .URLParam (r , "authzID" ))
215
217
if err != nil {
216
- api . WriteError (w , acme .WrapErrorISE (err , "error retrieving authorization" ))
218
+ render . Error (w , acme .WrapErrorISE (err , "error retrieving authorization" ))
217
219
return
218
220
}
219
221
if acc .ID != az .AccountID {
220
- api . WriteError (w , acme .NewError (acme .ErrorUnauthorizedType ,
222
+ render . Error (w , acme .NewError (acme .ErrorUnauthorizedType ,
221
223
"account '%s' does not own authorization '%s'" , acc .ID , az .ID ))
222
224
return
223
225
}
224
226
if err = az .UpdateStatus (ctx , h .db ); err != nil {
225
- api . WriteError (w , acme .WrapErrorISE (err , "error updating authorization status" ))
227
+ render . Error (w , acme .WrapErrorISE (err , "error updating authorization status" ))
226
228
return
227
229
}
228
230
229
231
h .linker .LinkAuthorization (ctx , az )
230
232
231
233
w .Header ().Set ("Location" , h .linker .GetLink (ctx , AuthzLinkType , az .ID ))
232
- api .JSON (w , az )
234
+ render .JSON (w , az )
233
235
}
234
236
235
237
// GetChallenge ACME api for retrieving a Challenge.
236
238
func (h * Handler ) GetChallenge (w http.ResponseWriter , r * http.Request ) {
237
239
ctx := r .Context ()
238
240
acc , err := accountFromContext (ctx )
239
241
if err != nil {
240
- api . WriteError (w , err )
242
+ render . Error (w , err )
241
243
return
242
244
}
243
245
// Just verify that the payload was set, since we're not strictly adhering
244
246
// to ACME V2 spec for reasons specified below.
245
247
_ , err = payloadFromContext (ctx )
246
248
if err != nil {
247
- api . WriteError (w , err )
249
+ render . Error (w , err )
248
250
return
249
251
}
250
252
@@ -257,49 +259,49 @@ func (h *Handler) GetChallenge(w http.ResponseWriter, r *http.Request) {
257
259
azID := chi .URLParam (r , "authzID" )
258
260
ch , err := h .db .GetChallenge (ctx , chi .URLParam (r , "chID" ), azID )
259
261
if err != nil {
260
- api . WriteError (w , acme .WrapErrorISE (err , "error retrieving challenge" ))
262
+ render . Error (w , acme .WrapErrorISE (err , "error retrieving challenge" ))
261
263
return
262
264
}
263
265
ch .AuthorizationID = azID
264
266
if acc .ID != ch .AccountID {
265
- api . WriteError (w , acme .NewError (acme .ErrorUnauthorizedType ,
267
+ render . Error (w , acme .NewError (acme .ErrorUnauthorizedType ,
266
268
"account '%s' does not own challenge '%s'" , acc .ID , ch .ID ))
267
269
return
268
270
}
269
271
jwk , err := jwkFromContext (ctx )
270
272
if err != nil {
271
- api . WriteError (w , err )
273
+ render . Error (w , err )
272
274
return
273
275
}
274
276
if err = ch .Validate (ctx , h .db , jwk , h .validateChallengeOptions ); err != nil {
275
- api . WriteError (w , acme .WrapErrorISE (err , "error validating challenge" ))
277
+ render . Error (w , acme .WrapErrorISE (err , "error validating challenge" ))
276
278
return
277
279
}
278
280
279
281
h .linker .LinkChallenge (ctx , ch , azID )
280
282
281
283
w .Header ().Add ("Link" , link (h .linker .GetLink (ctx , AuthzLinkType , azID ), "up" ))
282
284
w .Header ().Set ("Location" , h .linker .GetLink (ctx , ChallengeLinkType , azID , ch .ID ))
283
- api .JSON (w , ch )
285
+ render .JSON (w , ch )
284
286
}
285
287
286
288
// GetCertificate ACME api for retrieving a Certificate.
287
289
func (h * Handler ) GetCertificate (w http.ResponseWriter , r * http.Request ) {
288
290
ctx := r .Context ()
289
291
acc , err := accountFromContext (ctx )
290
292
if err != nil {
291
- api . WriteError (w , err )
293
+ render . Error (w , err )
292
294
return
293
295
}
294
296
certID := chi .URLParam (r , "certID" )
295
297
296
298
cert , err := h .db .GetCertificate (ctx , certID )
297
299
if err != nil {
298
- api . WriteError (w , acme .WrapErrorISE (err , "error retrieving certificate" ))
300
+ render . Error (w , acme .WrapErrorISE (err , "error retrieving certificate" ))
299
301
return
300
302
}
301
303
if cert .AccountID != acc .ID {
302
- api . WriteError (w , acme .NewError (acme .ErrorUnauthorizedType ,
304
+ render . Error (w , acme .NewError (acme .ErrorUnauthorizedType ,
303
305
"account '%s' does not own certificate '%s'" , acc .ID , certID ))
304
306
return
305
307
}
0 commit comments