Use postJSON and getJSON wherever possible.
Encapsulates JSON marshalling.
This commit is contained in:
parent
2dc2fdd1af
commit
039b7c50dc
1 changed files with 25 additions and 87 deletions
112
acme/client.go
112
acme/client.go
|
@ -68,16 +68,9 @@ func NewClient(caDirURL string, user User, keyBits int, optPort string) (*Client
|
||||||
return nil, fmt.Errorf("invalid private key: %v", err)
|
return nil, fmt.Errorf("invalid private key: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dirResp, err := http.Get(caDirURL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("get directory at '%s': %v", caDirURL, err)
|
|
||||||
}
|
|
||||||
defer dirResp.Body.Close()
|
|
||||||
|
|
||||||
var dir directory
|
var dir directory
|
||||||
err = json.NewDecoder(dirResp.Body).Decode(&dir)
|
if err := getJSON(caDirURL, &dir); err != nil {
|
||||||
if err != nil {
|
return nil, fmt.Errorf("get directory at '%s': %v", caDirURL, err)
|
||||||
return nil, fmt.Errorf("decode directory: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if dir.NewRegURL == "" {
|
if dir.NewRegURL == "" {
|
||||||
|
@ -121,32 +114,16 @@ func (c *Client) Register() (*RegistrationResource, error) {
|
||||||
regMsg.Contact = []string{}
|
regMsg.Contact = []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(regMsg)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := c.jws.post(c.directory.NewRegURL, jsonBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode >= http.StatusBadRequest {
|
|
||||||
return nil, handleHTTPError(resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
var serverReg Registration
|
var serverReg Registration
|
||||||
decoder := json.NewDecoder(resp.Body)
|
hdr, err := postJSON(c.jws, c.directory.NewRegURL, regMsg, &serverReg)
|
||||||
err = decoder.Decode(&serverReg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
reg := &RegistrationResource{Body: serverReg}
|
reg := &RegistrationResource{Body: serverReg}
|
||||||
|
|
||||||
links := parseLinks(resp.Header["Link"])
|
links := parseLinks(hdr["Link"])
|
||||||
reg.URI = resp.Header.Get("Location")
|
reg.URI = hdr.Get("Location")
|
||||||
if links["terms-of-service"] != "" {
|
if links["terms-of-service"] != "" {
|
||||||
reg.TosURL = links["terms-of-service"]
|
reg.TosURL = links["terms-of-service"]
|
||||||
}
|
}
|
||||||
|
@ -165,22 +142,8 @@ func (c *Client) Register() (*RegistrationResource, error) {
|
||||||
func (c *Client) AgreeToTOS() error {
|
func (c *Client) AgreeToTOS() error {
|
||||||
c.user.GetRegistration().Body.Agreement = c.user.GetRegistration().TosURL
|
c.user.GetRegistration().Body.Agreement = c.user.GetRegistration().TosURL
|
||||||
c.user.GetRegistration().Body.Resource = "reg"
|
c.user.GetRegistration().Body.Resource = "reg"
|
||||||
jsonBytes, err := json.Marshal(&c.user.GetRegistration().Body)
|
_, err := postJSON(c.jws, c.user.GetRegistration().URI, c.user.GetRegistration().Body, nil)
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := c.jws.post(c.user.GetRegistration().URI, jsonBytes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusAccepted {
|
|
||||||
return handleHTTPError(resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ObtainCertificates tries to obtain certificates from the CA server
|
// ObtainCertificates tries to obtain certificates from the CA server
|
||||||
|
@ -277,22 +240,8 @@ func (c *Client) RevokeCertificate(certificate []byte) error {
|
||||||
|
|
||||||
encodedCert := base64.URLEncoding.EncodeToString(x509Cert.Raw)
|
encodedCert := base64.URLEncoding.EncodeToString(x509Cert.Raw)
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(revokeCertMessage{Resource: "revoke-cert", Certificate: encodedCert})
|
_, err = postJSON(c.jws, c.directory.RevokeCertURL, revokeCertMessage{Resource: "revoke-cert", Certificate: encodedCert}, nil)
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := c.jws.post(c.directory.RevokeCertURL, jsonBytes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return handleHTTPError(resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenewCertificate takes a CertificateResource and tries to renew the certificate.
|
// RenewCertificate takes a CertificateResource and tries to renew the certificate.
|
||||||
|
@ -428,37 +377,21 @@ func (c *Client) getChallenges(domains []string) ([]authorizationResource, map[s
|
||||||
|
|
||||||
for _, domain := range domains {
|
for _, domain := range domains {
|
||||||
go func(domain string) {
|
go func(domain string) {
|
||||||
jsonBytes, err := json.Marshal(authorization{Resource: "new-authz", Identifier: identifier{Type: "dns", Value: domain}})
|
authMsg := authorization{Resource: "new-authz", Identifier: identifier{Type: "dns", Value: domain}}
|
||||||
|
var authz authorization
|
||||||
|
hdr, err := postJSON(c.jws, c.user.GetRegistration().NewAuthzURL, authMsg, &authz)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errc <- domainError{Domain: domain, Error: err}
|
errc <- domainError{Domain: domain, Error: err}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.jws.post(c.user.GetRegistration().NewAuthzURL, jsonBytes)
|
links := parseLinks(hdr["Link"])
|
||||||
if err != nil {
|
|
||||||
errc <- domainError{Domain: domain, Error: err}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusCreated {
|
|
||||||
errc <- domainError{Domain: domain, Error: handleHTTPError(resp)}
|
|
||||||
}
|
|
||||||
|
|
||||||
links := parseLinks(resp.Header["Link"])
|
|
||||||
if links["next"] == "" {
|
if links["next"] == "" {
|
||||||
logf("[ERROR] acme: Server did not provide next link to proceed")
|
logf("[ERROR] acme: Server did not provide next link to proceed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var authz authorization
|
resc <- authorizationResource{Body: authz, NewCertURL: links["next"], AuthURL: hdr.Get("Location"), Domain: domain}
|
||||||
decoder := json.NewDecoder(resp.Body)
|
|
||||||
err = decoder.Decode(&authz)
|
|
||||||
if err != nil {
|
|
||||||
errc <- domainError{Domain: domain, Error: err}
|
|
||||||
}
|
|
||||||
resp.Body.Close()
|
|
||||||
|
|
||||||
resc <- authorizationResource{Body: authz, NewCertURL: links["next"], AuthURL: resp.Header.Get("Location"), Domain: domain}
|
|
||||||
}(domain)
|
}(domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -681,7 +614,8 @@ var (
|
||||||
func validate(j *jws, uri string, chlng challenge) error {
|
func validate(j *jws, uri string, chlng challenge) error {
|
||||||
var challengeResponse challenge
|
var challengeResponse challenge
|
||||||
|
|
||||||
if err := postJSON(j, uri, chlng, &challengeResponse); err != nil {
|
_, err := postJSON(j, uri, chlng, &challengeResponse)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -735,21 +669,25 @@ func getJSON(uri string, respBody interface{}) error {
|
||||||
|
|
||||||
// postJSON performs an HTTP POST request and parses the response body
|
// postJSON performs an HTTP POST request and parses the response body
|
||||||
// as JSON, into the provided respBody object.
|
// as JSON, into the provided respBody object.
|
||||||
func postJSON(j *jws, uri string, reqBody, respBody interface{}) error {
|
func postJSON(j *jws, uri string, reqBody, respBody interface{}) (http.Header, error) {
|
||||||
jsonBytes, err := json.Marshal(reqBody)
|
jsonBytes, err := json.Marshal(reqBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Failed to marshal network message...")
|
return nil, errors.New("Failed to marshal network message...")
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := j.post(uri, jsonBytes)
|
resp, err := j.post(uri, jsonBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to post JWS message. -> %v", err)
|
return nil, fmt.Errorf("Failed to post JWS message. -> %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode >= http.StatusBadRequest {
|
if resp.StatusCode >= http.StatusBadRequest {
|
||||||
return handleHTTPError(resp)
|
return resp.Header, handleHTTPError(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.NewDecoder(resp.Body).Decode(respBody)
|
if respBody == nil {
|
||||||
|
return resp.Header, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.Header, json.NewDecoder(resp.Body).Decode(respBody)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue