5
5
6
6
"github.com/smallstep/certificates/api"
7
7
"github.com/smallstep/certificates/authority/admin"
8
+ "go.step.sm/linkedca"
8
9
)
9
10
10
11
// CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
@@ -13,44 +14,50 @@ type CreateExternalAccountKeyRequest struct {
13
14
Name string `json:"name"`
14
15
}
15
16
16
- // CreateExternalAccountKeyResponse is the type for POST /admin/acme/eab responses
17
- type CreateExternalAccountKeyResponse struct {
18
- ProvisionerName string `json:"provisioner"`
19
- KeyID string `json:"keyID"`
20
- Name string `json:"name"`
21
- Key []byte `json:"key"`
17
+ // Validate validates a new-admin request body.
18
+ func (r * CreateExternalAccountKeyRequest ) Validate () error {
19
+ if r .ProvisionerName == "" {
20
+ return admin .NewError (admin .ErrorBadRequestType , "provisioner name cannot be empty" )
21
+ }
22
+ if r .Name == "" {
23
+ return admin .NewError (admin .ErrorBadRequestType , "name / reference cannot be empty" )
24
+ }
25
+ return nil
22
26
}
23
27
24
28
// GetExternalAccountKeysResponse is the type for GET /admin/acme/eab responses
25
29
type GetExternalAccountKeysResponse struct {
26
- EAKs []* CreateExternalAccountKeyResponse `json:"eaks"`
27
- NextCursor string `json:"nextCursor"`
30
+ EAKs []* linkedca. EABKey `json:"eaks"`
31
+ NextCursor string `json:"nextCursor"`
28
32
}
29
33
30
34
// CreateExternalAccountKey creates a new External Account Binding key
31
35
func (h * Handler ) CreateExternalAccountKey (w http.ResponseWriter , r * http.Request ) {
32
36
var body CreateExternalAccountKeyRequest
33
37
if err := api .ReadJSON (r .Body , & body ); err != nil { // TODO: rewrite into protobuf json (likely)
34
- api .WriteError (w , err )
38
+ api .WriteError (w , admin . WrapError ( admin . ErrorBadRequestType , err , "error reading request body" ) )
35
39
return
36
40
}
37
41
38
- // TODO: Validate input
42
+ if err := body .Validate (); err != nil {
43
+ api .WriteError (w , err )
44
+ return
45
+ }
39
46
40
47
eak , err := h .acmeDB .CreateExternalAccountKey (r .Context (), body .ProvisionerName , body .Name )
41
48
if err != nil {
42
49
api .WriteError (w , admin .WrapErrorISE (err , "error creating external account key %s" , body .Name ))
43
50
return
44
51
}
45
52
46
- eakResponse := CreateExternalAccountKeyResponse {
53
+ response := & linkedca.EABKey {
54
+ EabKid : eak .ID ,
55
+ EabHmacKey : eak .KeyBytes ,
47
56
ProvisionerName : eak .ProvisionerName ,
48
- KeyID : eak .ID ,
49
57
Name : eak .Name ,
50
- Key : eak .KeyBytes ,
51
58
}
52
59
53
- api .JSONStatus (w , eakResponse , http .StatusCreated ) // TODO: rewrite into protobuf json (likely )
60
+ api .ProtoJSONStatus (w , response , http .StatusCreated )
54
61
}
55
62
56
63
// GetExternalAccountKeys returns a segment of ACME EAB Keys.
0 commit comments