authorization.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package acme
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "github.com/smallstep/certificates/webhook"
  7. )
  8. // Authorization representst an ACME Authorization.
  9. type Authorization struct {
  10. ID string `json:"-"`
  11. AccountID string `json:"-"`
  12. Token string `json:"-"`
  13. Fingerprint string `json:"-"`
  14. // Attestation is the evidence a remote-attest-01 challenge of the
  15. // authorization verified.
  16. Attestation *webhook.Attestation `json:"-"`
  17. Identifier Identifier `json:"identifier"`
  18. Status Status `json:"status"`
  19. Challenges []*Challenge `json:"challenges"`
  20. Wildcard bool `json:"wildcard"`
  21. ExpiresAt time.Time `json:"expires"`
  22. Error *Error `json:"error,omitempty"`
  23. }
  24. // ToLog enables response logging.
  25. func (az *Authorization) ToLog() (any, error) {
  26. b, err := json.Marshal(az)
  27. if err != nil {
  28. return nil, WrapErrorISE(err, "error marshaling authz for logging")
  29. }
  30. return string(b), nil
  31. }
  32. // UpdateStatus updates the ACME Authorization Status if necessary.
  33. // Changes to the Authorization are saved using the database interface.
  34. func (az *Authorization) UpdateStatus(ctx context.Context, db DB) error {
  35. now := clock.Now()
  36. switch az.Status {
  37. case StatusInvalid:
  38. return nil
  39. case StatusValid:
  40. return nil
  41. case StatusPending:
  42. // check expiry
  43. if now.After(az.ExpiresAt) {
  44. az.Status = StatusInvalid
  45. break
  46. }
  47. var isValid = false
  48. for _, ch := range az.Challenges {
  49. if ch.Status == StatusValid {
  50. isValid = true
  51. break
  52. }
  53. }
  54. if !isValid {
  55. return nil
  56. }
  57. az.Status = StatusValid
  58. az.Error = nil
  59. default:
  60. return NewErrorISE("unrecognized authorization status: %s", az.Status)
  61. }
  62. if err := db.UpdateAuthorization(ctx, az); err != nil {
  63. return WrapErrorISE(err, "error updating authorization")
  64. }
  65. return nil
  66. }