4
0

sshRenew.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package api
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/certificates/authority/provisioner"
  7. )
  8. // SSHRenewRequest is the request body of an SSH certificate request.
  9. type SSHRenewRequest struct {
  10. OTT string `json:"ott"`
  11. }
  12. // Validate validates the SSHSignRequest.
  13. func (s *SSHRenewRequest) Validate() error {
  14. switch {
  15. case len(s.OTT) == 0:
  16. return errors.New("missing or empty ott")
  17. default:
  18. return nil
  19. }
  20. }
  21. // SSHRenewResponse is the response object that returns the SSH certificate.
  22. type SSHRenewResponse struct {
  23. Certificate SSHCertificate `json:"crt"`
  24. }
  25. // SSHRenew is an HTTP handler that reads an RenewSSHRequest with a one-time-token
  26. // (ott) from the body and creates a new SSH certificate with the information in
  27. // the request.
  28. func (h *caHandler) SSHRenew(w http.ResponseWriter, r *http.Request) {
  29. var body SSHRenewRequest
  30. if err := ReadJSON(r.Body, &body); err != nil {
  31. WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
  32. return
  33. }
  34. logOtt(w, body.OTT)
  35. if err := body.Validate(); err != nil {
  36. WriteError(w, BadRequest(err))
  37. return
  38. }
  39. ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.RenewSSHMethod)
  40. _, err := h.Authority.Authorize(ctx, body.OTT)
  41. if err != nil {
  42. WriteError(w, Unauthorized(err))
  43. return
  44. }
  45. oldCert, err := provisioner.ExtractSSHPOPCert(body.OTT)
  46. if err != nil {
  47. WriteError(w, InternalServerError(err))
  48. }
  49. newCert, err := h.Authority.RenewSSH(oldCert)
  50. if err != nil {
  51. WriteError(w, Forbidden(err))
  52. return
  53. }
  54. JSONStatus(w, &SSHSignResponse{
  55. Certificate: SSHCertificate{newCert},
  56. }, http.StatusCreated)
  57. }