lego/providers/http/webroot/webroot.go

55 lines
1.6 KiB
Go
Raw Normal View History

// Package webroot implements a HTTP provider for solving the HTTP-01 challenge using web server's root path.
2019-03-11 16:56:48 +00:00
package webroot
import (
2020-02-27 18:14:46 +00:00
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/challenge/http01"
)
2020-05-08 17:35:25 +00:00
// HTTPProvider implements ChallengeProvider for `http-01` challenge.
type HTTPProvider struct {
path string
}
2020-05-08 17:35:25 +00:00
// NewHTTPProvider returns a HTTPProvider instance with a configured webroot path.
func NewHTTPProvider(path string) (*HTTPProvider, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
2020-02-27 18:14:46 +00:00
return nil, errors.New("webroot path does not exist")
}
return &HTTPProvider{path: path}, nil
}
2020-05-08 17:35:25 +00:00
// Present makes the token available at `HTTP01ChallengePath(token)` by creating a file in the given webroot path.
func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
var err error
challengeFilePath := filepath.Join(w.path, http01.ChallengePath(token))
2020-07-09 23:48:18 +00:00
err = os.MkdirAll(filepath.Dir(challengeFilePath), 0o755)
if err != nil {
return fmt.Errorf("could not create required directories in webroot for HTTP challenge: %w", err)
}
2020-07-09 23:48:18 +00:00
err = ioutil.WriteFile(challengeFilePath, []byte(keyAuth), 0o644)
if err != nil {
return fmt.Errorf("could not write file in webroot for HTTP challenge: %w", err)
}
return nil
}
2020-05-08 17:35:25 +00:00
// CleanUp removes the file created for the challenge.
func (w *HTTPProvider) CleanUp(domain, token, keyAuth string) error {
err := os.Remove(filepath.Join(w.path, http01.ChallengePath(token)))
if err != nil {
return fmt.Errorf("could not remove file in webroot after HTTP challenge: %w", err)
}
return nil
}