forked from TrueCloudLab/lego
parent
cf288a3503
commit
3842dc6432
3 changed files with 41 additions and 12 deletions
|
@ -121,9 +121,8 @@ func (c *Client) Register() (*RegistrationResource, error) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusConflict {
|
if resp.StatusCode >= http.StatusBadRequest {
|
||||||
// REVIEW: should this return an error?
|
return nil, handleHTTPError(resp)
|
||||||
return nil, errors.New("This account is already registered with this CA.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverReg Registration
|
var serverReg Registration
|
||||||
|
@ -167,7 +166,7 @@ func (c *Client) AgreeToTOS() error {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusAccepted {
|
if resp.StatusCode != http.StatusAccepted {
|
||||||
return fmt.Errorf("The server returned %d but we expected %d", resp.StatusCode, http.StatusAccepted)
|
return handleHTTPError(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -216,9 +215,8 @@ func (c *Client) RevokeCertificate(certificate []byte) error {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != http.StatusOK {
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
return handleHTTPError(resp)
|
||||||
return fmt.Errorf("The server returned an error while trying to revoke the certificate.\n%s", body)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -369,8 +367,7 @@ func (c *Client) getChallenges(domains []string) []*authorizationResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusCreated {
|
if resp.StatusCode != http.StatusCreated {
|
||||||
errc <- fmt.Errorf("Getting challenges for %s failed. Got status %d but expected %d",
|
errc <- handleHTTPError(resp)
|
||||||
domain, resp.StatusCode, http.StatusCreated)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
links := parseLinks(resp.Header["Link"])
|
links := parseLinks(resp.Header["Link"])
|
||||||
|
@ -523,7 +520,7 @@ func (c *Client) requestCertificate(authz *authorizationResource, result chan Ce
|
||||||
|
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
logger().Printf("[%s] The server returned an unexpected status code %d.", authz.Domain, resp.StatusCode)
|
errc <- handleHTTPError(resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
acme/error.go
Normal file
30
acme/error.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package acme
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Error asdf
|
||||||
|
type Error struct {
|
||||||
|
StatusCode int `json:"status,omitempty"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Detail string `json:"detail"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Error) Error() string {
|
||||||
|
return fmt.Sprintf("[%d] Type: %s Detail: %s", e.StatusCode, e.Type, e.Detail)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleHTTPError(resp *http.Response) error {
|
||||||
|
var errorDetail Error
|
||||||
|
decoder := json.NewDecoder(resp.Body)
|
||||||
|
err := decoder.Decode(&errorDetail)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
errorDetail.StatusCode = resp.StatusCode
|
||||||
|
return errorDetail
|
||||||
|
}
|
|
@ -48,6 +48,10 @@ func (s *simpleHTTPChallenge) Solve(chlng challenge, domain string) error {
|
||||||
var challengeResponse challenge
|
var challengeResponse challenge
|
||||||
Loop:
|
Loop:
|
||||||
for {
|
for {
|
||||||
|
if resp.StatusCode >= http.StatusBadRequest {
|
||||||
|
return handleHTTPError(resp)
|
||||||
|
}
|
||||||
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&challengeResponse)
|
err = json.NewDecoder(resp.Body).Decode(&challengeResponse)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -61,10 +65,8 @@ Loop:
|
||||||
case "pending":
|
case "pending":
|
||||||
break
|
break
|
||||||
case "invalid":
|
case "invalid":
|
||||||
logger().Print("The server could not validate our request.")
|
|
||||||
return errors.New("The server could not validate our request.")
|
return errors.New("The server could not validate our request.")
|
||||||
default:
|
default:
|
||||||
logger().Print("The server returned an unexpected state.")
|
|
||||||
return errors.New("The server returned an unexpected state.")
|
return errors.New("The server returned an unexpected state.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue