|
1 | 1 | package registration
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "errors"
|
| 6 | + "fmt" |
5 | 7 | "net/http"
|
| 8 | + "net/url" |
6 | 9 |
|
7 | 10 | "github.com/go-acme/lego/v4/acme"
|
8 | 11 | "github.com/go-acme/lego/v4/acme/api"
|
@@ -67,6 +70,47 @@ func (r *Registrar) Register(options RegisterOptions) (*Resource, error) {
|
67 | 70 | return &Resource{URI: account.Location, Body: account.Account}, nil
|
68 | 71 | }
|
69 | 72 |
|
| 73 | +func createZeroSSLAccount(email string) (string, string, error) { |
| 74 | + newAccountURL := "http://api.zerossl.com/acme/eab-credentials-email" |
| 75 | + data := struct { |
| 76 | + Success bool `json:"success"` |
| 77 | + KID string `json:"eab_kid"` |
| 78 | + HMAC string `json:"eab_hmac_key"` |
| 79 | + }{} |
| 80 | + |
| 81 | + resp, err := http.PostForm(newAccountURL, url.Values{"email": {email}}) |
| 82 | + if err != nil { |
| 83 | + return "", "", fmt.Errorf("acme: error creating ZeroSSL account EAB details request: %w", err) |
| 84 | + } |
| 85 | + defer resp.Body.Close() |
| 86 | + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { |
| 87 | + return "", "", fmt.Errorf("acme: error reading ZeroSSL account EAB details response: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + if !data.Success { |
| 91 | + return "", "", fmt.Errorf("acme: error in ZeroSSL account EAB details response, success=false") |
| 92 | + } |
| 93 | + return data.KID, data.HMAC, nil |
| 94 | +} |
| 95 | + |
| 96 | +// RegisterWithZeroSSL Register the current account to the ZeroSSL server. |
| 97 | +func (r *Registrar) RegisterWithZeroSSL(options RegisterOptions) (*Resource, error) { |
| 98 | + if r.user.GetEmail() == "" { |
| 99 | + return nil, errors.New("acme: cannot register ZeroSSL account without email address") |
| 100 | + } |
| 101 | + |
| 102 | + kid, hmac, err := createZeroSSLAccount(r.user.GetEmail()) |
| 103 | + if err != nil { |
| 104 | + return nil, fmt.Errorf("acme: error registering new ZeroSSL account: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + return r.RegisterWithExternalAccountBinding(RegisterEABOptions{ |
| 108 | + TermsOfServiceAgreed: options.TermsOfServiceAgreed, |
| 109 | + Kid: kid, |
| 110 | + HmacEncoded: hmac, |
| 111 | + }) |
| 112 | +} |
| 113 | + |
70 | 114 | // RegisterWithExternalAccountBinding Register the current account to the ACME server.
|
71 | 115 | func (r *Registrar) RegisterWithExternalAccountBinding(options RegisterEABOptions) (*Resource, error) {
|
72 | 116 | accMsg := acme.Account{
|
|
0 commit comments