2019-03-11 18:56:48 +02:00
|
|
|
package api
|
2018-12-06 23:50:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
2023-05-02 19:02:18 +02:00
|
|
|
"net"
|
2023-05-28 16:45:48 +02:00
|
|
|
"time"
|
2018-12-06 23:50:17 +02:00
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
2018-12-06 23:50:17 +02:00
|
|
|
)
|
|
|
|
|
2023-05-28 16:45:48 +02:00
|
|
|
// OrderOptions used to create an order (optional).
|
|
|
|
type OrderOptions struct {
|
|
|
|
NotBefore time.Time
|
|
|
|
NotAfter time.Time
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
type OrderService service
|
|
|
|
|
|
|
|
// New Creates a new order.
|
|
|
|
func (o *OrderService) New(domains []string) (acme.ExtendedOrder, error) {
|
2023-05-28 16:45:48 +02:00
|
|
|
return o.NewWithOptions(domains, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithOptions Creates a new order.
|
|
|
|
func (o *OrderService) NewWithOptions(domains []string, opts *OrderOptions) (acme.ExtendedOrder, error) {
|
2018-12-06 23:50:17 +02:00
|
|
|
var identifiers []acme.Identifier
|
|
|
|
for _, domain := range domains {
|
2023-05-02 19:02:18 +02:00
|
|
|
ident := acme.Identifier{Value: domain, Type: "dns"}
|
|
|
|
|
|
|
|
if net.ParseIP(domain) != nil {
|
|
|
|
ident.Type = "ip"
|
|
|
|
}
|
|
|
|
|
|
|
|
identifiers = append(identifiers, ident)
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
orderReq := acme.Order{Identifiers: identifiers}
|
|
|
|
|
2023-05-28 16:45:48 +02:00
|
|
|
if opts != nil {
|
|
|
|
if !opts.NotAfter.IsZero() {
|
|
|
|
orderReq.NotAfter = opts.NotAfter.Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !opts.NotBefore.IsZero() {
|
|
|
|
orderReq.NotBefore = opts.NotBefore.Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
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-11-21 21:24:11 +02:00
|
|
|
Order: order,
|
|
|
|
Location: resp.Header.Get("Location"),
|
2018-12-06 23:50:17 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Gets an order.
|
2020-09-02 02:22:53 +02:00
|
|
|
func (o *OrderService) Get(orderURL string) (acme.ExtendedOrder, error) {
|
2021-03-04 21:16:59 +02:00
|
|
|
if orderURL == "" {
|
2020-09-02 02:22:53 +02:00
|
|
|
return acme.ExtendedOrder{}, errors.New("order[get]: empty URL")
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var order acme.Order
|
2020-11-21 21:24:11 +02:00
|
|
|
_, err := o.core.postAsGet(orderURL, &order)
|
2018-12-06 23:50:17 +02:00
|
|
|
if err != nil {
|
2020-09-02 02:22:53 +02:00
|
|
|
return acme.ExtendedOrder{}, err
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 21:24:11 +02:00
|
|
|
return acme.ExtendedOrder{Order: order}, nil
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateForCSR Updates an order for a CSR.
|
2020-09-02 02:22:53 +02:00
|
|
|
func (o *OrderService) UpdateForCSR(orderURL string, csr []byte) (acme.ExtendedOrder, error) {
|
2018-12-06 23:50:17 +02:00
|
|
|
csrMsg := acme.CSRMessage{
|
|
|
|
Csr: base64.RawURLEncoding.EncodeToString(csr),
|
|
|
|
}
|
|
|
|
|
|
|
|
var order acme.Order
|
2020-11-21 21:24:11 +02:00
|
|
|
_, err := o.core.post(orderURL, csrMsg, &order)
|
2018-12-06 23:50:17 +02:00
|
|
|
if err != nil {
|
2020-09-02 02:22:53 +02:00
|
|
|
return acme.ExtendedOrder{}, err
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if order.Status == acme.StatusInvalid {
|
2020-09-02 02:22:53 +02:00
|
|
|
return acme.ExtendedOrder{}, order.Error
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 21:24:11 +02:00
|
|
|
return acme.ExtendedOrder{Order: order}, nil
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|