authorization.go 1.5 KB

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