account.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/go-chi/chi"
  6. "github.com/smallstep/certificates/acme"
  7. "github.com/smallstep/certificates/api/render"
  8. "github.com/smallstep/certificates/logging"
  9. )
  10. // NewAccountRequest represents the payload for a new account request.
  11. type NewAccountRequest struct {
  12. Contact []string `json:"contact"`
  13. OnlyReturnExisting bool `json:"onlyReturnExisting"`
  14. TermsOfServiceAgreed bool `json:"termsOfServiceAgreed"`
  15. ExternalAccountBinding *ExternalAccountBinding `json:"externalAccountBinding,omitempty"`
  16. }
  17. func validateContacts(cs []string) error {
  18. for _, c := range cs {
  19. if c == "" {
  20. return acme.NewError(acme.ErrorMalformedType, "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.NewError(acme.ErrorMalformedType, "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 acme.Status `json:"status"`
  36. }
  37. // Validate validates a update-account request body.
  38. func (u *UpdateAccountRequest) Validate() error {
  39. switch {
  40. case len(u.Status) > 0 && len(u.Contact) > 0:
  41. return acme.NewError(acme.ErrorMalformedType, "incompatible input; contact and "+
  42. "status updates are mutually exclusive")
  43. case len(u.Contact) > 0:
  44. if err := validateContacts(u.Contact); err != nil {
  45. return err
  46. }
  47. return nil
  48. case len(u.Status) > 0:
  49. if u.Status != acme.StatusDeactivated {
  50. return acme.NewError(acme.ErrorMalformedType, "cannot update account "+
  51. "status to %s, only deactivated", u.Status)
  52. }
  53. return nil
  54. default:
  55. // According to the ACME spec (https://tools.ietf.org/html/rfc8555#section-7.3.2)
  56. // accountUpdate should ignore any fields not recognized by the server.
  57. return nil
  58. }
  59. }
  60. // NewAccount is the handler resource for creating new ACME accounts.
  61. func (h *Handler) NewAccount(w http.ResponseWriter, r *http.Request) {
  62. ctx := r.Context()
  63. payload, err := payloadFromContext(ctx)
  64. if err != nil {
  65. render.Error(w, err)
  66. return
  67. }
  68. var nar NewAccountRequest
  69. if err := json.Unmarshal(payload.value, &nar); err != nil {
  70. render.Error(w, acme.WrapError(acme.ErrorMalformedType, err,
  71. "failed to unmarshal new-account request payload"))
  72. return
  73. }
  74. if err := nar.Validate(); err != nil {
  75. render.Error(w, err)
  76. return
  77. }
  78. prov, err := acmeProvisionerFromContext(ctx)
  79. if err != nil {
  80. render.Error(w, err)
  81. return
  82. }
  83. httpStatus := http.StatusCreated
  84. acc, err := accountFromContext(ctx)
  85. if err != nil {
  86. acmeErr, ok := err.(*acme.Error)
  87. if !ok || acmeErr.Status != http.StatusBadRequest {
  88. // Something went wrong ...
  89. render.Error(w, err)
  90. return
  91. }
  92. // Account does not exist //
  93. if nar.OnlyReturnExisting {
  94. render.Error(w, acme.NewError(acme.ErrorAccountDoesNotExistType,
  95. "account does not exist"))
  96. return
  97. }
  98. jwk, err := jwkFromContext(ctx)
  99. if err != nil {
  100. render.Error(w, err)
  101. return
  102. }
  103. eak, err := h.validateExternalAccountBinding(ctx, &nar)
  104. if err != nil {
  105. render.Error(w, err)
  106. return
  107. }
  108. acc = &acme.Account{
  109. Key: jwk,
  110. Contact: nar.Contact,
  111. Status: acme.StatusValid,
  112. }
  113. if err := h.db.CreateAccount(ctx, acc); err != nil {
  114. render.Error(w, acme.WrapErrorISE(err, "error creating account"))
  115. return
  116. }
  117. if eak != nil { // means that we have a (valid) External Account Binding key that should be bound, updated and sent in the response
  118. err := eak.BindTo(acc)
  119. if err != nil {
  120. render.Error(w, err)
  121. return
  122. }
  123. if err := h.db.UpdateExternalAccountKey(ctx, prov.ID, eak); err != nil {
  124. render.Error(w, acme.WrapErrorISE(err, "error updating external account binding key"))
  125. return
  126. }
  127. acc.ExternalAccountBinding = nar.ExternalAccountBinding
  128. }
  129. } else {
  130. // Account exists
  131. httpStatus = http.StatusOK
  132. }
  133. h.linker.LinkAccount(ctx, acc)
  134. w.Header().Set("Location", h.linker.GetLink(r.Context(), AccountLinkType, acc.ID))
  135. render.JSONStatus(w, acc, httpStatus)
  136. }
  137. // GetOrUpdateAccount is the api for updating an ACME account.
  138. func (h *Handler) GetOrUpdateAccount(w http.ResponseWriter, r *http.Request) {
  139. ctx := r.Context()
  140. acc, err := accountFromContext(ctx)
  141. if err != nil {
  142. render.Error(w, err)
  143. return
  144. }
  145. payload, err := payloadFromContext(ctx)
  146. if err != nil {
  147. render.Error(w, err)
  148. return
  149. }
  150. // If PostAsGet just respond with the account, otherwise process like a
  151. // normal Post request.
  152. if !payload.isPostAsGet {
  153. var uar UpdateAccountRequest
  154. if err := json.Unmarshal(payload.value, &uar); err != nil {
  155. render.Error(w, acme.WrapError(acme.ErrorMalformedType, err,
  156. "failed to unmarshal new-account request payload"))
  157. return
  158. }
  159. if err := uar.Validate(); err != nil {
  160. render.Error(w, err)
  161. return
  162. }
  163. if len(uar.Status) > 0 || len(uar.Contact) > 0 {
  164. if len(uar.Status) > 0 {
  165. acc.Status = uar.Status
  166. } else if len(uar.Contact) > 0 {
  167. acc.Contact = uar.Contact
  168. }
  169. if err := h.db.UpdateAccount(ctx, acc); err != nil {
  170. render.Error(w, acme.WrapErrorISE(err, "error updating account"))
  171. return
  172. }
  173. }
  174. }
  175. h.linker.LinkAccount(ctx, acc)
  176. w.Header().Set("Location", h.linker.GetLink(ctx, AccountLinkType, acc.ID))
  177. render.JSON(w, acc)
  178. }
  179. func logOrdersByAccount(w http.ResponseWriter, oids []string) {
  180. if rl, ok := w.(logging.ResponseLogger); ok {
  181. m := map[string]interface{}{
  182. "orders": oids,
  183. }
  184. rl.WithFields(m)
  185. }
  186. }
  187. // GetOrdersByAccountID ACME api for retrieving the list of order urls belonging to an account.
  188. func (h *Handler) GetOrdersByAccountID(w http.ResponseWriter, r *http.Request) {
  189. ctx := r.Context()
  190. acc, err := accountFromContext(ctx)
  191. if err != nil {
  192. render.Error(w, err)
  193. return
  194. }
  195. accID := chi.URLParam(r, "accID")
  196. if acc.ID != accID {
  197. render.Error(w, acme.NewError(acme.ErrorUnauthorizedType, "account ID '%s' does not match url param '%s'", acc.ID, accID))
  198. return
  199. }
  200. orders, err := h.db.GetOrdersByAccountID(ctx, acc.ID)
  201. if err != nil {
  202. render.Error(w, err)
  203. return
  204. }
  205. h.linker.LinkOrdersByAccountID(ctx, orders)
  206. render.JSON(w, orders)
  207. logOrdersByAccount(w, orders)
  208. }