1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 01:07:23 +02:00

Add support to update account (#1002)

This commit is contained in:
Rémi Lapeyre 2019-11-19 01:07:46 +01:00 committed by Ludovic Fernandez
parent e345074ff6
commit b1dba4f23d
3 changed files with 69 additions and 0 deletions

View File

@ -57,6 +57,20 @@ func (a *AccountService) Get(accountURL string) (acme.Account, error) {
return account, nil
}
// Update Updates an account.
func (a *AccountService) Update(accountURL string, req acme.Account) (acme.ExtendedAccount, error) {
if len(accountURL) == 0 {
return acme.ExtendedAccount{}, errors.New("account[update]: empty URL")
}
var account acme.ExtendedAccount
_, err := a.core.post(accountURL, req, &account)
if err != nil {
return acme.ExtendedAccount{}, err
}
return account, nil
}
// Deactivate Deactivates an account.
func (a *AccountService) Deactivate(accountURL string) error {
if len(accountURL) == 0 {

View File

@ -319,6 +319,37 @@ func TestChallengeTLS_Client_ObtainForCSR(t *testing.T) {
assert.NotEmpty(t, resource.CSR)
}
func TestRegistrar_UpdateAccount(t *testing.T) {
err := os.Setenv("LEGO_CA_CERTIFICATES", "./fixtures/certs/pebble.minica.pem")
require.NoError(t, err)
defer func() { _ = os.Unsetenv("LEGO_CA_CERTIFICATES") }()
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err, "Could not generate test key")
user := &fakeUser{
privateKey: privateKey,
email: "foo@example.com",
}
config := lego.NewConfig(user)
config.CADirURL = load.PebbleOptions.HealthCheckURL
client, err := lego.NewClient(config)
require.NoError(t, err)
regOptions := registration.RegisterOptions{TermsOfServiceAgreed: true}
reg, err := client.Registration.Register(regOptions)
require.NoError(t, err)
require.Equal(t, reg.Body.Contact, []string{"mailto:foo@example.com"})
user.registration = reg
user.email = "bar@example.com"
resource, err := client.Registration.UpdateRegistration(regOptions)
require.NoError(t, err)
require.Equal(t, resource.Body.Contact, []string{"mailto:bar@example.com"})
require.Empty(t, resource.URI)
}
type fakeUser struct {
email string
privateKey crypto.PrivateKey

View File

@ -115,6 +115,30 @@ func (r *Registrar) QueryRegistration() (*Resource, error) {
}, nil
}
// UpdateRegistration update the user registration on the ACME server.
func (r *Registrar) UpdateRegistration(options RegisterOptions) (*Resource, error) {
if r == nil || r.user == nil {
return nil, errors.New("acme: cannot update a nil client or user")
}
accMsg := acme.Account{
TermsOfServiceAgreed: options.TermsOfServiceAgreed,
Contact: []string{},
}
if r.user.GetEmail() != "" {
log.Infof("acme: Registering account for %s", r.user.GetEmail())
accMsg.Contact = []string{"mailto:" + r.user.GetEmail()}
}
account, err := r.core.Accounts.Update(r.user.GetRegistration().URI, accMsg)
if err != nil {
return nil, err
}
return &Resource{URI: account.Location, Body: account.Account}, nil
}
// DeleteRegistration deletes the client's user registration from the ACME server.
func (r *Registrar) DeleteRegistration() error {
if r == nil || r.user == nil {