4
0

authorization.go 1.6 KB

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