2016-03-14 10:49:02 +00:00
|
|
|
// Package webroot implements a HTTP provider for solving the HTTP-01 challenge using web server's root path.
|
|
|
|
package webroot
|
2016-02-10 11:19:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2016-03-14 10:49:02 +00:00
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
2016-02-10 11:19:29 +00:00
|
|
|
)
|
|
|
|
|
2016-03-16 10:32:09 +00:00
|
|
|
// HTTPProvider implements ChallengeProvider for `http-01` challenge
|
|
|
|
type HTTPProvider struct {
|
2016-02-10 11:19:29 +00:00
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2016-03-16 10:32:09 +00:00
|
|
|
// NewHTTPProvider returns a HTTPProvider instance with a configured webroot path
|
|
|
|
func NewHTTPProvider(path string) (*HTTPProvider, error) {
|
2016-02-10 15:55:10 +00:00
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("Webroot path does not exist")
|
|
|
|
}
|
|
|
|
|
2016-03-16 10:32:09 +00:00
|
|
|
c := &HTTPProvider{
|
2016-02-10 15:55:10 +00:00
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present makes the token available at `HTTP01ChallengePath(token)` by creating a file in the given webroot path
|
2016-03-16 10:32:09 +00:00
|
|
|
func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
|
2016-02-10 11:19:29 +00:00
|
|
|
var err error
|
2016-02-10 15:55:10 +00:00
|
|
|
|
2016-03-14 10:49:02 +00:00
|
|
|
challengeFilePath := path.Join(w.path, acme.HTTP01ChallengePath(token))
|
2016-09-04 08:06:18 +00:00
|
|
|
err = os.MkdirAll(path.Dir(challengeFilePath), 0755)
|
2016-02-10 15:55:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not create required directories in webroot for HTTP challenge -> %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-04 08:06:18 +00:00
|
|
|
err = ioutil.WriteFile(challengeFilePath, []byte(keyAuth), 0644)
|
2016-02-10 11:19:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not write file in webroot for HTTP challenge -> %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 15:55:10 +00:00
|
|
|
// CleanUp removes the file created for the challenge
|
2016-03-16 10:32:09 +00:00
|
|
|
func (w *HTTPProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-02-10 11:19:29 +00:00
|
|
|
var err error
|
2016-03-14 10:49:02 +00:00
|
|
|
err = os.Remove(path.Join(w.path, acme.HTTP01ChallengePath(token)))
|
2016-02-10 11:19:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not remove file in webroot after HTTP challenge -> %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|