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