2019-05-27 00:41:10 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/certificates/acme"
|
|
|
|
"github.com/smallstep/certificates/api"
|
|
|
|
"github.com/smallstep/certificates/logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewAccountRequest represents the payload for a new account request.
|
|
|
|
type NewAccountRequest struct {
|
|
|
|
Contact []string `json:"contact"`
|
|
|
|
OnlyReturnExisting bool `json:"onlyReturnExisting"`
|
|
|
|
TermsOfServiceAgreed bool `json:"termsOfServiceAgreed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateContacts(cs []string) error {
|
|
|
|
for _, c := range cs {
|
|
|
|
if len(c) == 0 {
|
2021-03-03 23:16:25 +00:00
|
|
|
return acme.NewError(acme.ErrorMalformedType, "contact cannot be empty string")
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates a new-account request body.
|
|
|
|
func (n *NewAccountRequest) Validate() error {
|
|
|
|
if n.OnlyReturnExisting && len(n.Contact) > 0 {
|
2021-03-03 23:16:25 +00:00
|
|
|
return acme.NewError(acme.ErrorMalformedType, "incompatible input; onlyReturnExisting must be alone")
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
return validateContacts(n.Contact)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAccountRequest represents an update-account request.
|
|
|
|
type UpdateAccountRequest struct {
|
|
|
|
Contact []string `json:"contact"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDeactivateRequest returns true if the update request is a deactivation
|
|
|
|
// request, false otherwise.
|
|
|
|
func (u *UpdateAccountRequest) IsDeactivateRequest() bool {
|
2021-03-01 06:49:20 +00:00
|
|
|
return u.Status == string(acme.StatusDeactivated)
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates a update-account request body.
|
|
|
|
func (u *UpdateAccountRequest) Validate() error {
|
|
|
|
switch {
|
|
|
|
case len(u.Status) > 0 && len(u.Contact) > 0:
|
2021-03-03 23:16:25 +00:00
|
|
|
return acme.NewError(acme.ErrorMalformedType, "incompatible input; contact and "+
|
|
|
|
"status updates are mutually exclusive")
|
2019-05-27 00:41:10 +00:00
|
|
|
case len(u.Contact) > 0:
|
|
|
|
if err := validateContacts(u.Contact); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case len(u.Status) > 0:
|
2021-03-01 06:49:20 +00:00
|
|
|
if u.Status != string(acme.StatusDeactivated) {
|
2021-03-03 23:16:25 +00:00
|
|
|
return acme.NewError(acme.ErrorMalformedType, "cannot update account "+
|
|
|
|
"status to %s, only deactivated", u.Status)
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
2020-05-08 18:52:30 +00:00
|
|
|
// According to the ACME spec (https://tools.ietf.org/html/rfc8555#section-7.3.2)
|
|
|
|
// accountUpdate should ignore any fields not recognized by the server.
|
|
|
|
return nil
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccount is the handler resource for creating new ACME accounts.
|
|
|
|
func (h *Handler) NewAccount(w http.ResponseWriter, r *http.Request) {
|
2020-05-07 03:18:12 +00:00
|
|
|
payload, err := payloadFromContext(r.Context())
|
2019-05-27 00:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var nar NewAccountRequest
|
|
|
|
if err := json.Unmarshal(payload.value, &nar); err != nil {
|
2021-03-03 23:16:25 +00:00
|
|
|
api.WriteError(w, acme.ErrorWrap(acme.ErrorMalformedType, err,
|
|
|
|
"failed to unmarshal new-account request payload"))
|
2019-05-27 00:41:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := nar.Validate(); err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpStatus := http.StatusCreated
|
2020-05-07 03:18:12 +00:00
|
|
|
acc, err := acme.AccountFromContext(r.Context())
|
2019-05-27 00:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
acmeErr, ok := err.(*acme.Error)
|
2020-02-02 01:35:41 +00:00
|
|
|
if !ok || acmeErr.Status != http.StatusBadRequest {
|
2019-05-27 00:41:10 +00:00
|
|
|
// Something went wrong ...
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account does not exist //
|
|
|
|
if nar.OnlyReturnExisting {
|
2021-03-03 23:16:25 +00:00
|
|
|
api.WriteError(w, acme.NewError(acme.ErrorAccountDoesNotExistType,
|
|
|
|
"account does not exist"))
|
2019-05-27 00:41:10 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-07 03:18:12 +00:00
|
|
|
jwk, err := acme.JwkFromContext(r.Context())
|
2019-05-27 00:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
if acc, err = h.Auth.NewAccount(r.Context(), &acme.Account{
|
2019-05-27 00:41:10 +00:00
|
|
|
Key: jwk,
|
|
|
|
Contact: nar.Contact,
|
2021-03-01 06:49:20 +00:00
|
|
|
Status: acme.StatusValid,
|
2019-05-27 00:41:10 +00:00
|
|
|
}); err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Account exists //
|
|
|
|
httpStatus = http.StatusOK
|
|
|
|
}
|
|
|
|
|
2020-05-07 03:18:12 +00:00
|
|
|
w.Header().Set("Location", h.Auth.GetLink(r.Context(), acme.AccountLink,
|
|
|
|
true, acc.GetID()))
|
2019-05-27 00:41:10 +00:00
|
|
|
api.JSONStatus(w, acc, httpStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUpdateAccount is the api for updating an ACME account.
|
|
|
|
func (h *Handler) GetUpdateAccount(w http.ResponseWriter, r *http.Request) {
|
2020-05-07 03:18:12 +00:00
|
|
|
acc, err := acme.AccountFromContext(r.Context())
|
2019-05-27 00:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2020-05-07 03:18:12 +00:00
|
|
|
payload, err := payloadFromContext(r.Context())
|
2019-05-27 00:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:52:30 +00:00
|
|
|
// If PostAsGet just respond with the account, otherwise process like a
|
|
|
|
// normal Post request.
|
2019-05-27 00:41:10 +00:00
|
|
|
if !payload.isPostAsGet {
|
|
|
|
var uar UpdateAccountRequest
|
|
|
|
if err := json.Unmarshal(payload.value, &uar); err != nil {
|
2021-03-03 23:16:25 +00:00
|
|
|
api.WriteError(w, acme.ErrorWrap(acme.ErrorMalformedType, err,
|
|
|
|
"failed to unmarshal new-account request payload"))
|
2019-05-27 00:41:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := uar.Validate(); err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
2020-05-08 18:52:30 +00:00
|
|
|
// If neither the status nor the contacts are being updated then ignore
|
|
|
|
// the updates and return 200. This conforms with the behavior detailed
|
|
|
|
// in the ACME spec (https://tools.ietf.org/html/rfc8555#section-7.3.2).
|
2019-05-27 00:41:10 +00:00
|
|
|
if uar.IsDeactivateRequest() {
|
2020-05-07 03:18:12 +00:00
|
|
|
acc, err = h.Auth.DeactivateAccount(r.Context(), acc.GetID())
|
2020-05-08 18:52:30 +00:00
|
|
|
} else if len(uar.Contact) > 0 {
|
2020-05-07 03:18:12 +00:00
|
|
|
acc, err = h.Auth.UpdateAccount(r.Context(), acc.GetID(), uar.Contact)
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 03:18:12 +00:00
|
|
|
w.Header().Set("Location", h.Auth.GetLink(r.Context(), acme.AccountLink,
|
|
|
|
true, acc.GetID()))
|
2019-05-27 00:41:10 +00:00
|
|
|
api.JSON(w, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logOrdersByAccount(w http.ResponseWriter, oids []string) {
|
|
|
|
if rl, ok := w.(logging.ResponseLogger); ok {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"orders": oids,
|
|
|
|
}
|
|
|
|
rl.WithFields(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrdersByAccount ACME api for retrieving the list of order urls belonging to an account.
|
|
|
|
func (h *Handler) GetOrdersByAccount(w http.ResponseWriter, r *http.Request) {
|
2020-05-07 03:18:12 +00:00
|
|
|
acc, err := acme.AccountFromContext(r.Context())
|
2019-05-27 00:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
accID := chi.URLParam(r, "accID")
|
|
|
|
if acc.ID != accID {
|
|
|
|
api.WriteError(w, acme.UnauthorizedErr(errors.New("account ID does not match url param")))
|
|
|
|
return
|
|
|
|
}
|
2020-05-07 03:18:12 +00:00
|
|
|
orders, err := h.Auth.GetOrdersByAccount(r.Context(), acc.GetID())
|
2019-05-27 00:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
api.JSON(w, orders)
|
|
|
|
logOrdersByAccount(w, orders)
|
|
|
|
}
|