aboutsummaryrefslogtreecommitdiff
path: root/provider.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-01-11 19:59:45 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-01-11 19:59:45 +0100
commitaf03cb9ce9a7f258b6d313531c417af2a7a93d28 (patch)
tree7b4d39d8d5903e50e7fe1f2715a6576140a19a15 /provider.go
parent83804cb3971d32b460783eb934592a6e6c821e6d (diff)
Chop in chunks
Diffstat (limited to 'provider.go')
-rw-r--r--provider.go94
1 files changed, 44 insertions, 50 deletions
diff --git a/provider.go b/provider.go
index 7b2b1bd..c6acc84 100644
--- a/provider.go
+++ b/provider.go
@@ -45,11 +45,7 @@ func NewProvider(directory string) (*Provider, error) {
Timeout: time.Duration(5 * time.Second),
},
}
- resp, err := p.Get(directory)
- if err != nil {
- return nil, err
- }
- _, err = p.parseJson(resp, &p.Directory)
+ err := p.getJson(directory, &p.Directory)
if err != nil {
return nil, err
}
@@ -57,7 +53,6 @@ func NewProvider(directory string) (*Provider, error) {
}
func (p *Provider) post(uri string, s Signer, v interface{}) (*http.Response, error) {
- log.Println("post", uri)
signed, err := s.Sign(v, p)
if err != nil {
return nil, err
@@ -65,6 +60,42 @@ func (p *Provider) post(uri string, s Signer, v interface{}) (*http.Response, er
return p.Post(uri, "application/jose+json", signed)
}
+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 p.parseJson(resp, v)
+}
+
+func (p *Provider) postCert(uri string, s Signer, v interface{}) (*x509.Certificate, nextStep, error) {
+ log.Println("post cert", uri)
+ resp, err := p.post(uri, s, v)
+ if err != nil {
+ return nil, nextStep{}, err
+ }
+ return p.parseCert(resp)
+}
+
+func (p *Provider) getJson(uri string, v interface{}) error {
+ resp, err := p.Get(uri)
+ if err != nil {
+ return err
+ }
+ _, err = p.parseJson(resp, v)
+ return err
+}
+
+func (p *Provider) getCert(uri string) (*x509.Certificate, error) {
+ resp, err := p.Get(uri)
+ if err != nil {
+ return nil, err
+ }
+ crt, _, err := p.parseCert(resp)
+ return crt, err
+}
+
type nextStep struct {
Link map[string]string
Location string
@@ -137,11 +168,7 @@ func (p *Provider) newReg(uri string, s Signer, c Contacts) (nextStep, error) {
Resource: ResNewReg,
Contact: c,
}
- resp, err := p.post(uri, s, r)
- if err != nil {
- return nextStep{}, err
- }
- return p.parseJson(resp, r)
+ return p.postJson(uri, s, r)
}
func (p *Provider) agree(uri string, s Signer, tos string) (nextStep, error) {
@@ -149,11 +176,7 @@ func (p *Provider) agree(uri string, s Signer, tos string) (nextStep, error) {
Resource: ResReg,
Agreement: tos,
}
- resp, err := p.post(uri, s, r)
- if err != nil {
- return nextStep{}, err
- }
- return p.parseJson(resp, r)
+ return p.postJson(uri, s, r)
}
func (p *Provider) Register(s Signer, c Contacts) error {
@@ -194,19 +217,11 @@ func (p *Provider) Authorize(s ThumbSigner, d *Desire) error {
Identifier: NewIdent(domain),
}
- resp, err := p.post(p.NewAuthz, s, r)
- if err != nil {
- return err
- }
- _, err = p.parseJson(resp, r)
+ _, err := p.postJson(p.NewAuthz, s, r)
if err != nil {
return err
}
- if resp.StatusCode != http.StatusCreated {
- return errStatus
- }
-
for _, ch := range r.pick(d) {
ka, _ := KeyAuthorization(s, ch.Token)
@@ -216,12 +231,7 @@ func (p *Provider) Authorize(s ThumbSigner, d *Desire) error {
KeyAuthorization: ka,
}
- resp, err = p.post(ch.URI, s, ans)
- if err != nil {
- return err
- }
-
- ns, err := p.parseJson(resp, ans)
+ ns, err := p.postJson(ch.URI, s, ans)
if err != nil {
return err
}
@@ -247,11 +257,7 @@ func (p *Provider) Authorize(s ThumbSigner, d *Desire) error {
func (p *Provider) queryStatus(url string) (bool, error) {
log.Println("query", url)
r := &Challenge{}
- resp, err := p.Get(url)
- if err != nil {
- return false, err
- }
- _, err = p.parseJson(resp, r)
+ err := p.getJson(url, r)
if err != nil {
return false, err
}
@@ -267,19 +273,7 @@ func (p *Provider) newCert(uri string, s Signer, d *Desire) (*x509.Certificate,
Resource: ResNewCert,
CSR: csr,
}
- resp, err := p.post(uri, s, r)
- if err != nil {
- return nil, nextStep{}, err
- }
- return p.parseCert(resp)
-}
-
-func (p *Provider) newCA(uri string) (*x509.Certificate, nextStep, error) {
- resp, err := p.Get(uri)
- if err != nil {
- return nil, nextStep{}, err
- }
- return p.parseCert(resp)
+ return p.postCert(uri, s, r)
}
func (p *Provider) Cert(s Signer, d *Desire) error {
@@ -290,7 +284,7 @@ func (p *Provider) Cert(s Signer, d *Desire) error {
d.cert = append(d.cert, crt)
// TODO Get cert on empty response
- crt, _, err = p.newCA(ns.Link["up"])
+ crt, err = p.getCert(ns.Link["up"])
if err != nil {
return err
}