aboutsummaryrefslogtreecommitdiff
path: root/provider.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-01-18 19:14:03 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-01-18 19:14:03 +0100
commit1fa9120f4550659418c54960d6e11f188003280a (patch)
treed3c8bd8f963254b2daccd033b76f42a0a360405f /provider.go
parent7f3a0cdcae9aebb119e00253eb581b51e0ef8cc5 (diff)
Reduce
Diffstat (limited to 'provider.go')
-rw-r--r--provider.go111
1 files changed, 58 insertions, 53 deletions
diff --git a/provider.go b/provider.go
index 551f8dc..5268842 100644
--- a/provider.go
+++ b/provider.go
@@ -50,7 +50,11 @@ func DialProvider(directory string) (*Provider, error) {
Timeout: time.Duration(5 * time.Second),
},
}
- return p, p.getJson(directory, &p.Directory)
+ resp, err := p.get(directory)
+ if err != nil {
+ return nil, err
+ }
+ return p, parseJson(resp, &p.Directory)
}
func (p *Provider) post(uri string, s Signer, v interface{}) (*http.Response, error) {
@@ -59,47 +63,28 @@ func (p *Provider) post(uri string, s Signer, v interface{}) (*http.Response, er
return nil, err
}
resp, err := p.Post(uri, "application/jose+json", signed)
- p.nonce(resp)
- return resp, err
-}
-
-func (p *Provider) postJson(uri string, s Signer, v interface{}) (nextStep, error) {
- log.Println("post json", uri)
- resp, err := p.post(uri, s, v)
if err != nil {
- return nextStep{}, err
- }
- return parseHeader(resp), parseJson(resp, v)
-}
-
-func (p *Provider) postCert(uri string, s Signer, v interface{}) (nextStep, *x509.Certificate, error) {
- log.Println("post cert", uri)
- resp, err := p.post(uri, s, v)
- if err != nil {
- return nextStep{}, nil, err
- }
- log.Println("post len", resp.ContentLength)
- cert, err := parseCert(resp)
- return parseHeader(resp), cert, err
-}
-
-func (p *Provider) getJson(uri string, v interface{}) error {
- resp, err := p.Get(uri)
- if err != nil {
- return err
+ return nil, err
}
p.nonce(resp)
- return parseJson(resp, v)
+ if hasProblem(resp) {
+ defer resp.Body.Close()
+ return nil, problem(resp.Body)
+ }
+ return resp, nil
}
-func (p *Provider) getCert(uri string) (*x509.Certificate, error) {
+func (p *Provider) get(uri string) (*http.Response, error) {
resp, err := p.Get(uri)
if err != nil {
return nil, err
}
p.nonce(resp)
- log.Println("get len", resp.ContentLength)
- return parseCert(resp)
+ if hasProblem(resp) {
+ defer resp.Body.Close()
+ return nil, problem(resp.Body)
+ }
+ return resp, nil
}
type nextStep struct {
@@ -126,26 +111,22 @@ func parseHeader(resp *http.Response) nextStep {
func parseJson(resp *http.Response, v interface{}) error {
defer resp.Body.Close()
- switch resp.Header.Get("Content-Type") {
- case "application/problem+json":
- return problem(resp.Body)
- case "application/json":
- return json.NewDecoder(resp.Body).Decode(v)
- default:
+ if resp.Header.Get("Content-Type") != "application/json" {
return errContentType
}
+ return json.NewDecoder(resp.Body).Decode(v)
}
func parseCert(resp *http.Response) (*x509.Certificate, error) {
defer resp.Body.Close()
- switch resp.Header.Get("Content-Type") {
- case "application/problem+json":
- return nil, problem(resp.Body)
- case "application/pkix-cert":
- return readCert(resp.Body)
- default:
+ if resp.Header.Get("Content-Type") != "application/pkix-cert" {
return nil, errContentType
}
+ return readCert(resp.Body)
+}
+
+func hasProblem(resp *http.Response) bool {
+ return resp.Header.Get("Content-Type") == "application/problem+json"
}
func problem(r io.Reader) error {
@@ -162,7 +143,11 @@ func (p *Provider) newReg(uri string, s Signer, c Contacts) (nextStep, error) {
Resource: ResNewReg,
Contact: c,
}
- return p.postJson(uri, s, r)
+ resp, err := p.post(uri, s, r)
+ if err != nil {
+ return nextStep{}, err
+ }
+ return parseHeader(resp), parseJson(resp, r)
}
func (p *Provider) agree(uri string, s Signer, tos string) (nextStep, error) {
@@ -170,7 +155,11 @@ func (p *Provider) agree(uri string, s Signer, tos string) (nextStep, error) {
Resource: ResReg,
Agreement: tos,
}
- return p.postJson(uri, s, r)
+ resp, err := p.post(uri, s, r)
+ if err != nil {
+ return nextStep{}, err
+ }
+ return parseHeader(resp), parseJson(resp, r)
}
func (p *Provider) Register(s Signer, c Contacts) error {
@@ -192,10 +181,11 @@ func (p *Provider) solve(s Signer, ch Challenge) error {
Type: ch.Type,
KeyAuthorization: ka,
}
- ns, err := p.postJson(ch.URI, s, r)
+ resp, err := p.post(ch.URI, s, r)
if err != nil {
return err
}
+ ns := parseHeader(resp)
err = ch.Solve(ch.Token, ka)
if err != nil {
return err
@@ -221,10 +211,11 @@ func (p *Provider) Authorize(s Signer, d *Desire) error {
},
}
- _, err := p.postJson(p.NewAuthz, s, r)
+ resp, err := p.post(p.NewAuthz, s, r)
if err != nil {
return err
}
+ err = parseJson(resp, r)
for _, ch := range d.pick(r) {
if err = p.solve(s, ch); err != nil {
@@ -235,10 +226,14 @@ func (p *Provider) Authorize(s Signer, d *Desire) error {
return nil
}
-func (p *Provider) queryStatus(url string) (bool, error) {
- log.Println("query", url)
+func (p *Provider) queryStatus(uri string) (bool, error) {
+ log.Println("query", uri)
r := &Challenge{}
- err := p.getJson(url, r)
+ resp, err := p.get(uri)
+ if err != nil {
+ return false, err
+ }
+ err = parseJson(resp, r)
if err != nil {
return false, err
}
@@ -258,7 +253,13 @@ func (p *Provider) newCert(uri string, s Signer, d *Desire) (nextStep, *x509.Cer
Resource: ResNewCert,
CSR: csr,
}
- return p.postCert(uri, s, r)
+ resp, err := p.post(uri, s, r)
+ if err != nil {
+ return nextStep{}, nil, err
+ }
+ ns := parseHeader(resp)
+ cert, err := parseCert(resp)
+ return ns, cert, err
}
func (p *Provider) Cert(s Signer, d *Desire) error {
@@ -269,7 +270,11 @@ func (p *Provider) Cert(s Signer, d *Desire) error {
d.cert = append(d.cert, crt)
// TODO Get cert on empty response
- crt, err = p.getCert(ns.Link["up"])
+ resp, err := p.get(ns.Link["up"])
+ if err != nil {
+ return err
+ }
+ crt, err = parseCert(resp)
if err != nil {
return err
}