mirror of
https://github.com/1f349/orchid.git
synced 2024-11-09 23:02:53 +00:00
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package http_acme
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/go-acme/lego/v4/challenge"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
var _ challenge.Provider = &HttpAcmeProvider{}
|
|
|
|
type HttpAcmeProvider struct {
|
|
accessToken, refreshToken string
|
|
apiUrlPresent, apiUrlCleanUp string
|
|
trip http.RoundTripper
|
|
}
|
|
|
|
func NewCustomHTTPProvider(accessToken, refreshToken, apiUrlPresent, apiUrlCleanUp string) *HttpAcmeProvider {
|
|
return &HttpAcmeProvider{accessToken, refreshToken, apiUrlPresent, apiUrlCleanUp, http.DefaultTransport}
|
|
}
|
|
|
|
func (h *HttpAcmeProvider) Present(domain, token, keyAuth string) error {
|
|
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
|
|
}
|
|
|
|
func (h *HttpAcmeProvider) CleanUp(domain, token, keyAuth string) error {
|
|
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
|
|
}
|