2019-03-11 18:56:48 +02:00
|
|
|
package api
|
2018-12-06 23:50:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-09 06:40:45 +02:00
|
|
|
"context"
|
2018-12-06 23:50:17 +02:00
|
|
|
"crypto"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-02-09 06:40:45 +02:00
|
|
|
"time"
|
2018-12-06 23:50:17 +02:00
|
|
|
|
2020-01-12 18:16:46 +02:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
|
|
|
"github.com/go-acme/lego/v4/acme/api/internal/nonces"
|
|
|
|
"github.com/go-acme/lego/v4/acme/api/internal/secure"
|
|
|
|
"github.com/go-acme/lego/v4/acme/api/internal/sender"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
2018-12-06 23:50:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Core ACME/LE core API.
|
|
|
|
type Core struct {
|
|
|
|
doer *sender.Doer
|
|
|
|
nonceManager *nonces.Manager
|
|
|
|
jws *secure.JWS
|
|
|
|
directory acme.Directory
|
|
|
|
HTTPClient *http.Client
|
|
|
|
|
|
|
|
common service // Reuse a single struct instead of allocating one for each service on the heap.
|
|
|
|
Accounts *AccountService
|
|
|
|
Authorizations *AuthorizationService
|
|
|
|
Certificates *CertificateService
|
|
|
|
Challenges *ChallengeService
|
|
|
|
Orders *OrderService
|
|
|
|
}
|
|
|
|
|
|
|
|
// New Creates a new Core.
|
2020-07-10 01:48:18 +02:00
|
|
|
func New(httpClient *http.Client, userAgent, caDirURL, kid string, privateKey crypto.PrivateKey) (*Core, error) {
|
2018-12-06 23:50:17 +02:00
|
|
|
doer := sender.NewDoer(httpClient, userAgent)
|
|
|
|
|
|
|
|
dir, err := getDirectory(doer, caDirURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonceManager := nonces.NewManager(doer, dir.NewNonceURL)
|
|
|
|
|
|
|
|
jws := secure.NewJWS(privateKey, kid, nonceManager)
|
|
|
|
|
2019-01-12 01:17:29 +02:00
|
|
|
c := &Core{doer: doer, nonceManager: nonceManager, jws: jws, directory: dir, HTTPClient: httpClient}
|
2018-12-06 23:50:17 +02:00
|
|
|
|
|
|
|
c.common.core = c
|
|
|
|
c.Accounts = (*AccountService)(&c.common)
|
|
|
|
c.Authorizations = (*AuthorizationService)(&c.common)
|
|
|
|
c.Certificates = (*CertificateService)(&c.common)
|
|
|
|
c.Challenges = (*ChallengeService)(&c.common)
|
|
|
|
c.Orders = (*OrderService)(&c.common)
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// post performs an HTTP POST request and parses the response body as JSON,
|
|
|
|
// into the provided respBody object.
|
|
|
|
func (a *Core) post(uri string, reqBody, response interface{}) (*http.Response, error) {
|
|
|
|
content, err := json.Marshal(reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("failed to marshal message")
|
|
|
|
}
|
|
|
|
|
2019-02-09 06:40:45 +02:00
|
|
|
return a.retrievablePost(uri, content, response)
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// postAsGet performs an HTTP POST ("POST-as-GET") request.
|
2019-11-05 18:58:30 +02:00
|
|
|
// https://tools.ietf.org/html/rfc8555#section-6.3
|
2018-12-06 23:50:17 +02:00
|
|
|
func (a *Core) postAsGet(uri string, response interface{}) (*http.Response, error) {
|
2019-02-09 06:40:45 +02:00
|
|
|
return a.retrievablePost(uri, []byte{}, response)
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 06:40:45 +02:00
|
|
|
func (a *Core) retrievablePost(uri string, content []byte, response interface{}) (*http.Response, error) {
|
|
|
|
// during tests, allow to support ~90% of bad nonce with a minimum of attempts.
|
|
|
|
bo := backoff.NewExponentialBackOff()
|
|
|
|
bo.InitialInterval = 200 * time.Millisecond
|
|
|
|
bo.MaxInterval = 5 * time.Second
|
|
|
|
bo.MaxElapsedTime = 20 * time.Second
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
var resp *http.Response
|
|
|
|
operation := func() error {
|
|
|
|
var err error
|
|
|
|
resp, err = a.signedPost(uri, content, response)
|
|
|
|
if err != nil {
|
|
|
|
// Retry if the nonce was invalidated
|
2020-10-27 13:01:05 +02:00
|
|
|
var e *acme.NonceError
|
|
|
|
if errors.As(err, &e) {
|
2019-02-09 06:40:45 +02:00
|
|
|
return err
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
2020-10-27 13:01:05 +02:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
return err
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
2019-02-09 06:40:45 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:20:52 +02:00
|
|
|
notify := func(err error, duration time.Duration) {
|
|
|
|
log.Infof("retry due to: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := backoff.RetryNotify(operation, backoff.WithContext(bo, ctx), notify)
|
2019-02-09 06:40:45 +02:00
|
|
|
if err != nil {
|
2020-10-27 13:01:05 +02:00
|
|
|
return resp, err
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Core) signedPost(uri string, content []byte, response interface{}) (*http.Response, error) {
|
|
|
|
signedContent, err := a.jws.SignContent(uri, content)
|
|
|
|
if err != nil {
|
2020-03-20 23:53:09 +02:00
|
|
|
return nil, fmt.Errorf("failed to post JWS message: failed to sign content: %w", err)
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
signedBody := bytes.NewBuffer([]byte(signedContent.FullSerialize()))
|
|
|
|
|
|
|
|
resp, err := a.doer.Post(uri, signedBody, "application/jose+json", response)
|
|
|
|
|
|
|
|
// nonceErr is ignored to keep the root error.
|
|
|
|
nonce, nonceErr := nonces.GetFromResponse(resp)
|
|
|
|
if nonceErr == nil {
|
|
|
|
a.nonceManager.Push(nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Core) signEABContent(newAccountURL, kid string, hmac []byte) ([]byte, error) {
|
|
|
|
eabJWS, err := a.jws.SignEABContent(newAccountURL, kid, hmac)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(eabJWS.FullSerialize()), nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// GetKeyAuthorization Gets the key authorization.
|
2018-12-06 23:50:17 +02:00
|
|
|
func (a *Core) GetKeyAuthorization(token string) (string, error) {
|
|
|
|
return a.jws.GetKeyAuthorization(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Core) GetDirectory() acme.Directory {
|
|
|
|
return a.directory
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDirectory(do *sender.Doer, caDirURL string) (acme.Directory, error) {
|
|
|
|
var dir acme.Directory
|
|
|
|
if _, err := do.Get(caDirURL, &dir); err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return dir, fmt.Errorf("get directory at '%s': %w", caDirURL, err)
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if dir.NewAccountURL == "" {
|
|
|
|
return dir, errors.New("directory missing new registration URL")
|
|
|
|
}
|
|
|
|
if dir.NewOrderURL == "" {
|
|
|
|
return dir, errors.New("directory missing new order URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, nil
|
|
|
|
}
|