directory.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package acme
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "github.com/pkg/errors"
  8. )
  9. // Directory represents an ACME directory for configuring clients.
  10. type Directory struct {
  11. NewNonce string `json:"newNonce,omitempty"`
  12. NewAccount string `json:"newAccount,omitempty"`
  13. NewOrder string `json:"newOrder,omitempty"`
  14. NewAuthz string `json:"newAuthz,omitempty"`
  15. RevokeCert string `json:"revokeCert,omitempty"`
  16. KeyChange string `json:"keyChange,omitempty"`
  17. }
  18. // ToLog enables response logging for the Directory type.
  19. func (d *Directory) ToLog() (interface{}, error) {
  20. b, err := json.Marshal(d)
  21. if err != nil {
  22. return nil, ServerInternalErr(errors.Wrap(err, "error marshaling directory for logging"))
  23. }
  24. return string(b), nil
  25. }
  26. type directory struct {
  27. prefix, dns string
  28. }
  29. // newDirectory returns a new Directory type.
  30. func newDirectory(dns, prefix string) *directory {
  31. return &directory{prefix: prefix, dns: dns}
  32. }
  33. // Link captures the link type.
  34. type Link int
  35. const (
  36. // NewNonceLink new-nonce
  37. NewNonceLink Link = iota
  38. // NewAccountLink new-account
  39. NewAccountLink
  40. // AccountLink account
  41. AccountLink
  42. // OrderLink order
  43. OrderLink
  44. // NewOrderLink new-order
  45. NewOrderLink
  46. // OrdersByAccountLink list of orders owned by account
  47. OrdersByAccountLink
  48. // FinalizeLink finalize order
  49. FinalizeLink
  50. // NewAuthzLink authz
  51. NewAuthzLink
  52. // AuthzLink new-authz
  53. AuthzLink
  54. // ChallengeLink challenge
  55. ChallengeLink
  56. // CertificateLink certificate
  57. CertificateLink
  58. // DirectoryLink directory
  59. DirectoryLink
  60. // RevokeCertLink revoke certificate
  61. RevokeCertLink
  62. // KeyChangeLink key rollover
  63. KeyChangeLink
  64. )
  65. func (l Link) String() string {
  66. switch l {
  67. case NewNonceLink:
  68. return "new-nonce"
  69. case NewAccountLink:
  70. return "new-account"
  71. case AccountLink:
  72. return "account"
  73. case NewOrderLink:
  74. return "new-order"
  75. case OrderLink:
  76. return "order"
  77. case NewAuthzLink:
  78. return "new-authz"
  79. case AuthzLink:
  80. return "authz"
  81. case ChallengeLink:
  82. return "challenge"
  83. case CertificateLink:
  84. return "certificate"
  85. case DirectoryLink:
  86. return "directory"
  87. case RevokeCertLink:
  88. return "revoke-cert"
  89. case KeyChangeLink:
  90. return "key-change"
  91. default:
  92. return "unexpected"
  93. }
  94. }
  95. func (d *directory) getLink(ctx context.Context, typ Link, abs bool, inputs ...string) string {
  96. var provName string
  97. if p, err := ProvisionerFromContext(ctx); err == nil && p != nil {
  98. provName = p.GetName()
  99. }
  100. return d.getLinkExplicit(typ, provName, abs, BaseURLFromContext(ctx), inputs...)
  101. }
  102. // getLinkExplicit returns an absolute or partial path to the given resource and a base
  103. // URL dynamically obtained from the request for which the link is being
  104. // calculated.
  105. func (d *directory) getLinkExplicit(typ Link, provisionerName string, abs bool, baseURL *url.URL, inputs ...string) string {
  106. var link string
  107. switch typ {
  108. case NewNonceLink, NewAccountLink, NewOrderLink, NewAuthzLink, DirectoryLink, KeyChangeLink, RevokeCertLink:
  109. link = fmt.Sprintf("/%s/%s", provisionerName, typ.String())
  110. case AccountLink, OrderLink, AuthzLink, ChallengeLink, CertificateLink:
  111. link = fmt.Sprintf("/%s/%s/%s", provisionerName, typ.String(), inputs[0])
  112. case OrdersByAccountLink:
  113. link = fmt.Sprintf("/%s/%s/%s/orders", provisionerName, AccountLink.String(), inputs[0])
  114. case FinalizeLink:
  115. link = fmt.Sprintf("/%s/%s/%s/finalize", provisionerName, OrderLink.String(), inputs[0])
  116. }
  117. if abs {
  118. // Copy the baseURL value from the pointer. https://github.com/golang/go/issues/38351
  119. u := url.URL{}
  120. if baseURL != nil {
  121. u = *baseURL
  122. }
  123. // If no Scheme is set, then default to https.
  124. if u.Scheme == "" {
  125. u.Scheme = "https"
  126. }
  127. // If no Host is set, then use the default (first DNS attr in the ca.json).
  128. if u.Host == "" {
  129. u.Host = d.dns
  130. }
  131. u.Path = d.prefix + link
  132. return u.String()
  133. }
  134. return link
  135. }