2019-03-11 16:56:48 +00:00
|
|
|
package http01
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/acme"
|
|
|
|
"github.com/go-acme/lego/v3/acme/api"
|
|
|
|
"github.com/go-acme/lego/v3/challenge"
|
|
|
|
"github.com/go-acme/lego/v3/log"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ValidateFunc func(core *api.Core, domain string, chlng acme.Challenge) error
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// ChallengePath returns the URL path for the `http-01` challenge.
|
2018-12-06 21:50:17 +00:00
|
|
|
func ChallengePath(token string) string {
|
|
|
|
return "/.well-known/acme-challenge/" + token
|
|
|
|
}
|
|
|
|
|
|
|
|
type Challenge struct {
|
|
|
|
core *api.Core
|
|
|
|
validate ValidateFunc
|
|
|
|
provider challenge.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChallenge(core *api.Core, validate ValidateFunc, provider challenge.Provider) *Challenge {
|
|
|
|
return &Challenge{
|
|
|
|
core: core,
|
|
|
|
validate: validate,
|
|
|
|
provider: provider,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Challenge) SetProvider(provider challenge.Provider) {
|
|
|
|
c.provider = provider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Challenge) Solve(authz acme.Authorization) error {
|
|
|
|
domain := challenge.GetTargetedDomain(authz)
|
|
|
|
log.Infof("[%s] acme: Trying to solve HTTP-01", domain)
|
|
|
|
|
|
|
|
chlng, err := challenge.FindChallenge(challenge.HTTP01, authz)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the Key Authorization for the challenge
|
|
|
|
keyAuth, err := c.core.GetKeyAuthorization(chlng.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.provider.Present(authz.Identifier.Value, chlng.Token, keyAuth)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("[%s] acme: error presenting token: %w", domain, err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := c.provider.CleanUp(authz.Identifier.Value, chlng.Token, keyAuth)
|
|
|
|
if err != nil {
|
2020-03-09 17:10:40 +00:00
|
|
|
log.Warnf("[%s] acme: cleaning up failed: %v", domain, err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
chlng.KeyAuthorization = keyAuth
|
2019-01-26 00:11:45 +00:00
|
|
|
return c.validate(c.core, domain, chlng)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|