From 466af286723b9a166bffebed0964d20d6f74d502 Mon Sep 17 00:00:00 2001 From: xenolf Date: Sun, 27 Dec 2015 19:08:17 +0100 Subject: [PATCH] Extract validateFunc from httpChallenge and tlsSNIChallenge --- acme/client.go | 12 +++++++----- acme/http_challenge.go | 4 ++-- acme/tls_sni_challenge.go | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/acme/client.go b/acme/client.go index b00a8656..5becbeea 100644 --- a/acme/client.go +++ b/acme/client.go @@ -44,6 +44,8 @@ type solver interface { Solve(challenge challenge, domain string) error } +type validateFunc func(j *jws, domain, uri string, chlng challenge) error + // Client is the user-friendy way to ACME type Client struct { directory directory @@ -97,8 +99,8 @@ func NewClient(caDirURL string, user User, keyBits int) (*Client, error) { // Add all available solvers with the right index as per ACME // spec to this map. Otherwise they won`t be found. solvers := make(map[string]solver) - solvers["http-01"] = &httpChallenge{jws: jws} - solvers["tls-sni-01"] = &tlsSNIChallenge{jws: jws} + solvers["http-01"] = &httpChallenge{jws: jws, validate: validate} + solvers["tls-sni-01"] = &tlsSNIChallenge{jws: jws, validate: validate} return &Client{directory: dir, user: user, jws: jws, keyBits: keyBits, solvers: solvers}, nil } @@ -548,7 +550,7 @@ func parseLinks(links []string) map[string]string { // validate makes the ACME server start validating a // challenge response, only returning once it is done. -func validate(j *jws, uri string, chlng challenge) error { +func validate(j *jws, domain, uri string, chlng challenge) error { var challengeResponse challenge hdr, err := postJSON(j, uri, chlng, &challengeResponse) @@ -561,12 +563,12 @@ func validate(j *jws, uri string, chlng challenge) error { for { switch challengeResponse.Status { case "valid": - logf("The server validated our request") + logf("[INFO][%s] The server validated our request", domain) return nil case "pending": break case "invalid": - return errors.New("The server could not validate our request.") + return handleChallengeError(challengeResponse) default: return errors.New("The server returned an unexpected state.") } diff --git a/acme/http_challenge.go b/acme/http_challenge.go index e8fdb4d5..75195877 100644 --- a/acme/http_challenge.go +++ b/acme/http_challenge.go @@ -9,7 +9,7 @@ import ( type httpChallenge struct { jws *jws - validate func(j *jws, uri string, chlng challenge) error + validate validateFunc optPort string } @@ -57,5 +57,5 @@ func (s *httpChallenge) Solve(chlng challenge, domain string) error { go http.Serve(listener, mux) - return s.validate(s.jws, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth}) + return s.validate(s.jws, domain, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth}) } diff --git a/acme/tls_sni_challenge.go b/acme/tls_sni_challenge.go index 966647ad..ad099d54 100644 --- a/acme/tls_sni_challenge.go +++ b/acme/tls_sni_challenge.go @@ -11,7 +11,7 @@ import ( type tlsSNIChallenge struct { jws *jws - validate func(j *jws, uri string, chlng challenge) error + validate validateFunc optPort string } @@ -49,7 +49,7 @@ func (t *tlsSNIChallenge) Solve(chlng challenge, domain string) error { go http.Serve(listener, nil) - return t.validate(t.jws, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth}) + return t.validate(t.jws, domain, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth}) } func (t *tlsSNIChallenge) generateCertificate(keyAuth string) (tls.Certificate, error) {