2019-03-11 16:56:48 +00:00
|
|
|
package api
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrderService service
|
|
|
|
|
|
|
|
// New Creates a new order.
|
|
|
|
func (o *OrderService) New(domains []string) (acme.ExtendedOrder, error) {
|
|
|
|
var identifiers []acme.Identifier
|
|
|
|
for _, domain := range domains {
|
|
|
|
identifiers = append(identifiers, acme.Identifier{Type: "dns", Value: domain})
|
|
|
|
}
|
|
|
|
|
|
|
|
orderReq := acme.Order{Identifiers: identifiers}
|
|
|
|
|
|
|
|
var order acme.Order
|
|
|
|
resp, err := o.core.post(o.core.GetDirectory().NewOrderURL, orderReq, &order)
|
|
|
|
if err != nil {
|
|
|
|
return acme.ExtendedOrder{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return acme.ExtendedOrder{
|
2020-09-02 00:22:53 +00:00
|
|
|
Order: order,
|
|
|
|
Location: resp.Header.Get("Location"),
|
|
|
|
AlternateChainLinks: getLinks(resp.Header, "alternate"),
|
2018-12-06 21:50:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Gets an order.
|
2020-09-02 00:22:53 +00:00
|
|
|
func (o *OrderService) Get(orderURL string) (acme.ExtendedOrder, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
if len(orderURL) == 0 {
|
2020-09-02 00:22:53 +00:00
|
|
|
return acme.ExtendedOrder{}, errors.New("order[get]: empty URL")
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var order acme.Order
|
2020-09-02 00:22:53 +00:00
|
|
|
resp, err := o.core.postAsGet(orderURL, &order)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
2020-09-02 00:22:53 +00:00
|
|
|
return acme.ExtendedOrder{}, err
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 00:22:53 +00:00
|
|
|
return acme.ExtendedOrder{
|
|
|
|
Order: order,
|
|
|
|
AlternateChainLinks: getLinks(resp.Header, "alternate"),
|
|
|
|
}, nil
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateForCSR Updates an order for a CSR.
|
2020-09-02 00:22:53 +00:00
|
|
|
func (o *OrderService) UpdateForCSR(orderURL string, csr []byte) (acme.ExtendedOrder, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
csrMsg := acme.CSRMessage{
|
|
|
|
Csr: base64.RawURLEncoding.EncodeToString(csr),
|
|
|
|
}
|
|
|
|
|
|
|
|
var order acme.Order
|
2020-09-02 00:22:53 +00:00
|
|
|
resp, err := o.core.post(orderURL, csrMsg, &order)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
2020-09-02 00:22:53 +00:00
|
|
|
return acme.ExtendedOrder{}, err
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if order.Status == acme.StatusInvalid {
|
2020-09-02 00:22:53 +00:00
|
|
|
return acme.ExtendedOrder{}, order.Error
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 00:22:53 +00:00
|
|
|
return acme.ExtendedOrder{
|
|
|
|
Order: order,
|
|
|
|
AlternateChainLinks: getLinks(resp.Header, "alternate"),
|
|
|
|
}, nil
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|