orchid/http-acme/http-acme-provider.go

59 lines
1.6 KiB
Go
Raw Normal View History

2023-06-26 11:56:21 +01:00
package http_acme
2023-06-21 23:09:33 +01:00
import (
2023-06-23 23:00:09 +01:00
"fmt"
2023-06-21 23:09:33 +01:00
"github.com/go-acme/lego/v4/challenge"
2023-06-23 23:00:09 +01:00
"net/http"
"strings"
2023-06-21 23:09:33 +01:00
)
var _ challenge.Provider = &HttpAcmeProvider{}
type HttpAcmeProvider struct {
accessToken, refreshToken string
apiUrlPresent, apiUrlCleanUp string
2023-06-23 23:00:09 +01:00
trip http.RoundTripper
2023-06-21 23:09:33 +01:00
}
func NewCustomHTTPProvider(accessToken, refreshToken, apiUrlPresent, apiUrlCleanUp string) *HttpAcmeProvider {
2023-06-23 23:00:09 +01:00
return &HttpAcmeProvider{accessToken, refreshToken, apiUrlPresent, apiUrlCleanUp, http.DefaultTransport}
2023-06-21 23:09:33 +01:00
}
func (h *HttpAcmeProvider) Present(domain, token, keyAuth string) error {
2023-06-23 23:00:09 +01:00
v := strings.NewReplacer("%domain%", domain, "%token%", token, "%content%", keyAuth).Replace(h.apiUrlPresent)
req, err := http.NewRequest(http.MethodPut, v, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+h.accessToken)
// round trip
trip, err := h.trip.RoundTrip(req)
if err != nil {
return err
}
if trip.StatusCode != http.StatusOK {
return fmt.Errorf("Trip response status code was not 200")
}
return nil
2023-06-21 23:09:33 +01:00
}
func (h *HttpAcmeProvider) CleanUp(domain, token, keyAuth string) error {
2023-06-23 23:00:09 +01:00
v := strings.NewReplacer("%domain%", domain, "%token%", token, "%content%", keyAuth).Replace(h.apiUrlCleanUp)
req, err := http.NewRequest(http.MethodPut, v, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+h.accessToken)
// round trip
trip, err := h.trip.RoundTrip(req)
if err != nil {
return err
}
if trip.StatusCode != http.StatusOK {
return fmt.Errorf("Trip response status code was not 200")
}
return nil
2023-06-21 23:09:33 +01:00
}