account.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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"
  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. }
  16. func validateContacts(cs []string) error {
  17. for _, c := range cs {
  18. if len(c) == 0 {
  19. return acme.NewError(acme.ErrorMalformedType, "contact cannot be empty string")
  20. }
  21. }
  22. return nil
  23. }
  24. // Validate validates a new-account request body.
  25. func (n *NewAccountRequest) Validate() error {
  26. if n.OnlyReturnExisting && len(n.Contact) > 0 {
  27. return acme.NewError(acme.ErrorMalformedType, "incompatible input; onlyReturnExisting must be alone")
  28. }
  29. return validateContacts(n.Contact)
  30. }
  31. // UpdateAccountRequest represents an update-account request.
  32. type UpdateAccountRequest struct {
  33. Contact []string `json:"contact"`
  34. Status acme.Status `json:"status"`
  35. }
  36. // Validate validates a update-account request body.
  37. func (u *UpdateAccountRequest) Validate() error {
  38. switch {
  39. case len(u.Status) > 0 && len(u.Contact) > 0:
  40. return acme.NewError(acme.ErrorMalformedType, "incompatible input; contact and "+
  41. "status updates are mutually exclusive")
  42. case len(u.Contact) > 0:
  43. if err := validateContacts(u.Contact); err != nil {
  44. return err
  45. }
  46. return nil
  47. case len(u.Status) > 0:
  48. if u.Status != acme.StatusDeactivated {
  49. return acme.NewError(acme.ErrorMalformedType, "cannot update account "+
  50. "status to %s, only deactivated", u.Status)
  51. }
  52. return nil
  53. default:
  54. // According to the ACME spec (https://tools.ietf.org/html/rfc8555#section-7.3.2)
  55. // accountUpdate should ignore any fields not recognized by the server.
  56. return nil
  57. }
  58. }
  59. // NewAccount is the handler resource for creating new ACME accounts.
  60. func (h *Handler) NewAccount(w http.ResponseWriter, r *http.Request) {
  61. ctx := r.Context()
  62. payload, err := payloadFromContext(ctx)
  63. if err != nil {
  64. api.WriteError(w, err)
  65. return
  66. }
  67. var nar NewAccountRequest
  68. if err := json.Unmarshal(payload.value, &nar); err != nil {
  69. api.WriteError(w, acme.WrapError(acme.ErrorMalformedType, err,
  70. "failed to unmarshal new-account request payload"))
  71. return
  72. }
  73. if err := nar.Validate(); err != nil {
  74. api.WriteError(w, err)
  75. return
  76. }
  77. httpStatus := http.StatusCreated
  78. acc, err := accountFromContext(r.Context())
  79. if err != nil {
  80. acmeErr, ok := err.(*acme.Error)
  81. if !ok || acmeErr.Status != http.StatusBadRequest {
  82. // Something went wrong ...
  83. api.WriteError(w, err)
  84. return
  85. }
  86. // Account does not exist //
  87. if nar.OnlyReturnExisting {
  88. api.WriteError(w, acme.NewError(acme.ErrorAccountDoesNotExistType,
  89. "account does not exist"))
  90. return
  91. }
  92. jwk, err := jwkFromContext(ctx)
  93. if err != nil {
  94. api.WriteError(w, err)
  95. return
  96. }
  97. acc = &acme.Account{
  98. Key: jwk,
  99. Contact: nar.Contact,
  100. Status: acme.StatusValid,
  101. }
  102. if err := h.db.CreateAccount(ctx, acc); err != nil {
  103. api.WriteError(w, acme.WrapErrorISE(err, "error creating account"))
  104. return
  105. }
  106. } else {
  107. // Account exists //
  108. httpStatus = http.StatusOK
  109. }
  110. h.linker.LinkAccount(ctx, acc)
  111. w.Header().Set("Location", h.linker.GetLink(r.Context(), AccountLinkType, acc.ID))
  112. api.JSONStatus(w, acc, httpStatus)
  113. }
  114. // GetOrUpdateAccount is the api for updating an ACME account.
  115. func (h *Handler) GetOrUpdateAccount(w http.ResponseWriter, r *http.Request) {
  116. ctx := r.Context()
  117. acc, err := accountFromContext(ctx)
  118. if err != nil {
  119. api.WriteError(w, err)
  120. return
  121. }
  122. payload, err := payloadFromContext(ctx)
  123. if err != nil {
  124. api.WriteError(w, err)
  125. return
  126. }
  127. // If PostAsGet just respond with the account, otherwise process like a
  128. // normal Post request.
  129. if !payload.isPostAsGet {
  130. var uar UpdateAccountRequest
  131. if err := json.Unmarshal(payload.value, &uar); err != nil {
  132. api.WriteError(w, acme.WrapError(acme.ErrorMalformedType, err,
  133. "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. if len(uar.Status) > 0 || len(uar.Contact) > 0 {
  141. if len(uar.Status) > 0 {
  142. acc.Status = uar.Status
  143. } else if len(uar.Contact) > 0 {
  144. acc.Contact = uar.Contact
  145. }
  146. if err := h.db.UpdateAccount(ctx, acc); err != nil {
  147. api.WriteError(w, acme.WrapErrorISE(err, "error updating account"))
  148. return
  149. }
  150. }
  151. }
  152. h.linker.LinkAccount(ctx, acc)
  153. w.Header().Set("Location", h.linker.GetLink(ctx, AccountLinkType, acc.ID))
  154. api.JSON(w, acc)
  155. }
  156. func logOrdersByAccount(w http.ResponseWriter, oids []string) {
  157. if rl, ok := w.(logging.ResponseLogger); ok {
  158. m := map[string]interface{}{
  159. "orders": oids,
  160. }
  161. rl.WithFields(m)
  162. }
  163. }
  164. // GetOrdersByAccountID ACME api for retrieving the list of order urls belonging to an account.
  165. func (h *Handler) GetOrdersByAccountID(w http.ResponseWriter, r *http.Request) {
  166. ctx := r.Context()
  167. acc, err := accountFromContext(ctx)
  168. if err != nil {
  169. api.WriteError(w, err)
  170. return
  171. }
  172. accID := chi.URLParam(r, "accID")
  173. if acc.ID != accID {
  174. api.WriteError(w, acme.NewError(acme.ErrorUnauthorizedType, "account ID '%s' does not match url param '%s'", acc.ID, accID))
  175. return
  176. }
  177. orders, err := h.db.GetOrdersByAccountID(ctx, acc.ID)
  178. if err != nil {
  179. api.WriteError(w, err)
  180. return
  181. }
  182. h.linker.LinkOrdersByAccountID(ctx, orders)
  183. api.JSON(w, orders)
  184. logOrdersByAccount(w, orders)
  185. }