telebit/internal/http01fs/http01fs.go

54 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-06-05 09:41:00 +00:00
package http01fs
import (
"context"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/mholt/acmez/acme"
)
const (
challengeDir = ".well-known/acme-challenge"
tmpBase = "acme-tmp"
)
2022-06-05 09:53:13 +00:00
// Provider is the default Challenge Solver Provider
2022-06-05 09:41:00 +00:00
var Provider Solver
// Solver implements the challenge.Provider interface.
type Solver struct {
Path string
}
// Present creates a HTTP-01 Challenge Token
func (s *Solver) Present(ctx context.Context, ch acme.Challenge) error {
2022-06-05 09:53:13 +00:00
log.Println("Present HTTP-01 (fs) challenge solution for", ch.Identifier.Value)
2022-06-05 09:41:00 +00:00
2022-06-05 09:53:13 +00:00
if 0 == len(s.Path) {
s.Path = tmpBase
2022-06-05 09:41:00 +00:00
}
2022-06-05 09:53:13 +00:00
challengeBase := filepath.Join(s.Path, ch.Identifier.Value, challengeDir)
2022-06-05 09:41:00 +00:00
_ = os.MkdirAll(challengeBase, 0700)
tokenPath := filepath.Join(challengeBase, ch.Token)
return ioutil.WriteFile(tokenPath, []byte(ch.KeyAuthorization), 0600)
}
// CleanUp deletes an HTTP-01 Challenge Token
func (s *Solver) CleanUp(ctx context.Context, ch acme.Challenge) error {
2022-06-05 09:53:13 +00:00
log.Println("CleanUp HTTP-01 (fs) challenge solution for", ch.Identifier.Value)
2022-06-05 09:41:00 +00:00
2022-06-05 09:53:13 +00:00
if 0 == len(s.Path) {
s.Path = tmpBase
2022-06-05 09:41:00 +00:00
}
// always try to remove, as there's no harm
2022-06-05 09:53:13 +00:00
tokenPath := filepath.Join(s.Path, ch.Identifier.Value, challengeDir, ch.Token)
2022-06-05 09:41:00 +00:00
_ = os.Remove(tokenPath)
return nil
}