2021-07-17 15:35:44 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/api"
|
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
|
|
|
)
|
|
|
|
|
2021-07-22 20:43:21 +00:00
|
|
|
// CreateExternalAccountKeyRequest is the type for POST /admin/eak requests
|
2021-07-17 15:35:44 +00:00
|
|
|
type CreateExternalAccountKeyRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:43:21 +00:00
|
|
|
// CreateExternalAccountKeyResponse is the type for POST /admin/eak responses
|
2021-07-17 15:35:44 +00:00
|
|
|
type CreateExternalAccountKeyResponse struct {
|
|
|
|
KeyID string `json:"keyID"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Key []byte `json:"key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateExternalAccountKey creates a new External Account Binding key
|
|
|
|
func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Request) {
|
2021-07-22 20:43:21 +00:00
|
|
|
var body CreateExternalAccountKeyRequest
|
|
|
|
if err := api.ReadJSON(r.Body, &body); err != nil { // TODO: rewrite into protobuf json (likely)
|
2021-07-17 15:35:44 +00:00
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Validate input
|
|
|
|
|
2021-07-22 20:43:21 +00:00
|
|
|
eak, err := h.db.CreateExternalAccountKey(r.Context(), body.Name)
|
2021-07-17 15:35:44 +00:00
|
|
|
if err != nil {
|
2021-07-22 20:43:21 +00:00
|
|
|
api.WriteError(w, admin.WrapErrorISE(err, "error creating external account key %s", body.Name))
|
2021-07-17 15:35:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
eakResponse := CreateExternalAccountKeyResponse{
|
|
|
|
KeyID: eak.ID,
|
|
|
|
Name: eak.Name,
|
|
|
|
Key: eak.KeyBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
api.JSONStatus(w, eakResponse, http.StatusCreated) // TODO: rewrite into protobuf json (likely)
|
|
|
|
}
|