account.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if err := eak.BindTo(acc); err != nil {
  119. render.Error(w, err)
  120. return
  121. }
  122. if err := h.db.UpdateExternalAccountKey(ctx, prov.ID, eak); err != nil {
  123. render.Error(w, acme.WrapErrorISE(err, "error updating external account binding key"))
  124. return
  125. }
  126. acc.ExternalAccountBinding = nar.ExternalAccountBinding
  127. }
  128. } else {
  129. // Account exists
  130. httpStatus = http.StatusOK
  131. }
  132. h.linker.LinkAccount(ctx, acc)
  133. w.Header().Set("Location", h.linker.GetLink(r.Context(), AccountLinkType, acc.ID))
  134. render.JSONStatus(w, acc, httpStatus)
  135. }
  136. // GetOrUpdateAccount is the api for updating an ACME account.
  137. func (h *Handler) GetOrUpdateAccount(w http.ResponseWriter, r *http.Request) {
  138. ctx := r.Context()
  139. acc, err := accountFromContext(ctx)
  140. if err != nil {
  141. render.Error(w, err)
  142. return
  143. }
  144. payload, err := payloadFromContext(ctx)
  145. if err != nil {
  146. render.Error(w, err)
  147. return
  148. }
  149. // If PostAsGet just respond with the account, otherwise process like a
  150. // normal Post request.
  151. if !payload.isPostAsGet {
  152. var uar UpdateAccountRequest
  153. if err := json.Unmarshal(payload.value, &uar); err != nil {
  154. render.Error(w, acme.WrapError(acme.ErrorMalformedType, err,
  155. "failed to unmarshal new-account request payload"))
  156. return
  157. }
  158. if err := uar.Validate(); err != nil {
  159. render.Error(w, err)
  160. return
  161. }
  162. if len(uar.Status) > 0 || len(uar.Contact) > 0 {
  163. if len(uar.Status) > 0 {
  164. acc.Status = uar.Status
  165. } else if len(uar.Contact) > 0 {
  166. acc.Contact = uar.Contact
  167. }
  168. if err := h.db.UpdateAccount(ctx, acc); err != nil {
  169. render.Error(w, acme.WrapErrorISE(err, "error updating account"))
  170. return
  171. }
  172. }
  173. }
  174. h.linker.LinkAccount(ctx, acc)
  175. w.Header().Set("Location", h.linker.GetLink(ctx, AccountLinkType, acc.ID))
  176. render.JSON(w, acc)
  177. }
  178. func logOrdersByAccount(w http.ResponseWriter, oids []string) {
  179. if rl, ok := w.(logging.ResponseLogger); ok {
  180. m := map[string]interface{}{
  181. "orders": oids,
  182. }
  183. rl.WithFields(m)
  184. }
  185. }
  186. // GetOrdersByAccountID ACME api for retrieving the list of order urls belonging to an account.
  187. func (h *Handler) GetOrdersByAccountID(w http.ResponseWriter, r *http.Request) {
  188. ctx := r.Context()
  189. acc, err := accountFromContext(ctx)
  190. if err != nil {
  191. render.Error(w, err)
  192. return
  193. }
  194. accID := chi.URLParam(r, "accID")
  195. if acc.ID != accID {
  196. render.Error(w, acme.NewError(acme.ErrorUnauthorizedType, "account ID '%s' does not match url param '%s'", acc.ID, accID))
  197. return
  198. }
  199. orders, err := h.db.GetOrdersByAccountID(ctx, acc.ID)
  200. if err != nil {
  201. render.Error(w, err)
  202. return
  203. }
  204. h.linker.LinkOrdersByAccountID(ctx, orders)
  205. render.JSON(w, orders)
  206. logOrdersByAccount(w, orders)
  207. }