|
1 | 1 | package registration
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "encoding/json" |
4 | 6 | "errors"
|
| 7 | + "fmt" |
| 8 | + "io" |
5 | 9 | "net/http"
|
| 10 | + "net/url" |
6 | 11 |
|
7 | 12 | "github.com/go-acme/lego/v4/acme"
|
8 | 13 | "github.com/go-acme/lego/v4/acme/api"
|
@@ -69,6 +74,54 @@ func (r *Registrar) Register(options RegisterOptions) (*Resource, error) {
|
69 | 74 | return &Resource{URI: account.Location, Body: account.Account}, nil
|
70 | 75 | }
|
71 | 76 |
|
| 77 | +func createZeroSSLAccount(email string) (string, string, error) { |
| 78 | + newAccountURL := "https://api.zerossl.com/acme/eab-credentials-email" |
| 79 | + data := struct { |
| 80 | + Success bool `json:"success"` |
| 81 | + KID string `json:"eab_kid"` |
| 82 | + HMAC string `json:"eab_hmac_key"` |
| 83 | + }{} |
| 84 | + |
| 85 | + resp, err := http.PostForm(newAccountURL, url.Values{"email": {email}}) |
| 86 | + if err != nil { |
| 87 | + return "", "", fmt.Errorf("sending request: %w", err) |
| 88 | + } |
| 89 | + defer resp.Body.Close() |
| 90 | + |
| 91 | + // ZeroSSL might return errors as plain-text messages instead of JSON, |
| 92 | + // so we buffer the response to be able to return it as error. |
| 93 | + var rawResp bytes.Buffer |
| 94 | + r := io.TeeReader(io.LimitReader(resp.Body, 10*1024), &rawResp) // Limit response to 10KB |
| 95 | + if err := json.NewDecoder(r).Decode(&data); err != nil { |
| 96 | + // It is likely not a JSON but a plain-text error message |
| 97 | + _, _ = io.ReadAll(r) // read the rest of the body |
| 98 | + return "", "", fmt.Errorf("parsing response: %w. Original response:\n%s", err, rawResp.String()) |
| 99 | + } |
| 100 | + |
| 101 | + if !data.Success { |
| 102 | + return "", "", fmt.Errorf("received success=false") |
| 103 | + } |
| 104 | + return data.KID, data.HMAC, nil |
| 105 | +} |
| 106 | + |
| 107 | +// RegisterWithZeroSSL Register the current account to the ZeroSSL server. |
| 108 | +func (r *Registrar) RegisterWithZeroSSL(options RegisterOptions) (*Resource, error) { |
| 109 | + if r.user.GetEmail() == "" { |
| 110 | + return nil, errors.New("acme: cannot register ZeroSSL account without email address") |
| 111 | + } |
| 112 | + |
| 113 | + kid, hmac, err := createZeroSSLAccount(r.user.GetEmail()) |
| 114 | + if err != nil { |
| 115 | + return nil, fmt.Errorf("acme: error registering new ZeroSSL account: %w", err) |
| 116 | + } |
| 117 | + |
| 118 | + return r.RegisterWithExternalAccountBinding(RegisterEABOptions{ |
| 119 | + TermsOfServiceAgreed: options.TermsOfServiceAgreed, |
| 120 | + Kid: kid, |
| 121 | + HmacEncoded: hmac, |
| 122 | + }) |
| 123 | +} |
| 124 | + |
72 | 125 | // RegisterWithExternalAccountBinding Register the current account to the ACME server.
|
73 | 126 | func (r *Registrar) RegisterWithExternalAccountBinding(options RegisterEABOptions) (*Resource, error) {
|
74 | 127 | accMsg := acme.Account{
|
|
0 commit comments