account.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/go-chi/chi"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/acme"
  8. "github.com/smallstep/certificates/api"
  9. "github.com/smallstep/certificates/logging"
  10. )
  11. // NewAccountRequest represents the payload for a new account request.
  12. type NewAccountRequest struct {
  13. Contact []string `json:"contact"`
  14. OnlyReturnExisting bool `json:"onlyReturnExisting"`
  15. TermsOfServiceAgreed bool `json:"termsOfServiceAgreed"`
  16. }
  17. func validateContacts(cs []string) error {
  18. for _, c := range cs {
  19. if len(c) == 0 {
  20. return acme.MalformedErr(errors.New("contact cannot be empty string"))
  21. }
  22. }
  23. return nil
  24. }
  25. // Validate validates a new-account request body.
  26. func (n *NewAccountRequest) Validate() error {
  27. if n.OnlyReturnExisting && len(n.Contact) > 0 {
  28. return acme.MalformedErr(errors.New("incompatible input; onlyReturnExisting must be alone"))
  29. }
  30. return validateContacts(n.Contact)
  31. }
  32. // UpdateAccountRequest represents an update-account request.
  33. type UpdateAccountRequest struct {
  34. Contact []string `json:"contact"`
  35. Status string `json:"status"`
  36. }
  37. // IsDeactivateRequest returns true if the update request is a deactivation
  38. // request, false otherwise.
  39. func (u *UpdateAccountRequest) IsDeactivateRequest() bool {
  40. return u.Status == acme.StatusDeactivated
  41. }
  42. // Validate validates a update-account request body.
  43. func (u *UpdateAccountRequest) Validate() error {
  44. switch {
  45. case len(u.Status) > 0 && len(u.Contact) > 0:
  46. return acme.MalformedErr(errors.New("incompatible input; contact and " +
  47. "status updates are mutually exclusive"))
  48. case len(u.Contact) > 0:
  49. if err := validateContacts(u.Contact); err != nil {
  50. return err
  51. }
  52. return nil
  53. case len(u.Status) > 0:
  54. if u.Status != acme.StatusDeactivated {
  55. return acme.MalformedErr(errors.Errorf("cannot update account "+
  56. "status to %s, only deactivated", u.Status))
  57. }
  58. return nil
  59. default:
  60. // According to the ACME spec (https://tools.ietf.org/html/rfc8555#section-7.3.2)
  61. // accountUpdate should ignore any fields not recognized by the server.
  62. return nil
  63. }
  64. }
  65. // NewAccount is the handler resource for creating new ACME accounts.
  66. func (h *Handler) NewAccount(w http.ResponseWriter, r *http.Request) {
  67. payload, err := payloadFromContext(r.Context())
  68. if err != nil {
  69. api.WriteError(w, err)
  70. return
  71. }
  72. var nar NewAccountRequest
  73. if err := json.Unmarshal(payload.value, &nar); err != nil {
  74. api.WriteError(w, acme.MalformedErr(errors.Wrap(err,
  75. "failed to unmarshal new-account request payload")))
  76. return
  77. }
  78. if err := nar.Validate(); err != nil {
  79. api.WriteError(w, err)
  80. return
  81. }
  82. httpStatus := http.StatusCreated
  83. acc, err := acme.AccountFromContext(r.Context())
  84. if err != nil {
  85. acmeErr, ok := err.(*acme.Error)
  86. if !ok || acmeErr.Status != http.StatusBadRequest {
  87. // Something went wrong ...
  88. api.WriteError(w, err)
  89. return
  90. }
  91. // Account does not exist //
  92. if nar.OnlyReturnExisting {
  93. api.WriteError(w, acme.AccountDoesNotExistErr(nil))
  94. return
  95. }
  96. jwk, err := acme.JwkFromContext(r.Context())
  97. if err != nil {
  98. api.WriteError(w, err)
  99. return
  100. }
  101. if acc, err = h.Auth.NewAccount(r.Context(), acme.AccountOptions{
  102. Key: jwk,
  103. Contact: nar.Contact,
  104. }); err != nil {
  105. api.WriteError(w, err)
  106. return
  107. }
  108. } else {
  109. // Account exists //
  110. httpStatus = http.StatusOK
  111. }
  112. w.Header().Set("Location", h.Auth.GetLink(r.Context(), acme.AccountLink,
  113. true, acc.GetID()))
  114. api.JSONStatus(w, acc, httpStatus)
  115. }
  116. // GetUpdateAccount is the api for updating an ACME account.
  117. func (h *Handler) GetUpdateAccount(w http.ResponseWriter, r *http.Request) {
  118. acc, err := acme.AccountFromContext(r.Context())
  119. if err != nil {
  120. api.WriteError(w, err)
  121. return
  122. }
  123. payload, err := payloadFromContext(r.Context())
  124. if err != nil {
  125. api.WriteError(w, err)
  126. return
  127. }
  128. // If PostAsGet just respond with the account, otherwise process like a
  129. // normal Post request.
  130. if !payload.isPostAsGet {
  131. var uar UpdateAccountRequest
  132. if err := json.Unmarshal(payload.value, &uar); err != nil {
  133. api.WriteError(w, acme.MalformedErr(errors.Wrap(err, "failed to unmarshal new-account request payload")))
  134. return
  135. }
  136. if err := uar.Validate(); err != nil {
  137. api.WriteError(w, err)
  138. return
  139. }
  140. var err error
  141. // If neither the status nor the contacts are being updated then ignore
  142. // the updates and return 200. This conforms with the behavior detailed
  143. // in the ACME spec (https://tools.ietf.org/html/rfc8555#section-7.3.2).
  144. if uar.IsDeactivateRequest() {
  145. acc, err = h.Auth.DeactivateAccount(r.Context(), acc.GetID())
  146. } else if len(uar.Contact) > 0 {
  147. acc, err = h.Auth.UpdateAccount(r.Context(), acc.GetID(), uar.Contact)
  148. }
  149. if err != nil {
  150. api.WriteError(w, err)
  151. return
  152. }
  153. }
  154. w.Header().Set("Location", h.Auth.GetLink(r.Context(), acme.AccountLink,
  155. true, acc.GetID()))
  156. api.JSON(w, acc)
  157. }
  158. func logOrdersByAccount(w http.ResponseWriter, oids []string) {
  159. if rl, ok := w.(logging.ResponseLogger); ok {
  160. m := map[string]interface{}{
  161. "orders": oids,
  162. }
  163. rl.WithFields(m)
  164. }
  165. }
  166. // GetOrdersByAccount ACME api for retrieving the list of order urls belonging to an account.
  167. func (h *Handler) GetOrdersByAccount(w http.ResponseWriter, r *http.Request) {
  168. acc, err := acme.AccountFromContext(r.Context())
  169. if err != nil {
  170. api.WriteError(w, err)
  171. return
  172. }
  173. accID := chi.URLParam(r, "accID")
  174. if acc.ID != accID {
  175. api.WriteError(w, acme.UnauthorizedErr(errors.New("account ID does not match url param")))
  176. return
  177. }
  178. orders, err := h.Auth.GetOrdersByAccount(r.Context(), acc.GetID())
  179. if err != nil {
  180. api.WriteError(w, err)
  181. return
  182. }
  183. api.JSON(w, orders)
  184. logOrdersByAccount(w, orders)
  185. }