types.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package acme
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. // ACME server response statuses used to describe Authorization and Challenge states.
  7. const (
  8. StatusUnknown = "unknown"
  9. StatusPending = "pending"
  10. StatusProcessing = "processing"
  11. StatusValid = "valid"
  12. StatusInvalid = "invalid"
  13. StatusRevoked = "revoked"
  14. )
  15. // Account is a user account. It is associated with a private key.
  16. type Account struct {
  17. // URI is the account unique ID, which is also a URL used to retrieve
  18. // account data from the CA.
  19. URI string
  20. // Contact is a slice of contact info used during registration.
  21. Contact []string
  22. // The terms user has agreed to.
  23. // Zero value indicates that the user hasn't agreed yet.
  24. AgreedTerms string
  25. // Actual terms of a CA.
  26. CurrentTerms string
  27. // Authz is the authorization URL used to initiate a new authz flow.
  28. Authz string
  29. // Authorizations is a URI from which a list of authorizations
  30. // granted to this account can be fetched via a GET request.
  31. Authorizations string
  32. // Certificates is a URI from which a list of certificates
  33. // issued for this account can be fetched via a GET request.
  34. Certificates string
  35. }
  36. // Directory is ACME server discovery data.
  37. type Directory struct {
  38. // RegURL is an account endpoint URL, allowing for creating new
  39. // and modifying existing accounts.
  40. RegURL string
  41. // AuthzURL is used to initiate Identifier Authorization flow.
  42. AuthzURL string
  43. // CertURL is a new certificate issuance endpoint URL.
  44. CertURL string
  45. // RevokeURL is used to initiate a certificate revocation flow.
  46. RevokeURL string
  47. // Term is a URI identifying the current terms of service.
  48. Terms string
  49. // Website is an HTTP or HTTPS URL locating a website
  50. // providing more information about the ACME server.
  51. Website string
  52. // CAA consists of lowercase hostname elements, which the ACME server
  53. // recognises as referring to itself for the purposes of CAA record validation
  54. // as defined in RFC6844.
  55. CAA []string
  56. }
  57. // Challenge encodes a returned CA challenge.
  58. type Challenge struct {
  59. // Type is the challenge type, e.g. "http-01", "tls-sni-02", "dns-01".
  60. Type string
  61. // URI is where a challenge response can be posted to.
  62. URI string
  63. // Token is a random value that uniquely identifies the challenge.
  64. Token string
  65. // Status identifies the status of this challenge.
  66. Status string
  67. }
  68. // Authorization encodes an authorization response.
  69. type Authorization struct {
  70. // URI uniquely identifies a authorization.
  71. URI string
  72. // Status identifies the status of an authorization.
  73. Status string
  74. // Identifier is what the account is authorized to represent.
  75. Identifier AuthzID
  76. // Challenges that the client needs to fulfill in order to prove possession
  77. // of the identifier (for pending authorizations).
  78. // For final authorizations, the challenges that were used.
  79. Challenges []*Challenge
  80. // A collection of sets of challenges, each of which would be sufficient
  81. // to prove possession of the identifier.
  82. // Clients must complete a set of challenges that covers at least one set.
  83. // Challenges are identified by their indices in the challenges array.
  84. // If this field is empty, the client needs to complete all challenges.
  85. Combinations [][]int
  86. }
  87. // AuthzID is an identifier that an account is authorized to represent.
  88. type AuthzID struct {
  89. Type string // The type of identifier, e.g. "dns".
  90. Value string // The identifier itself, e.g. "example.org".
  91. }
  92. // Error is an ACME error, defined in Problem Details for HTTP APIs doc
  93. // http://tools.ietf.org/html/draft-ietf-appsawg-http-problem.
  94. type Error struct {
  95. // StatusCode is The HTTP status code generated by the origin server.
  96. StatusCode int
  97. // ProblemType is a URI reference that identifies the problem type,
  98. // typically in a "urn:acme:error:xxx" form.
  99. ProblemType string
  100. // Detail is a human-readable explanation specific to this occurrence of the problem.
  101. Detail string
  102. // Header is the original server error response headers.
  103. Header http.Header
  104. }
  105. func (e *Error) Error() string {
  106. return fmt.Sprintf("%d %s: %s", e.StatusCode, e.ProblemType, e.Detail)
  107. }
  108. // wireAuthz is ACME JSON representation of Authorization objects.
  109. type wireAuthz struct {
  110. Status string
  111. Challenges []wireChallenge
  112. Combinations [][]int
  113. Identifier struct {
  114. Type string
  115. Value string
  116. }
  117. }
  118. func (z *wireAuthz) authorization(uri string) *Authorization {
  119. a := &Authorization{
  120. URI: uri,
  121. Status: z.Status,
  122. Identifier: AuthzID{Type: z.Identifier.Type, Value: z.Identifier.Value},
  123. Combinations: z.Combinations, // shallow copy
  124. Challenges: make([]*Challenge, len(z.Challenges)),
  125. }
  126. for i, v := range z.Challenges {
  127. a.Challenges[i] = v.challenge()
  128. }
  129. return a
  130. }
  131. // wireChallenge is ACME JSON challenge representation.
  132. type wireChallenge struct {
  133. URI string `json:"uri"`
  134. Type string
  135. Token string
  136. Status string
  137. }
  138. func (c *wireChallenge) challenge() *Challenge {
  139. v := &Challenge{
  140. URI: c.URI,
  141. Type: c.Type,
  142. Token: c.Token,
  143. Status: c.Status,
  144. }
  145. if v.Status == "" {
  146. v.Status = StatusPending
  147. }
  148. return v
  149. }