2019-03-11 16:56:48 +00:00
|
|
|
package api
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AccountService service
|
|
|
|
|
|
|
|
// New Creates a new account.
|
|
|
|
func (a *AccountService) New(req acme.Account) (acme.ExtendedAccount, error) {
|
|
|
|
var account acme.Account
|
|
|
|
resp, err := a.core.post(a.core.GetDirectory().NewAccountURL, req, &account)
|
|
|
|
location := getLocation(resp)
|
|
|
|
|
|
|
|
if len(location) > 0 {
|
|
|
|
a.core.jws.SetKid(location)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return acme.ExtendedAccount{Location: location}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return acme.ExtendedAccount{Account: account, Location: location}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEAB Creates a new account with an External Account Binding.
|
2020-07-09 23:48:18 +00:00
|
|
|
func (a *AccountService) NewEAB(accMsg acme.Account, kid, hmacEncoded string) (acme.ExtendedAccount, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
hmac, err := base64.RawURLEncoding.DecodeString(hmacEncoded)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return acme.ExtendedAccount{}, fmt.Errorf("acme: could not decode hmac key: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eabJWS, err := a.core.signEABContent(a.core.GetDirectory().NewAccountURL, kid, hmac)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return acme.ExtendedAccount{}, fmt.Errorf("acme: error signing eab content: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
2020-10-27 11:01:05 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
accMsg.ExternalAccountBinding = eabJWS
|
|
|
|
|
|
|
|
return a.New(accMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Retrieves an account.
|
|
|
|
func (a *AccountService) Get(accountURL string) (acme.Account, error) {
|
2021-03-04 19:16:59 +00:00
|
|
|
if accountURL == "" {
|
2018-12-06 21:50:17 +00:00
|
|
|
return acme.Account{}, errors.New("account[get]: empty URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
var account acme.Account
|
2020-11-06 22:30:51 +00:00
|
|
|
_, err := a.core.postAsGet(accountURL, &account)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return acme.Account{}, err
|
|
|
|
}
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 00:07:46 +00:00
|
|
|
// Update Updates an account.
|
2020-05-26 18:04:54 +00:00
|
|
|
func (a *AccountService) Update(accountURL string, req acme.Account) (acme.Account, error) {
|
2021-03-04 19:16:59 +00:00
|
|
|
if accountURL == "" {
|
2020-05-26 18:04:54 +00:00
|
|
|
return acme.Account{}, errors.New("account[update]: empty URL")
|
2019-11-19 00:07:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 18:04:54 +00:00
|
|
|
var account acme.Account
|
2019-11-19 00:07:46 +00:00
|
|
|
_, err := a.core.post(accountURL, req, &account)
|
|
|
|
if err != nil {
|
2020-05-26 18:04:54 +00:00
|
|
|
return acme.Account{}, err
|
2019-11-19 00:07:46 +00:00
|
|
|
}
|
2020-05-26 18:04:54 +00:00
|
|
|
|
2019-11-19 00:07:46 +00:00
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// Deactivate Deactivates an account.
|
|
|
|
func (a *AccountService) Deactivate(accountURL string) error {
|
2021-03-04 19:16:59 +00:00
|
|
|
if accountURL == "" {
|
2018-12-06 21:50:17 +00:00
|
|
|
return errors.New("account[deactivate]: empty URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := acme.Account{Status: acme.StatusDeactivated}
|
|
|
|
_, err := a.core.post(accountURL, req, nil)
|
|
|
|
return err
|
|
|
|
}
|