authorization.go 1.6 KB

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