2019-03-11 16:56:48 +00:00
|
|
|
package api
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"encoding/json"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2018-12-06 21:50:17 +00:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
2023-05-28 14:45:48 +00:00
|
|
|
"time"
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2024-03-08 13:07:55 +00:00
|
|
|
"github.com/go-jose/go-jose/v4"
|
2018-12-06 21:50:17 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-05-28 14:45:48 +00:00
|
|
|
func TestOrderService_NewWithOptions(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
mux, apiURL := tester.SetupFakeAPI(t)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
// small value keeps test fast
|
|
|
|
privateKey, errK := rsa.GenerateKey(rand.Reader, 512)
|
|
|
|
require.NoError(t, errK, "Could not generate test key")
|
|
|
|
|
|
|
|
mux.HandleFunc("/newOrder", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := readSignedBody(r, privateKey)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
2021-11-03 19:39:12 +00:00
|
|
|
return
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
order := acme.Order{}
|
|
|
|
err = json.Unmarshal(body, &order)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tester.WriteJSONResponse(w, acme.Order{
|
2023-05-28 14:45:48 +00:00
|
|
|
Status: acme.StatusValid,
|
|
|
|
Expires: order.Expires,
|
|
|
|
Identifiers: order.Identifiers,
|
|
|
|
NotBefore: order.NotBefore,
|
|
|
|
NotAfter: order.NotAfter,
|
|
|
|
Error: order.Error,
|
|
|
|
Authorizations: order.Authorizations,
|
|
|
|
Finalize: order.Finalize,
|
|
|
|
Certificate: order.Certificate,
|
2018-12-06 21:50:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
core, err := New(http.DefaultClient, "lego-test", apiURL+"/dir", "", privateKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-28 14:45:48 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
opts *OrderOptions
|
|
|
|
expected acme.ExtendedOrder
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "simple",
|
|
|
|
expected: acme.ExtendedOrder{
|
|
|
|
Order: acme.Order{
|
|
|
|
Status: "valid",
|
|
|
|
Identifiers: []acme.Identifier{{Type: "dns", Value: "example.com"}},
|
|
|
|
},
|
|
|
|
},
|
2018-12-06 21:50:17 +00:00
|
|
|
},
|
2023-05-28 14:45:48 +00:00
|
|
|
{
|
|
|
|
desc: "with options",
|
|
|
|
opts: &OrderOptions{
|
|
|
|
NotBefore: time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC),
|
|
|
|
NotAfter: time.Date(2023, 1, 2, 1, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
expected: acme.ExtendedOrder{
|
|
|
|
Order: acme.Order{
|
|
|
|
Status: "valid",
|
|
|
|
Identifiers: []acme.Identifier{{Type: "dns", Value: "example.com"}},
|
|
|
|
NotBefore: "2023-01-01T01:00:00Z",
|
|
|
|
NotAfter: "2023-01-02T01:00:00Z",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
order, err := core.Orders.NewWithOptions([]string{"example.com"}, test.opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, order)
|
|
|
|
})
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSignedBody(r *http.Request, privateKey *rsa.PrivateKey) ([]byte, error) {
|
2021-08-25 09:44:11 +00:00
|
|
|
reqBody, err := io.ReadAll(r.Body)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-08 13:07:55 +00:00
|
|
|
sigAlgs := []jose.SignatureAlgorithm{jose.RS256}
|
|
|
|
jws, err := jose.ParseSigned(string(reqBody), sigAlgs)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := jws.Verify(&jose.JSONWebKey{
|
|
|
|
Key: privateKey.Public(),
|
|
|
|
Algorithm: "RSA",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
}
|