options.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package acme
  2. import (
  3. "crypto"
  4. "crypto/hmac"
  5. "crypto/tls"
  6. "crypto/x509"
  7. "encoding/base64"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "net/http"
  12. "time"
  13. )
  14. // OptionFunc function prototype for passing options to NewClient
  15. type OptionFunc func(client *Client) error
  16. // WithHTTPTimeout sets a timeout on the http client used by the Client
  17. func WithHTTPTimeout(duration time.Duration) OptionFunc {
  18. return func(client *Client) error {
  19. client.httpClient.Timeout = duration
  20. return nil
  21. }
  22. }
  23. // WithInsecureSkipVerify sets InsecureSkipVerify on the http client transport tls client config used by the Client
  24. func WithInsecureSkipVerify() OptionFunc {
  25. return func(client *Client) error {
  26. client.httpClient.Transport = &http.Transport{
  27. TLSClientConfig: &tls.Config{
  28. InsecureSkipVerify: true,
  29. },
  30. }
  31. return nil
  32. }
  33. }
  34. // WithUserAgentSuffix appends a user agent suffix for http requests to acme resources
  35. func WithUserAgentSuffix(userAgentSuffix string) OptionFunc {
  36. return func(client *Client) error {
  37. client.userAgentSuffix = userAgentSuffix
  38. return nil
  39. }
  40. }
  41. // WithAcceptLanguage sets an Accept-Language header on http requests
  42. func WithAcceptLanguage(acceptLanguage string) OptionFunc {
  43. return func(client *Client) error {
  44. client.acceptLanguage = acceptLanguage
  45. return nil
  46. }
  47. }
  48. // WithRetryCount sets the number of times the acme client retries when receiving an api error (eg, nonce failures, etc).
  49. // Default: 5
  50. func WithRetryCount(retryCount int) OptionFunc {
  51. return func(client *Client) error {
  52. if retryCount < 1 {
  53. return errors.New("retryCount must be > 0")
  54. }
  55. client.retryCount = retryCount
  56. return nil
  57. }
  58. }
  59. // WithHTTPClient Allows setting a custom http client for acme connections
  60. func WithHTTPClient(httpClient *http.Client) OptionFunc {
  61. return func(client *Client) error {
  62. if httpClient == nil {
  63. return errors.New("client must not be nil")
  64. }
  65. client.httpClient = httpClient
  66. return nil
  67. }
  68. }
  69. // WithRootCerts sets the httpclient transport to use a given certpool for root certs
  70. func WithRootCerts(pool *x509.CertPool) OptionFunc {
  71. return func(client *Client) error {
  72. client.httpClient.Transport = &http.Transport{
  73. TLSClientConfig: &tls.Config{
  74. RootCAs: pool,
  75. },
  76. }
  77. return nil
  78. }
  79. }
  80. // NewAccountOptionFunc function prototype for passing options to NewClient
  81. type NewAccountOptionFunc func(crypto.Signer, *Account, *NewAccountRequest, Client) error
  82. // NewAcctOptOnlyReturnExisting sets the new client request to only return existing accounts
  83. func NewAcctOptOnlyReturnExisting() NewAccountOptionFunc {
  84. return func(privateKey crypto.Signer, account *Account, request *NewAccountRequest, client Client) error {
  85. request.OnlyReturnExisting = true
  86. return nil
  87. }
  88. }
  89. // NewAcctOptAgreeTOS sets the new account request as agreeing to the terms of service
  90. func NewAcctOptAgreeTOS() NewAccountOptionFunc {
  91. return func(privateKey crypto.Signer, account *Account, request *NewAccountRequest, client Client) error {
  92. request.TermsOfServiceAgreed = true
  93. return nil
  94. }
  95. }
  96. // NewAcctOptWithContacts adds contacts to a new account request
  97. func NewAcctOptWithContacts(contacts ...string) NewAccountOptionFunc {
  98. return func(privateKey crypto.Signer, account *Account, request *NewAccountRequest, client Client) error {
  99. request.Contact = contacts
  100. return nil
  101. }
  102. }
  103. // NewAcctOptExternalAccountBinding adds an external account binding to the new account request
  104. // Code adopted from jwsEncodeJSON
  105. func NewAcctOptExternalAccountBinding(binding ExternalAccountBinding) NewAccountOptionFunc {
  106. return func(privateKey crypto.Signer, account *Account, request *NewAccountRequest, client Client) error {
  107. if binding.KeyIdentifier == "" {
  108. return errors.New("acme: NewAcctOptExternalAccountBinding has no KeyIdentifier set")
  109. }
  110. if binding.MacKey == "" {
  111. return errors.New("acme: NewAcctOptExternalAccountBinding has no MacKey set")
  112. }
  113. if binding.Algorithm == "" {
  114. return errors.New("acme: NewAcctOptExternalAccountBinding has no Algorithm set")
  115. }
  116. if binding.HashFunc == 0 {
  117. return errors.New("acme: NewAcctOptExternalAccountBinding has no HashFunc set")
  118. }
  119. jwk, err := jwkEncode(privateKey.Public())
  120. if err != nil {
  121. return fmt.Errorf("acme: external account binding error encoding public key: %v", err)
  122. }
  123. payload := base64.RawURLEncoding.EncodeToString([]byte(jwk))
  124. phead := fmt.Sprintf(`{"alg":%q,"kid":%q,"url":%q}`,
  125. binding.Algorithm, binding.KeyIdentifier, client.Directory().NewAccount)
  126. phead = base64.RawURLEncoding.EncodeToString([]byte(phead))
  127. decodedAccountMac, err := base64.RawURLEncoding.DecodeString(binding.MacKey)
  128. if err != nil {
  129. return fmt.Errorf("acme: external account binding error decoding mac key: %v", err)
  130. }
  131. macHash := hmac.New(binding.HashFunc.New, decodedAccountMac)
  132. if _, err := macHash.Write([]byte(phead + "." + payload)); err != nil {
  133. return err
  134. }
  135. enc := struct {
  136. Protected string `json:"protected"`
  137. Payload string `json:"payload"`
  138. Sig string `json:"signature"`
  139. }{
  140. Protected: phead,
  141. Payload: payload,
  142. Sig: base64.RawURLEncoding.EncodeToString(macHash.Sum(nil)),
  143. }
  144. jwsEab, err := json.Marshal(&enc)
  145. if err != nil {
  146. return fmt.Errorf("acme: external account binding error marshalling struct: %v", err)
  147. }
  148. request.ExternalAccountBinding = jwsEab
  149. account.ExternalAccountBinding = binding
  150. return nil
  151. }
  152. }