diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2016-01-06 19:13:08 +0100 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2016-01-06 19:13:08 +0100 |
commit | b2bf8ff1a1983a81bcaf8a90a827de38b8bc1ad8 (patch) | |
tree | dd5e98795b291b2de20bfa3df1d02ad9fd6fda94 /provider.go | |
parent | beb07b9563b9370fb0123b3ed07ff319e2ebe835 (diff) |
Simplify for now
Diffstat (limited to 'provider.go')
-rw-r--r-- | provider.go | 64 |
1 files changed, 26 insertions, 38 deletions
diff --git a/provider.go b/provider.go index 1a79b23..1db7266 100644 --- a/provider.go +++ b/provider.go @@ -5,6 +5,7 @@ import ( "crypto/x509" "encoding/json" "errors" + "io" "io/ioutil" "log" "net/http" @@ -71,6 +72,7 @@ func NewProvider(directory string) (*Provider, error) { // new-reg, new-authz, challenge, new-cert 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 @@ -102,37 +104,38 @@ func (p *Provider) parse(resp *http.Response, v interface{}) (ns nextStep, err e p.nonces <- rn } - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return - } defer resp.Body.Close() - - log.Println(string(body)) - switch resp.Header.Get("Content-Type") { case "application/problem+json": - err = problem(resp) + err = problem(resp.Body) case "application/json": - err = json.Unmarshal(body, v) + err = json.NewDecoder(resp.Body).Decode(v) case "application/pkix-cert": // FIXME - v, err = x509.ParseCertificate(body) + v, err = cert(resp.Body) default: err = errContentType } return } -func problem(resp *http.Response) error { +func problem(r io.Reader) error { var p Problem - if err := json.NewDecoder(resp.Body).Decode(&p); err != nil { + if err := json.NewDecoder(r).Decode(&p); err != nil { return err } p.Err = urnErrors[p.Type] return p } +func cert(r io.Reader) (*x509.Certificate, error) { + der, err := ioutil.ReadAll(r) + if err != nil { + return nil, err + } + return x509.ParseCertificate(der) +} + /* directory . @@ -249,45 +252,30 @@ func (p *Provider) Authorize(s ThumbSigner, domain string) error { return err } - done := make(chan bool) - errc := make(chan error) - - ticker := time.NewTicker(time.Second) - defer ticker.Stop() - go func() { - for range ticker.C { - err := p.queryStatus(ns.Location, done) - if err != nil { - errc <- err - return - } + for { + done, err := p.queryStatus(ns.Location) + if err != nil { + return err + } + if done { + break } - }() - select { - case <-done: - case err = <-errc: - case <-time.After(30 * time.Second): - return errTimedOut } - } return err } -func (p *Provider) queryStatus(url string, done chan bool) error { +func (p *Provider) queryStatus(url string) (bool, error) { r := &Challenge{} resp, err := p.Get(url) if err != nil { - return err + return false, err } _, err = p.parse(resp, r) if err != nil { - return err - } - if r.Status == StatusValid { - done <- true + return false, err } - return nil + return r.Status == StatusValid, nil } func (p *Provider) Cert(s Signer, altnames []string, key *rsa.PrivateKey) (*x509.Certificate, error) { |