forked from go-acme/lego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webroot.go
53 lines (42 loc) · 1.61 KB
/
webroot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Package webroot implements a HTTP provider for solving the HTTP-01 challenge using web server's root path.
package webroot // import "github.com/xenolf/lego/providers/http/webroot"
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/xenolf/lego/challenge/http01"
)
// HTTPProvider implements ChallengeProvider for `http-01` challenge
type HTTPProvider struct {
path string
}
// NewHTTPProvider returns a HTTPProvider instance with a configured webroot path
func NewHTTPProvider(path string) (*HTTPProvider, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, fmt.Errorf("webroot path does not exist")
}
return &HTTPProvider{path: path}, nil
}
// 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))
err = os.MkdirAll(filepath.Dir(challengeFilePath), 0755)
if err != nil {
return fmt.Errorf("could not create required directories in webroot for HTTP challenge -> %v", err)
}
err = ioutil.WriteFile(challengeFilePath, []byte(keyAuth), 0644)
if err != nil {
return fmt.Errorf("could not write file in webroot for HTTP challenge -> %v", err)
}
return nil
}
// 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 -> %v", err)
}
return nil
}