account.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. return acme.MalformedErr(errors.Errorf("empty update request"))
  61. }
  62. }
  63. // NewAccount is the handler resource for creating new ACME accounts.
  64. func (h *Handler) NewAccount(w http.ResponseWriter, r *http.Request) {
  65. prov, err := provisionerFromContext(r)
  66. if err != nil {
  67. api.WriteError(w, err)
  68. return
  69. }
  70. payload, err := payloadFromContext(r)
  71. if err != nil {
  72. api.WriteError(w, err)
  73. return
  74. }
  75. var nar NewAccountRequest
  76. if err := json.Unmarshal(payload.value, &nar); err != nil {
  77. api.WriteError(w, acme.MalformedErr(errors.Wrap(err,
  78. "failed to unmarshal new-account request payload")))
  79. return
  80. }
  81. if err := nar.Validate(); err != nil {
  82. api.WriteError(w, err)
  83. return
  84. }
  85. httpStatus := http.StatusCreated
  86. acc, err := accountFromContext(r)
  87. if err != nil {
  88. acmeErr, ok := err.(*acme.Error)
  89. if !ok || acmeErr.Status != http.StatusNotFound {
  90. // Something went wrong ...
  91. api.WriteError(w, err)
  92. return
  93. }
  94. // Account does not exist //
  95. if nar.OnlyReturnExisting {
  96. api.WriteError(w, acme.AccountDoesNotExistErr(nil))
  97. return
  98. }
  99. jwk, err := jwkFromContext(r)
  100. if err != nil {
  101. api.WriteError(w, err)
  102. return
  103. }
  104. if acc, err = h.Auth.NewAccount(prov, acme.AccountOptions{
  105. Key: jwk,
  106. Contact: nar.Contact,
  107. }); err != nil {
  108. api.WriteError(w, err)
  109. return
  110. }
  111. } else {
  112. // Account exists //
  113. httpStatus = http.StatusOK
  114. }
  115. w.Header().Set("Location", h.Auth.GetLink(acme.AccountLink,
  116. acme.URLSafeProvisionerName(prov), true, acc.GetID()))
  117. api.JSONStatus(w, acc, httpStatus)
  118. return
  119. }
  120. // GetUpdateAccount is the api for updating an ACME account.
  121. func (h *Handler) GetUpdateAccount(w http.ResponseWriter, r *http.Request) {
  122. prov, err := provisionerFromContext(r)
  123. if err != nil {
  124. api.WriteError(w, err)
  125. return
  126. }
  127. acc, err := accountFromContext(r)
  128. if err != nil {
  129. api.WriteError(w, err)
  130. return
  131. }
  132. payload, err := payloadFromContext(r)
  133. if err != nil {
  134. api.WriteError(w, err)
  135. return
  136. }
  137. if !payload.isPostAsGet {
  138. var uar UpdateAccountRequest
  139. if err := json.Unmarshal(payload.value, &uar); err != nil {
  140. api.WriteError(w, acme.MalformedErr(errors.Wrap(err, "failed to unmarshal new-account request payload")))
  141. return
  142. }
  143. if err := uar.Validate(); err != nil {
  144. api.WriteError(w, err)
  145. return
  146. }
  147. var err error
  148. if uar.IsDeactivateRequest() {
  149. acc, err = h.Auth.DeactivateAccount(prov, acc.GetID())
  150. } else {
  151. acc, err = h.Auth.UpdateAccount(prov, acc.GetID(), uar.Contact)
  152. }
  153. if err != nil {
  154. api.WriteError(w, err)
  155. return
  156. }
  157. }
  158. w.Header().Set("Location", h.Auth.GetLink(acme.AccountLink, acme.URLSafeProvisionerName(prov), true, acc.GetID()))
  159. api.JSON(w, acc)
  160. return
  161. }
  162. func logOrdersByAccount(w http.ResponseWriter, oids []string) {
  163. if rl, ok := w.(logging.ResponseLogger); ok {
  164. m := map[string]interface{}{
  165. "orders": oids,
  166. }
  167. rl.WithFields(m)
  168. }
  169. }
  170. // GetOrdersByAccount ACME api for retrieving the list of order urls belonging to an account.
  171. func (h *Handler) GetOrdersByAccount(w http.ResponseWriter, r *http.Request) {
  172. prov, err := provisionerFromContext(r)
  173. if err != nil {
  174. api.WriteError(w, err)
  175. return
  176. }
  177. acc, err := accountFromContext(r)
  178. if err != nil {
  179. api.WriteError(w, err)
  180. return
  181. }
  182. accID := chi.URLParam(r, "accID")
  183. if acc.ID != accID {
  184. api.WriteError(w, acme.UnauthorizedErr(errors.New("account ID does not match url param")))
  185. return
  186. }
  187. orders, err := h.Auth.GetOrdersByAccount(prov, acc.GetID())
  188. if err != nil {
  189. api.WriteError(w, err)
  190. return
  191. }
  192. api.JSON(w, orders)
  193. logOrdersByAccount(w, orders)
  194. return
  195. }